我是靠谱客的博主 不安鸡翅,这篇文章主要介绍利用hadoop计算WordCount,现在分享给大家,希望可以做个参考。

这是一个简单的计算词频的程序,利用hadoop运行出结果。
简单的分析图:
这里写图片描述
代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; 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 org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class WordCount extends Configured implements Tool{ @Override public int run(String[] args) throws Exception { //获取hadoop的系统配置 Configuration conf = new Configuration() ; //加上URI可以在eclipse上运行改程序 URI uri = new URI("hdfs://hadoop:9000") ; //获取文件系统的配置 FileSystem fs = FileSystem.get(uri, conf) ; //输出文件的目录,结果文件存储的位置 Path outPath = new Path("hdfs://hadoop:9000/ming/wordout") ; //输入文件的目录,含有单词的文件 Path inPath = new Path("hdfs://hadoop:9000/ming/word.txt") ; //如果输出文件存在则删除,不能含有相同的输出文件目录 if(fs.isDirectory(outPath)){ fs.delete(outPath, true) ; } //创建一个工作的对象 Job job = new Job(conf) ; job.setJarByClass(WordCount.class); //设置Mapper的类和输出格式 job.setMapperClass(WCMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); //设置Reducer的类和输出格式 job.setReducerClass(WCReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); //增加文件的输入和输出格式 FileInputFormat.addInputPath(job, inPath); FileOutputFormat.setOutputPath(job, outPath); return job.waitForCompletion(true)?1:0; } public static void main(String[] args) throws Exception { //主函数的入口点 int ec = ToolRunner.run(new Configuration(), new WordCount(), args); System.exit(ec); } public static class WCMapper extends Mapper<LongWritable,Text,Text,LongWritable>{ @Override protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{ //对每行的单词进行切分,以单词作为key,每个单词的词频为1作为value String[] line = value.toString().split("t") ; for(String s : line){ context.write(new Text(s), new LongWritable(1)); } } } public static class WCReducer extends Reducer<Text,LongWritable,Text,Text>{ @Override protected void reduce(Text key,Iterable<LongWritable> values,Context context) throws IOException, InterruptedException{ //计算每个单词的词频总数 long sum = 0 ; for(LongWritable l:values){ sum = sum + l.get(); } context.write(key, new Text(String.valueOf(sum))); } } }

最后

以上就是不安鸡翅最近收集整理的关于利用hadoop计算WordCount的全部内容,更多相关利用hadoop计算WordCount内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部