使用MapReduce统计标题中以'Engineering'结尾的数量

本文将介绍如何使用Java编写一个MapReduce程序,用于统计大量文本数据中标题以'Engineering'结尾的数量。

代码示例javaimport java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.NullWritable;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;

public class TitleCount { public static class TitleMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {            String line = value.toString();            String[] fields = line.split(':');            String title = fields[2];

        if (title.endsWith('Engineering')) {                word.set('engineering');                context.write(word, one);            }        }    }

public static class TitleReducer extends Reducer<Text, IntWritable, NullWritable, IntWritable> {        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {            int count = 0;            for (IntWritable value : values) {                count += value.get();            }            context.write(NullWritable.get(), new IntWritable(count));        }    }

public static void main(String[] args) throws Exception {        Configuration conf = new Configuration();        Job job = Job.getInstance(conf, 'title count');        job.setJarByClass(TitleCount.class);        job.setMapperClass(TitleMapper.class);        job.setCombinerClass(TitleReducer.class);        job.setReducerClass(TitleReducer.class);        job.setOutputKeyClass(NullWritable.class);        job.setOutputValueClass(IntWritable.class);        FileInputFormat.addInputPath(job, new Path(args[0]));        FileOutputFormat.setOutputPath(job, new Path(args[1]));        System.exit(job.waitForCompletion(true) ? 0 : 1);    }}

代码解释

  1. Mapper阶段 (TitleMapper): - 接收每一行文本数据作为输入。 - 将行按冒号':'分割成字段,提取出标题字段。 - 判断标题是否以'Engineering'结尾。 - 如果是,将键设置为'engineering',值设置为1,并输出键值对。

  2. Reducer阶段 (TitleReducer): - 接收Mapper输出的所有键值对。 - 对具有相同键'engineering'的值进行累加,得到最终的计数结果。 - 输出最终结果,键为NullWritable,值为计数结果。

  3. 主程序 (main): - 配置和提交MapReduce作业。 - 设置Mapper、Combiner和Reducer类。 - 设置输入和输出路径。 - 提交作业并等待完成。

运行程序

  1. 将代码编译打包成JAR文件。2. 在Hadoop集群上运行JAR文件,指定输入和输出路径。

例如:

hadoop jar /path/to/your/jar.jar TitleCount /input/path /output/path

注意:

  • 输入路径应指向包含待统计数据的HDFS目录。- 输出路径应指向一个不存在的HDFS目录,用于存储统计结果。

希望本文能够帮助你理解如何使用MapReduce统计标题中以'Engineering'结尾的数量。

使用MapReduce统计标题中以'Engineering'结尾的数量

原文地址: https://www.cveoy.top/t/topic/bITs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录