Hadoop | MapReduce之 WordCount词频统计

WordCount词频统计

词频统计

WordCountMap.java

// Map类,继承于org.apache.hadoop.mapreduce.Mapper;
public class WordCountMap extends Mapper<LongWritable, Text,Text, IntWritable> {

    Text word = new Text();
    IntWritable value = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text t, Context context) throws IOException, InterruptedException {
    	// 切分每一行的数据
        String[] words = t.toString().split(" ");
        // 循环输出
        for (String w : words){
            word.set(w);
            context.write(word,value);
        }
    }

}

WordCountReduce.java

// Reduce类,继承于org.apache.hadoop.mapreduce.Reducer
public class WordCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {

    int sum;
    IntWritable count = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        sum = 0;
		// 累加求和
        for (IntWritable v: values){
            sum += v.get();
        }
        count.set(sum);
        context.write(key,count);

    }
}

WordCountDriver.java

public class WordCountDriver {

    public static void main(String[] args) throws Exception {
    	// 设置操作的用户
        System.setProperty("HADOOP_USER_NAME","root");

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
		// 配置Driver类
        job.setJarByClass(WordCountDriver.class);
		// 配置Map,Reduce类
        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);
		// 配置Map的输出Key类型及Value类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
		// 配置Reduce的输出Key类型及Value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
		// 设置输入路径以及输出路径
        FileInputFormat.setInputPaths(job,new Path("hdfs://192.168.19.16:9000/mol/test.txt"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://192.168.19.16:9000/mol/wordcount"));
		// 启动任务
        Boolean result = job.waitForCompletion(true);
        System.exit(result ? 0:1);

    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Hadoop WordCount 词频统计程序示例: Mapper 类: ```java import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> { @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); // 将每一行按空格分隔成多个单词 String[] words = line.split(" "); // 遍历每个单词,发送到 Reducer 进行统计 for (String word : words) { context.write(new Text(word), new LongWritable(1)); } } } ``` Reducer 类: ```java import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> { @Override public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { long sum = 0; // 遍历同一个单词的所有出现次数,求和 for (LongWritable value : values) { sum += value.get(); } // 将单词和出现次数发送到输出 context.write(key, new LongWritable(sum)); } } ``` Main 方法: ```java import org.apache.hadoop.conf.Configuration; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在执行该程序时,需要在命令行中输入以下命令: ```bash hadoop jar WordCount.jar WordCount /input /output ``` 其中,第一个参数 WordCount.jar 是打包好的程序文件,第二个参数 WordCount 表示程序的类名,后面的 /input 和 /output 分别是输入和输出的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值