Hadoop的簡單單詞統計案例
引入相關Hadoop目錄相關Jar文件:
(hdfs(必須),common(必須),mapreduce(必須))
引入配置文件:
core-site.xml;hdfs-site.xml;……
編寫Map程序:
package cn.guyouda.hadoop.mapreduce.wordcount;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
*
* @author Youda
* Map需要四個泛型參數
* KEYIN:輸入參數:默認是要處理的文本中的某一行的偏移量
* VALUEIN:輸入參數:要處理的某一行文本內容
* VALUEOUT:輸出給Reduce的數據類型
* KEYOUT:輸出給Reduce的偏移量
*
* 由於需要網絡傳輸,故參數需要序列化
* 但是Java自帶的序列化會攜帶一些冗餘信息,不利於大量的網絡傳輸
* 所以Hadoop對Long,String進行了封裝,變爲LongWritable,Text
*
*/
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
// 處理具體的業務邏輯
String text = value.toString();
String[] words = StringUtils.split(text," |,|\\.");
for(String word:words){
context.write(new Text(word), new LongWritable(1));
}
}
}
編寫Reduce程序:
package cn.guyouda.hadoop.mapreduce.wordcount;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
/**
*
* @author Youda
*
*/
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text arg0, Iterable<LongWritable> arg1,
Reducer<Text, LongWritable, Text, LongWritable>.Context arg2) throws IOException, InterruptedException {
Long value = 0L;
for(LongWritable num:arg1){
value += num.get();
}
arg2.write(arg0, new LongWritable(value));
}
}
編寫控制程序:
package cn.guyouda.hadoop.mapreduce.wordcount;
import java.io.IOException;
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;
/**
*
* @author Youda
*指定Map和Reduce類
*指定作業需要處理的數據位置
*還可以指定數據輸出的結果路徑
*/
public class WordCountRunner {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(WordCountRunner.class);
//指定Map和Reduce類
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
//指定Reduce的輸出類型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
//指定Map的輸出類型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
//指定源文件夾和輸出文件夾
FileInputFormat.setInputPaths(job, new Path("/wordcount/srcdata/"));
FileOutputFormat.setOutputPath(job, new Path("/wordcount/output/"));
//提交:參數:是否顯示處理進度
System.exit(job.waitForCompletion(true)?0:1);
}
}
在Hadoop中創建文件夾需要統計的單詞所在文件夾並上傳
運行程序:
注:運行程序前必須啓動YARN(start-yarn.sh)
顯示統計結果:
hadoop fs -cat /wordcount/output/part-r-00000