bum692 2023-12-25 14:27 采纳率: 0%
浏览 18
已结题

hadoop去除敏感词汇

大数据的发展,信息在网络上快速传播,为了使网络环境安全,信息正能量,在实际中,经常会需要去除某文件中不需要的字段,或者一些敏感词汇。
需求:现假设有一篇文章数据文件article.txt,需要过滤的敏感词库在数据文件sensitive.txt中。请过滤这篇文章包含的敏感词汇。

img

  • 写回答

12条回答 默认 最新

  • 吃不了席 2023-12-25 14:35
    关注

    引自微信小程序:皆我百晓生

    您可以使用Hadoop的MapReduce框架来实现对敏感词汇的过滤。下面是一个使用Java编写的示例代码,以演示如何在Hadoop中过滤敏感词汇。

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashSet;
    import java.util.Set;
    
    public class SensitiveWordFilter {
    
        public static class SensitiveWordFilterMapper extends Mapper<LongWritable, Text, Text, Text> {
    
            private Set<String> sensitiveWords = new HashSet<>();
    
            @Override
            protected void setup(Context context) throws IOException, InterruptedException {
                // 从敏感词汇文件中读取敏感词汇并存储到setConfiguration conf = context.getConfiguration();
                Path sensitiveWordsFile = new Path(conf.get("sensitive.words.file"));
                FileSystem fs = FileSystem.get(conf);
                BufferedReader reader = new BufferedReader(new InputStreamReader(fs.open(sensitiveWordsFile)));
                String line;
                while ((line = reader.readLine()) != null) {
                    sensitiveWords.add(line.trim());
                }
                reader.close();
            }
    
            @Override
            protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
                String article = value.toString();
                // 将文章按空格分割成单词
                String[] words = article.split(" ");
                for (String word : words) {
                    if (!sensitiveWords.contains(word)) {
                        context.write(new Text(word), value);
                    }
                }
            }
        }
    
        public static class SensitiveWordFilterReducer extends Reducer<Text, Text, Text, Text> {
    
            @Override
            protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
                // 对具有相同单词的文章进行去重
                Set<String> articles = new HashSet<>();
                for (Text value : values) {
                    articles.add(value.toString());
                }
                for (String article : articles) {
                    context.write(key, new Text(article));
                }
            }
        }
    
        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
            Configuration conf = new Configuration();
            conf.set("sensitive.words.file", "sensitive.txt");  // 设置敏感词汇文件
            Job job = Job.getInstance(conf, "Sensitive Word Filter");
            job.setJarByClass(SensitiveWordFilter.class);
            job.setMapperClass(SensitiveWordFilterMapper.class);
            job.setReducerClass(SensitiveWordFilterReducer.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
            FileInputFormat.addInputPath(job, new Path("article.txt"));  // 输入文件路径
            FileOutputFormat.setOutputPath(job, new Path("output"));  // 输出文件路径
            System.exit(job.waitForCompletion(true) ? 0 : 1);
        }
    }
    

    上述代码将敏感词汇文件路径作为一个配置参数传递给Mapper类。在Mapper类的setup()方法中,从配置中获取敏感词汇文件的路径,并使用FileSystem读取文件内容,将敏感词汇保存到Set<String>中。

    在Mapper类的map()方法中,将输入的文章按空格分割成单词,遍历单词,如果单词不在敏感词汇集合中,则将该单词作为输出的key,文章内容作为输出的value。

    在Reducer类的reduce()方法中,对具有相同单词的文章进行去重,去重后将结果输出。

    main()方法中,通过设置敏感词汇文件路径、输入文件路径和输出文件路径,配置MapReduce作业,并等待作业完成。

    请注意,您需要将输入文件article.txt和敏感词汇文件sensitive.txt在Hadoop集群上进行操作。将代码中的文件路径替换为集群中的实际文件路径。

    完成以上代码后,您可以在Hadoop集群上执行该作业,并获得过滤掉敏感词汇的输出结果。

    评论

报告相同问题?

问题事件

  • 系统已结题 1月2日
  • 创建了问题 12月25日