运行环境:
- CentOS-7.6-Minimal * 3 由Vmware平台搭建
三台主机对应IP地址:192.168.239.100 hadoop100
192.168.239.101 hadoop101
192.168.239.102 hadoop102
环境:hadoop-2.7.7 + JDK-8-251
- 三台虚拟机上节点分配:
hadoop100:DataNode NameNode NodeManager
hadoop200: ResourceManager NodeManager DataNode
hadoop300:NodeManager DataNode JobHistoryServer
- 程序的开发验证由PC机实现
Intellij IDEA 2018.4 + hadoop-2.7.7 + JDK-8-251 + Maven-3.3.9
- 三个验证实验:
- WordCount:词频统计
- TeraSort:Hadoop中的的一个排序作业
- PageRank:对网页进行分析并排序的算法
1. WordCount
这里我们对词频做出统计,我自己的刚开始是找的demo跑的,后来发现hadoop源码里面给出了这个算法的实现,不过自己网页的代码和源码里的代码是一致的运行的方式也大同小异。
原始数据:
word word
ss ss
cls
期望结果
cls 2
ss 2
word 2
1.1 算法思想
按照MapReduce编程规范,分别编写Mapper,Reducer,Driver
(1) Mapper
① 将MapperTask传过来的原始文本内容转化成String
1
2第一行:word word
② 根据空格将每一行的String切分成独立的单词
1
2
3word word
③ 将单词输出为<key,<1,1,1,1, …> >,输出给Reducer
1
2< word, <1,1> >
(2)Reducer
① 汇总接收的单词,计数每个单词”1“的数量
② 输出<单词,key的总次数(累加)>
1
2输出:word,2
(3)Driver
① 获取配置信息,configuration不设定为默认值,创建job对象实例
② 指定本程序的jar包所在的本地路径
③ 关联Mapper和Reducer的业务类
④ 设置Mapper和Reducer的输出格式
⑤ 指定job的输入原始文件所在目录,输出文件所在目录
⑥ 提交job,提交成功打印数字0,否则打印1
1.2 在hadoop集群上运行WordCount
源码(这里展示官方的源码)
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
57public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length < 2) { System.err.println("Usage: wordcount <in> [<in>...] <out>"); System.exit(2); } Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); for (int i = 0; i < otherArgs.length - 1; ++i) { FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
跑程序流程
- 自己写的python随机字符串生成代码,用来产生测试数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import random import string f1 = open('1.txt', 'x') for x in range(10000): string0 = str(''.join(random.sample( ['z', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'], 4))) f1.write(string0) if x % random.randint(1, 50) == 0: f1.write(' ') continue if x != 9999: f1.write('n') f1.close()
- 上传自己的txt文件到hdfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15$ hadoop fs -put /home/hadoop1/data/input/1.txt hdfs://192.168.239.100:9000/input #或者 $ hadoop fs -put /home/hadoop1/data/input/1.txt /input #查看上传结果 $ hadoop fs -ls /input #另:hdfs常用命令 hdfs dfs -copyFromLocal #local/data /hdfs/data:将本地文件上传到 hdfs 上(原路径只能是一个文件) hdfs dfs -put /tmp/ /hdfs/ #和 copyFromLocal 区别是,put 原路径可以是文件夹等 hadoop fs -ls / #查看根目录文件 hadoop fs -ls /tmp/data #查看/tmp/data目录 hadoop fs -cat /tmp/a.txt #查看 a.txt,与 -text 一样 hadoop fs -mkdir dir #创建目录dir hadoop fs -rmr dir:#删除目录dir
- 执行mapreduce
1
2
3
4
5
6
7
8
9
10
11#执行自己打包的demo $ hadoop jar WordCount_1-1.0-SNAPSHOT.jar com/hadoop1/wordcount.WdDriver /input/1.txt /out #在此之后,自己写的代码都需要如此执行,该执行方式的说明: #com/hadoop1/wordcount.WdDriver 对应的类是WdDriver,所在包:com.hadoop1.wordcount $ hadoop jar <jar文件> <注明主类> <hdfs输入文件> <hdfs输出文件> #执行官方源码中的WordCount $ hadoop jar /opt/module/hadoop-2.7.7/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.7.jar wordcount /input/1.txt /out #两种方式运行结果相同 注意在运行时,如果输出的文件夹已经存在,就会报错,在再次运行之前最好把相关的文件夹给删掉
- 查看运行结果
1
2
3$ hadoop fs -ls /out $ hadoop fs -cat /out/part-r-00000
- 运行时长 因为数据量很小,所以运行时间很短:11"
1.3 参考
【Hadoop】MapReduce编程之WordCount案例
在命令行中运行Hadoop自带的WordCount程序
最后
以上就是鳗鱼纸鹤最近收集整理的关于1. WordCount在Hadoop分布式集群中的实现的全部内容,更多相关1.内容请搜索靠谱客的其他文章。
发表评论 取消回复