map reduce 实战

2019-02-12  本文已影响11人  tracy_668

map reduce原理

问题:怎样解决海量数据的计算?

执行步骤

map任务处理

reduce任务处理

word count实例

image.png

map代码

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

import java.io.IOException;

public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        //接收一行数据
        String line = value.toString();
        //分割
        String[] words = line.split(" ");
        //迭代
        for(String w : words){
            //发送
            context.write(new Text(w), new LongWritable(1));
        }
    }

    
}

reduce代码

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> {

    @Override
    protected void reduce(Text key, Iterable<LongWritable> v2s, Context context)
            throws IOException, InterruptedException {
        //定义一行计数器
        long sum = 0;
        //迭代他的次数
        for(LongWritable lw : v2s){
            //求和
            sum += lw.get();
        }
        //输出
        context.write(key, new LongWritable(sum));
    }

    
}
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;
/**
 * 1.定义一个类,这个类是MR执行入口
 * 2.定义一个类,这个类要继承org.apache.hadoop.mapreduce.Mapper,指定泛型的类型(k1,v1和k2,v2),然后重新map方法,接收数据,实现具体的业务逻辑
 * 3.定义一个类,这个类要继承org.apache.hadoop.mapreduce.Reducer,指定泛型的类型(k2,v2和k3,v3),然后重新reduce方法,接收数据(k2和v2s,已经分组),实现具体的业务逻辑
 * 4.在main方法中将自定义的mapper和reducer组装起来,提交
 * 5.打包
 * @author zx
 *
 */
public class WordCount {

    public static void main(String[] args) throws Exception {
        // 构造一个Job对象,job对象要依赖Configuration
        Configuration conf = new Configuration();
        conf.set("mapreduce.framework.name", "yarn");
        conf.set("yarn.resourcemanager.hostname", "itcast01");
        Job job = Job.getInstance(conf);
        
        //注意:要将main所在的类设置一下
        job.setJarByClass(WordCount.class);
        
        //设置Mapper相关的属性
        job.setMapperClass(WCMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        
        //设置Reducer相关属性
        job.setReducerClass(WCReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        
        job.setCombinerClass(WCReducer.class);
        
        //提交任务
        job.waitForCompletion(true);
        
        
        
    }
    
    

}

详细代码见:worcount

idea 打jar包

image.png image.png

上传wordCount.txt到hdfs: hadoop fs -put wordCount.txt

执行MR的命令
hadoop jar <jar在linux的路径> <main方法所在的类的全类名> <参数>
例子:
hadoop jar mr.jar hdfs://server-2:9000/wordCount.txt /out2

得到结果:

 hadoop fs -cat /out2/part-r-00000
hard    1
hello   2
nihao   2
study   1
world   1
wusong  2
yes     1
上一篇下一篇

猜你喜欢

热点阅读