1. WordCount在Hadoop分布式集群中的实现

运行环境

  1. 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

  1. 三台虚拟机上节点分配:

hadoop100:DataNode NameNode NodeManager

hadoop200: ResourceManager NodeManager DataNode

hadoop300:NodeManager DataNode JobHistoryServer

  1. 程序的开发验证由PC机实现

Intellij IDEA 2018.4 + hadoop-2.7.7 + JDK-8-251 + Maven-3.3.9

  1. 三个验证实验:

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

 第一行:word word

② 根据空格将每一行的String切分成独立的单词

 word
 word

③ 将单词输出为<key,<1,1,1,1, …> >,输出给Reducer

 < word, <1,1> >

(2)Reducer
① 汇总接收的单词,计数每个单词”1“的数量

② 输出<单词,key的总次数(累加)>

输出:word,2

(3)Driver
① 获取配置信息,configuration不设定为默认值,创建job对象实例

② 指定本程序的jar包所在的本地路径

③ 关联Mapper和Reducer的业务类

④ 设置Mapper和Reducer的输出格式

⑤ 指定job的输入原始文件所在目录,输出文件所在目录

⑥ 提交job,提交成功打印数字0,否则打印1

1.2 在hadoop集群上运行WordCount

源码(这里展示官方的源码)

public 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随机字符串生成代码,用来产生测试数据
import 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
$ 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
#执行自己打包的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

#两种方式运行结果相同 注意在运行时,如果输出的文件夹已经存在,就会报错,在再次运行之前最好把相关的文件夹给删掉
  • 查看运行结果
$ hadoop fs -ls /out
$ hadoop fs -cat /out/part-r-00000
  • 运行时长 因为数据量很小,所以运行时间很短:11"

在这里插入图片描述

1.3 参考

【Hadoop】MapReduce编程之WordCount案例
在命令行中运行Hadoop自带的WordCount程序

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值