MapReduce案例2

案例1.统计一个单词分别在每个文件中出现几次

第一个MapReduce

package com.doit.index;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


import java.io.IOException;

public class InveseIndexOne {

    public static class InveseIndexOneMap extends Mapper<LongWritable, Text,Text, IntWritable>{
        String fileName = null;
        Text k = new Text();
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
             FileSplit inputSplit = (FileSplit) context.getInputSplit();
            fileName = inputSplit.getPath().getName();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] words = value.toString().split(" ");
            for(String word : words){
                k.set(word + "-" +fileName);
                context.write(k,new IntWritable(1));
            }

        }
    }

    public static class InveseIndexOneReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            Integer count = 0;
            for (IntWritable value : values){
                count++;
            }
            context.write(key,new IntWritable(count));
        }
    }

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

        Configuration cong = new Configuration();
        Job job = Job.getInstance(cong);

        job.setJarByClass(InveseIndexOne.class);
        job.setMapperClass(InveseIndexOneMap.class);
        job.setReducerClass(InveseIndexOneReduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path("E:\\mrtest\\index\\input"));
        Path output =  new Path("E:\\mrtest\\index\\output-one");

        FileSystem fileSystem = FileSystem.get(cong);
        boolean exists = fileSystem.exists(output);
        if(exists){
            fileSystem.delete(output,true);
        }

        FileOutputFormat.setOutputPath(job,output);
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

第二个MapReduce

package com.doit.index;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class InveseIndexTwo {
public static class InveseIndexTwoMap extends Mapper<LongWritable, Text,Text,Text> {


    //and-b.txt	2
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split("-");
        //and
        String word = split[0];
        //b.txt 2
        String index = split[1];
        context.write(new Text(word),new Text(index));
    }
}
public static class InveseIndexTwoReducer extends Reducer<Text,Text,Text,Text>{
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        // 线程不安全的StringBulider效率更高,在此处并不涉及线程安全问题
        StringBuilder sb = new StringBuilder();
        for (Text value : values){
            sb.append(value).append(",");
        }
        context.write(key,new Text(sb.toString()));
    }
}

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

    Configuration cong = new Configuration();
    Job job = Job.getInstance(cong);

    job.setJarByClass(InveseIndexTwo.class);
    job.setMapperClass(InveseIndexTwoMap.class);
    job.setReducerClass(InveseIndexTwoReducer.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    FileInputFormat.setInputPaths(job,new Path("E:\\mrtest\\index\\output-one"));
    Path output =  new Path("E:\\mrtest\\index\\output-two");

    FileSystem fileSystem = FileSystem.get(cong);
    boolean exists = fileSystem.exists(output);
    if(exists){
        fileSystem.delete(output,true);
    }

    FileOutputFormat.setOutputPath(job,output);
    boolean b = job.waitForCompletion(true);
    System.exit(b?0:1);
}

案例2 ,按手机号分区输出

MapReduce

package com.doit.flow;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FlowSum {
public static class FlowSumMap extends Mapper<LongWritable, Text,Text, IntWritable>{
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] split = value.toString().split("\t");
        String telleNum = split[1];
        Integer upFlow = Integer.parseInt(split[8]);
        Integer donwFlow = Integer.parseInt(split[9]);
        Integer sumFlow = upFlow + donwFlow;
        context.write(new Text(telleNum),new IntWritable(sumFlow));

    }
}

public static class FlowSumReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        Integer heFlow = 0;
        for (IntWritable value : values){
            heFlow += value.get();
        }
        context.write(key,new IntWritable(heFlow));
    }
}

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf);

    job.setJarByClass(FlowSum.class);
    //
    job.setNumReduceTasks(8);
    //具体使用哪个partition
    job.setPartitionerClass(PrivatePatitioner.class);

    job.setMapperClass(FlowSumMap.class);
    job.setReducerClass(FlowSumReduce.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    FileInputFormat.setInputPaths(job,new Path("E:\\mrtest\\flow\\input"));

    Path outputPath = new Path("E:\\mrtest\\flow\\output-partition");

    FileSystem fs = FileSystem.get(conf);

    if(fs.exists(outputPath)){
        fs.delete(outputPath,true);
    }
    FileOutputFormat.setOutputPath(job,outputPath);
    boolean b = job.waitForCompletion(true);
    System.exit(b?0:1);
}

partitioner分区类

package com.doit.flow;

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


import java.util.HashMap;

public class PrivatePatitioner extends Partitioner<Text, IntWritable> {

private static HashMap<String,Integer> privateMap = new HashMap<>();
static {
    privateMap.put("135",0);
    privateMap.put("136",1);
    privateMap.put("137",2);
    privateMap.put("138",3);
    privateMap.put("139",4);
}


@Override
public int getPartition(Text text, IntWritable intWritable, int i) {

    //135
    String tellNum = text.toString().substring(0, 3);
    Integer partitionCode = privateMap.get(tellNum);
    return partitionCode == null ? 5 : partitionCode;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值