1.Hadoop_MapReduce WordCount程序编写
Posted 蒸气awa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.Hadoop_MapReduce WordCount程序编写相关的知识,希望对你有一定的参考价值。
一、重写三个方法Mapper、Reducer和Driver
二、对于Mapper
package my.study.mapreduce.wordcount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/*
* KEYIN:,map阶段输入的Key的类型 :LongWritable;
* VALUEIN,map阶段输入value类型:Text(String);
* KEYOUT, map阶段输出的Key类型:Text;
* VALUEOUT, map阶段输出的value类型:IntWritable(Int)
*
* */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>
private Text outK = new Text();
private IntWritable outV = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
//1获取一行
String line = value.toString();
//2切割
String[] words = line.split(" ");
//3循环写出
for(String word : words)
//封装
outK.set(word);
//写出
context.write(outK,outV);
三、对于Reducer
package my.study.mapreduce.wordcount;
/*
* KEYIN:,reduce阶段输入的Key的类型 :Text;
* VALUEIN,reduce阶段输入value类型:IntWritable;
* KEYOUT, reduce阶段输出的Key类型:Text;
* VALUEOUT, reduce阶段输出的value类型:IntWritable(Int)
*
* */
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable>
private IntWritable outV = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException
int sum = 0;
//ss,(1,1)
for (IntWritable value : values)
sum += value.get();
outV.set(sum);
//写出
context.write(key, outV);
四、对于driver
package my.study.mapreduce.wordcount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WordCountDriver
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException
//1获取jop
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//2获取jar包路径
job.setJarByClass(WordCountDriver.class);
//3关联mapper和reducer
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
//4设置map输出的kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//5设置最终输出的kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//6设置输入路径和输出路径
FileInputFormat.setInputPaths(job, new Path("D:\\\\hadoop\\\\input\\\\wordCount\\\\words.txt"));
FileOutputFormat.setOutputPath(job, new Path("D:\\\\hadoop\\\\output\\\\wordCount"));
//7提交job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
五、debug调试节点设置
1.map方法:String line = value.toString();
mapper中run方法:setup(context); 和 cleanup(context);
2.reduce方法:int sum = 0;
Reducer中run方法:setup(context); 和 cleanup(context);
六、集群运行
hadoop jar jar包 copy_Reference 输入路径 输出路径
以上是关于1.Hadoop_MapReduce WordCount程序编写的主要内容,如果未能解决你的问题,请参考以下文章