Hadoop框架下运行MapReduce程序

本文介绍了在Linux中Hadoop环境下,利用mapReduce框架写wordCount应用程序的主要方法,并且提供程序的解释说明。

  1. 首先在工程中创建一个package:my.examples.hadoop.mr,在这个包下新建一个class:WCMapper;再新建一个class:WCReducer;最后新建一个class:WCRunner。

  2. WCMapper
    主要说明:map 和 reduce 的数据输入输出都是以 key-value对的形式封装的.

package my.examples.hadoop.mr;

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;

public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{   
    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
/*
在Mapper<LongWritable, Text, Text, LongWritable>4个泛型中,前两个是指定mapper输入数据的类型,KEYIN是输入的key的类型,VALUEIN是输入的value的类型
*/
//map 和 reduce 的数据输入输出都是以 key-value对的形式封装的
/*
默认情况下,框架传递给我们的mapper的输入数据中,key是要处理的文本中一行的起始偏移量,这一行的内容作为value
*/      
//下面这一行代码是把读到的一行数据的内容转换成string类型
        String line = value.toString();
//下面这一行代码是读到的一行数据的转化好的string类型的文本按特定分隔符切分,功能是分割成一个个单词,保存到数组中        
        String[] words = StringUtils.split(line, " ");
//下面for循环遍历这个单词数组输出为kv形式  k:单词   v : 1      
        for(String word : words){

            context.write(new Text(word), new LongWritable(1));

        }       
    }   
}

3.WCReducer

package my.examples.hadoop.mr;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable>{



    //框架在map处理完成之后,将所有kv对缓存起来,进行分组,然后传递一个组<key,valus{}>,调用一次reduce方法
    //例如<hello,{1,1,1,1,1,1.....}>
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values,Context context)
            throws IOException, InterruptedException {

        long count = 0;
        //遍历value的list,进行累加求和
        for(LongWritable value:values){

            count += value.get();
        }

        //输出这一个单词的统计结果

        context.write(key, new LongWritable(count));

    }
}

4.WCRunner

package my.examples.hadoop.mr;

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;

/**
 * 用来描述一个特定的作业
 * 比如,该作业使用哪个类作为逻辑处理中的map,哪个作为reduce
 * 还可以指定该作业要处理的数据所在的路径
 * 还可以指定改作业输出的结果放到哪个路径
 * ....
 * @author duanhaitao@itcast.cn
 *
 */
public class WCRunner {

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();

        Job wcjob = Job.getInstance(conf);

        //设置整个job所用的那些类在哪个jar包
        wcjob.setJarByClass(WCRunner.class);


        //本job使用的mapper和reducer的类
        wcjob.setMapperClass(WCMapper.class);
        wcjob.setReducerClass(WCReducer.class);


        //指定输出数据kv类型(如果M和R的相同,不同的话,下面再写mapper的输出类型)
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(LongWritable.class);

        //指定mapper的输出数据kv类型(如果和上面参数一样的话可以省略不写)
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(LongWritable.class);


        //指定要处理的输入数据存放路径
        FileInputFormat.setInputPaths(wcjob, new Path("hdfs://192.168.126.100:9000/wc/srcdata/"));

        //指定处理结果的输出数据存放路径
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://192.168.126.100:9000/wc/output3/"));

        //将job提交给集群运行 
        wcjob.waitForCompletion(true);  
    }
}

5.打成Jar包后,创建数据的源文件,存放在输入文件夹下,在hadooop环境下执行,
命令是:
hadoop jar wc.jar my.examples.hadoop.mr.WCRunner
源文件中输入:
hello world
hello tom
hello jim
baby is my nvshen
运行过程
16/04/13 21:00:39 INFO client.RMProxy: Connecting to ResourceManager at weekend110/192.168.126.100:8032
16/04/13 21:00:40 WARN mapreduce.JobSubmitter: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
16/04/13 21:00:40 INFO input.FileInputFormat: Total input paths to process : 1
16/04/13 21:00:40 INFO mapreduce.JobSubmitter: number of splits:1
16/04/13 21:00:41 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1460606168153_0001
16/04/13 21:00:41 INFO impl.YarnClientImpl: Submitted application application_1460606168153_0001
16/04/13 21:00:41 INFO mapreduce.Job: The url to track the job: http://weekend110:8088/proxy/application_1460606168153_0001/
16/04/13 21:00:41 INFO mapreduce.Job: Running job: job_1460606168153_0001
16/04/13 21:00:50 INFO mapreduce.Job: Job job_1460606168153_0001 running in uber mode : false
16/04/13 21:00:50 INFO mapreduce.Job: map 0% reduce 0%
16/04/13 21:00:55 INFO mapreduce.Job: map 100% reduce 0%
16/04/13 21:01:00 INFO mapreduce.Job: map 100% reduce 100%
16/04/13 21:01:01 INFO mapreduce.Job: Job job_1460606168153_0001 completed successfully
16/04/13 21:01:01 INFO mapreduce.Job: Counters: 49
File System Counters
FILE: Number of bytes read=156
FILE: Number of bytes written=186093
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=160
HDFS: Number of bytes written=54
HDFS: Number of read operations=6
HDFS: Number of large read operations=0
HDFS: Number of write operations=2
Job Counters
Launched map tasks=1
Launched reduce tasks=1
Data-local map tasks=1
Total time spent by all maps in occupied slots (ms)=3037
Total time spent by all reduces in occupied slots (ms)=2342
Total time spent by all map tasks (ms)=3037
Total time spent by all reduce tasks (ms)=2342
Total vcore-seconds taken by all map tasks=3037
Total vcore-seconds taken by all reduce tasks=2342
Total megabyte-seconds taken by all map tasks=3109888
Total megabyte-seconds taken by all reduce tasks=2398208
Map-Reduce Framework
Map input records=5
Map output records=10
Map output bytes=130
Map output materialized bytes=156
Input split bytes=108
Combine input records=0
Combine output records=0
Reduce input groups=8
Reduce shuffle bytes=156
Reduce input records=10
Reduce output records=8
Spilled Records=20
Shuffled Maps =1
Failed Shuffles=0
Merged Map outputs=1
GC time elapsed (ms)=139
CPU time spent (ms)=1310
Physical memory (bytes) snapshot=218542080
Virtual memory (bytes) snapshot=725782528
Total committed heap usage (bytes)=137433088
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
File Input Format Counters
Bytes Read=52
File Output Format Counters
Bytes Written=54
查看输出文件
命令:hadoop fs -cat /wc/output/part-r-00000
baby 1
hello 3
is 1
jim 1
my 1
nvshen 1
tom 1
world 1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop编写MapReduce程序是指使用Hadoop框架来实现MapReduce算法。MapReduce是一种分布式计算模型,它将大规模数据集分成小的数据块,然后在分布式计算集群上并行处理这些数据块。MapReduce程序由两个部分组成:Map和Reduce。 Map阶段:Map阶段将输入数据分成小的数据块,然后对每个数据块进行处理,生成键值对。Map阶段的输出结果是一个键值对列表。 Reduce阶段:Reduce阶段将Map阶段输出的键值对列表进行合并,生成最终的输出结果。Reduce阶段的输出结果是一个键值对列表。 编写MapReduce程序的步骤如下: 1. 定义Map函数:Map函数将输入数据分成小的数据块,然后对每个数据块进行处理,生成键值对。 2. 定义Reduce函数:Reduce函数将Map函数输出的键值对列表进行合并,生成最终的输出结果。 3. 定义输入格式:定义输入数据的格式,例如文本文件、CSV文件等。 4. 定义输出格式:定义输出数据的格式,例如文本文件、CSV文件等。 5. 配置Hadoop环境:配置Hadoop环境,包括Hadoop的安装、配置、启动等。 6. 编写MapReduce程序:编写MapReduce程序,包括Map函数、Reduce函数、输入格式、输出格式等。 7. 运行MapReduce程序:将编写好的MapReduce程序提交到Hadoop集群上运行。 8. 查看输出结果:查看MapReduce程序的输出结果,进行调试和优化。 以上就是Hadoop编写MapReduce程序的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值