MapReduce Join(二)--Reduce Join编程实现

案例要求

订单数据orders

订单号商品编号数量
100001033
100002021
100001044
100003011
100004012

商品数据produce

商品编号商品名称
01小米
02华为
03中兴
04联想

要求:对produce和orders按照商品编号进行连接

程序代码

package com.zhiyou100.hadoop.mr.join.reduce;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;

public class ReduceJoin {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //本地运行
        conf.set("mapreduce.framework.name", "local");
        //集群运行配置
        //System.setProperty("HADOOP_USER_NAME", "root");

        Job job = Job.getInstance(conf);

        job.setJarByClass(ReduceJoin.class);
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);
        //map输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(TableWritable.class);
        //reduce输出类型
        job.setOutputKeyClass(TableWritable.class);
        job.setOutputValueClass(NullWritable.class);
        //文件输入输出路径
        FileInputFormat.setInputPaths(job, new Path("E:\\input\\order\\"));
        FileOutputFormat.setOutputPath(job, new Path("E:\\output\\order\\"));

        System.exit(job.waitForCompletion(true)?0:1);   
    }
}

class MyMapper extends Mapper<LongWritable, Text, Text, TableWritable>{
    private TableWritable tw = new TableWritable();
    private Text k = new Text();
    @Override
    protected void map(LongWritable key, Text value,Context context)
            throws IOException, InterruptedException {
        //1.获取文件(输入切片)名
        FileSplit fs = (FileSplit) context.getInputSplit();
        String fileName = fs.getPath().getName();
        //2.获取输入数据
        String line = value.toString();
        if(fileName.equals("a.txt")) {
            String[] strs = line.split("\t");
            tw.setOid(strs[0]);
            tw.setPid(strs[1]);
            tw.setAmount(Integer.parseInt(strs[2]));
            tw.setFname(fileName);

            k.set(strs[1]);
        }else {
            String[] strs = line.split("\t");
            tw.setPid(strs[0]);
            tw.setPname(strs[1]);
            tw.setFname(fileName);

            k.set(strs[0]);
        }
        context.write(k, tw);
    }
}

class MyReducer extends Reducer<Text, TableWritable, TableWritable, NullWritable>{
    @Override
    protected void reduce(Text key, Iterable<TableWritable> values,Context context) 
            throws IOException, InterruptedException {
        //创建一个临时的list存储订单对象
        List<TableWritable> orders = new ArrayList<>();
        TableWritable produce = new TableWritable();
        for (TableWritable value : values) {
            if(value.getFname().equals("a.txt")) {
                TableWritable order = new TableWritable();
                try {
                    //使用工具类拷贝
                    BeanUtils.copyProperties(order, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                orders.add(order);
            }else {
                try {
                    //使用工具类拷贝
                    BeanUtils.copyProperties(produce, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        for (TableWritable order : orders) {
            order.setPname(produce.getPname());
            context.write(order, NullWritable.get());
        }
    }
}

输出文件显示:

10004,小米,2
10003,小米,1
10002,华为,1
10001,中兴,3
10001,联想,4

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值