Hadoop 作业链条化实现

作业链条化编程
[MAP+ / REDUCE MAP*]

实现功能:
    单词统计

 需求:
    1、过滤敏感词汇
    2、过滤单词出现小于5的词汇

 链条结构:
    MapMapper1(映射) + MapMapper2(过滤敏感词汇) + Reducer(聚合) + ReduceMapper1(过滤小于5的词汇)

   WCApp.class

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

/*
    作业链条化编程
    [MAP+ / REDUCE MAP*]

    实现功能:
        单词统计

     需求:
        1、过滤敏感词汇
        2、过滤单词出现小于5的词汇

     链条结构:
        MapMapper1(映射) + MapMapper2(过滤敏感词汇) + Reducer(聚合) + ReduceMapper1(过滤小于5的词汇)
 */
public class WCApp {
    public static void main(String[] args) throws Exception {
        System.setProperty("hadoop.home.dir", "H:\\hadoop-2.4.1");
        Configuration conf = new Configuration();

        //本地文件目录
        conf.set("fs.defaultFS","file:///");

        //先删除输出的目录
        /*if (args.length > 1) {
            //因为配置的是本地文件系统,而且删除的地址也是本地的,所以可以删除
            FileSystem.get(conf).delete(new Path(args[1]),true);
        }*/

        Job job = Job.getInstance(conf);
        job.setJobName("WCApp");
        job.setNumReduceTasks(3);       //设置Reducer的数量
        job.setJarByClass(WCApp.class); //搜索类

        //在Map链条上增加 MapMapper1
        ChainMapper.addMapper(job,WCMapMapper1.class, LongWritable.class,Text.class,Text.class,IntWritable.class,new Configuration(false));
        //在Map链条上增加 MapMapper2
        ChainMapper.addMapper(job,WCMapMapper2.class, Text.class,IntWritable.class,Text.class,IntWritable.class,new Configuration(false));
        //在Reduce链条上增加 Reduce
        ChainReducer.setReducer(job,WCReducer.class,Text.class,IntWritable.class,Text.class,IntWritable.class,new Configuration(false));
        //在Reduce链条上增加 ReduceMapper1
        ChainReducer.addMapper(job,WCReduceMapper1.class,Text.class,IntWritable.class,Text.class,IntWritable.class,new Configuration(false));

        job.setInputFormatClass(TextInputFormat.class); //设置输入格式
        //job.setOutputFormatClass(SequenceFileOutputFormat.class); //设置输出格式
        job.setOutputFormatClass(TextOutputFormat.class); //设置输出格式

        //设置切片的最小和最大值
        FileInputFormat.setMinInputSplitSize(job,1);
        FileInputFormat.setMaxInputSplitSize(job,Long.MAX_VALUE);

        FileInputFormat.setInputPaths(job, new Path("e:/mr/txt"));
        FileOutputFormat.setOutputPath(job, new Path("e:/mr/out"));

        //打印详细信息
        job.waitForCompletion(true);

    }
}

WCMapMapper1.class

import com.fresher.hdfs.mr.Util;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * MapMapper1 映射
 */
public class WCMapMapper1 extends Mapper{
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //分词
        String[] words = value.toString().split(" ");
        //输出
        for (String word : words) {
            context.write(new Text(word), new IntWritable(1));
        }
        System.out.println("---WCMapMapper1.map()");
        context.getCounter("m",Util.getInfo(this,"map")).increment(1);
    }
}

WCMapMapper2.class

import com.fresher.hdfs.mr.Util;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * MapMapper2 过滤
 */
public class WCMapMapper2 extends Mapper{
    @Override
    protected void map(Text key, IntWritable value, Context context) throws IOException, InterruptedException {
        if (!key.toString().contains("isis")) {
            context.write(key,value);
        }
        System.out.println("---WCMapMapper2.map()");
        context.getCounter("m",Util.getInfo(this,"map")).increment(1);
    }
}

WCReducer.class

import com.fresher.hdfs.mr.Util;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Reducer 聚合
 */
public class WCReducer extends Reducer {
    @Override
    protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
        int total = 0;
        for (IntWritable v : values) {
            total += v.get();
        }
        context.write(key,new IntWritable(total));
        System.out.println("---WCReducer.reduce()");
        context.getCounter("r",Util.getInfo(this,"reduce")).increment(1);
    }
}

WCReduceMapper1.class

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

import java.io.IOException;

/**
 * ReduceMapper1 过滤 小于5次的词汇
 */
public class WCReduceMapper1 extends Mapper{
    @Override
    protected void map(Text key, IntWritable value, Context context) throws IOException, InterruptedException {
        if (value.get() >= 5) {
            context.write(key,value);
        }
    }
}

 

你可能感兴趣的:(Hadoop)