java获取hadoop目录结构_Java从WordCount看hadoop执行流程

准备

要执行Map reduce程序,首先得安装hadoop,hadoop安装可以参考hadoop安装

启动hdfs和yarn

start-dfs.cmdstart-yarn.cmd

创建待处理数据目录:

hadoop fs -mkdir /wchadoop fs -mkdir /wc/in# 查看目录hadoop fs -ls -R /

上传待处理数据文件:

hadoop fs -put file:///G:\note\bigdata\hadoop\wordcount\word1.txt /wc/inhadoop fs -put file:///G:\note\bigdata\hadoop\wordcount\word2.txt /wc/in

其中数据文件内容如下: word1.txt

hello worldhello hadoop

word2.txt

hadoop worldhadoop learn

bdc1c3cc89e03c9269ac30af47031f2c.png

WordCount与Map Reduce流程

a94b1f52605aed0fb4b9d9ee12f1c88d.png

701ace7efc47f9dafc044b385f1d1ab7.png

b52bcc67c53347e82bbc36cd4d982ffd.png

如果输出目录已经存在,可以使用下面的命令删除:

hadoop fs -rm -r /wc/out

我们先来看一下程序的输出:

2454fd307cef0ba3e389202a15c4a429.png

从输出我们可以推测hadoop的map过程是:hadoop把待处理的文件按行拆分,每一行调用map函数,map函数的key就是每一行的起始位置,值就是这一行的值。

map处理之后,再按key-value的形式写中间值。

reduce函数就是处理这些中间过程,参数的key就是map写入的key,value就是,map之后按key分组的value。

Combin过程

再Map和Reduce中间还可以加入Combin过程,用于处理中间结果,减少网络间数据传输的数据量。

Map->Reduce->Combin

我们把上面程序中job.setCombinerClass(IntSumReducer.class);注释去掉就可以获取到有Combiner的输出:

61e0933f13c7de7b7f2b68828fb73ac7.png

从上面的输出我们可以看到,map之后有一个reduce输出,其实是combin操作,combin和reduce的区别是combin是在单节点内部执行的,为了减小中间数据。

注意:combin操作必须满足结合律,例如:

加法,求总和:a+b+c+d = (a+b) + (c+d)最大值:max(a+b+c+d) = max(max(a,b),max(c,d))均值就不能使用combin操作: (a+b+c+d)/4 明显不等价于 ((a+b)/2 + (c+d)/2)/2

pom文件

52a8e3e40667dad92d5b07f8d2635e9b.png

d21e459f1785cf78b1812f72f324d451.png

66734880fe3b267727e368afa162268f.png

2ddf702482733899da0f0a6760b224ca.png

d1250c82726dd579b8ec0f983c16ae68.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是针对Hadoop官网提供的WordCount示例代码的每一行Java代码的注释: ```java import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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; 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(); 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); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 其中,包括以下几个类和方法: - `import`:导入所需的类和方法。 - `public class WordCount`:定义了一个公共类WordCount,其中包含了一个main方法和两个静态内部类。 - `public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>`:定义了一个静态内部类TokenizerMapper,继承了Mapper类,并重写了其中的map方法。该类用于将输入的文本进行分词处理,并将每个单词输出为(key, value)的形式,其中key为单词,value为1。 - `public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable>`:定义了一个静态内部类IntSumReducer,继承了Reducer类,并重写了其中的reduce方法。该类用于将相同单词的value值进行累加,并输出为(key, value)的形式,其中key为单词,value为该单词的出现次数。 - `public static void main(String[] args) throws Exception`:定义了一个静态的主方法,其中包含了创建Job实例、设置Mapper和Reducer类、设置输入和输出路径等步骤,最后启动Job并等待其完成的代码。 - `Configuration conf = new Configuration()`:创建了一个Configuration对象,用于存储Hadoop集群的配置信息。 - `Job job = Job.getInstance(conf, "word count")`:创建了一个Job实例,用于描述一个Hadoop作业。 - `job.setJarByClass(WordCount.class)`:指定了作业运行时所需的jar包。 - `job.setMapperClass(TokenizerMapper.class)`:指定了Mapper类。 - `job.setCombinerClass(IntSumReducer.class)`:指定了Combiner类。 - `job.setReducerClass(IntSumReducer.class)`:指定了Reducer类。 - `job.setOutputKeyClass(Text.class)`:指定了输出的key类型。 - `job.setOutputValueClass(IntWritable.class)`:指定了输出的value类型。 - `FileInputFormat.addInputPath(job, new Path(args[0]))`:指定了输入路径。 - `FileOutputFormat.setOutputPath(job, new Path(args[1]))`:指定了输出路径。 - `System.exit(job.waitForCompletion(true) ? 0 : 1)`:启动Job并等待其完成,并根据Job的运行结果返回相应的状态码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值