MapReduce连接join操作

MapReduce连接操作

➜  student git:(master) ✗ hadoop dfs -cat /student/student_info.txt
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.

18/08/05 15:35:11 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Jenny   00001
Hardy   00002
Bradley 00003
➜  student git:(master) ✗ hadoop dfs -cat /student/student_class_info.txt
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.

18/08/05 15:35:37 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
00001   Chinese
00001   Math
00002   Music
00002   Math
00003   Physic
package cn.chen.hd.mr;

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.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 org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JoinDriver{
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf, "MRJoin");

        job.setJarByClass(JoinDriver.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/student"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/student/output"));

        job.setMapperClass(JoinMapper.class);
        job.setReducerClass(JoinReducer.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        System.out.println(job.waitForCompletion(true) ? 0 : 1);
    }
}

class JoinMapper extends Mapper<LongWritable, Text, Text, Text> {

    public static final String LEFT_FILENAME = "student_info.txt";
    public static final String RIGHT_FILENAME = "student_class_info.txt";
    public static final String LEFT_FILENAME_FLAG = "l";
    public static final String RIGHT_FILENAME_FLAG = "r";

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String filePath = ((FileSplit)context.getInputSplit()).getPath().toString();
        String fileFlag = null;
        String joinKey = null;
        String joinValue = null;

        if(filePath.contains(LEFT_FILENAME)){
            fileFlag = LEFT_FILENAME_FLAG;
            joinKey = value.toString().split("\t")[1];
            joinValue = value.toString().split("\t")[0];
        }else if(filePath.contains(RIGHT_FILENAME)){
            fileFlag = RIGHT_FILENAME_FLAG;
            joinKey = value.toString().split("\t")[0];
            joinValue = value.toString().split("\t")[1];
        }

        context.write(new Text(joinKey), new Text(joinValue + "\t" + fileFlag));
    }
}

class JoinReducer extends Reducer<Text, Text, Text, Text> {

    public static final String LEFT_FILENAME = "student_info.txt";
    public static final String RIGHT_FILENAME = "student_class_info.txt";
    public static final String LEFT_FILENAME_FLAG = "l";
    public static final String RIGHT_FILENAME_FLAG = "r";

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        Iterator<Text> iterator = values.iterator();

        List<String> studentClassName = new ArrayList<String>();
        String studentName = "";

        while(iterator.hasNext()){
            String[] infos = iterator.next().toString().split("\t");
            if(infos[1].equals(LEFT_FILENAME_FLAG)){
                studentName = infos[0];
            }else if(infos[1].equals(RIGHT_FILENAME_FLAG)){
                studentClassName.add(infos[0]);
            }
        }

        for(int i = 0; i < studentClassName.size();i ++){
            context.write(new Text(studentName), new Text(studentClassName.get(i)));
        }
    }
}
➜  student git:(master) ✗ hadoop dfs -cat /student/output/part-r-00000
DEPRECATED: Use of this script to execute hdfs command is deprecated.
Instead use the hdfs command for it.

18/08/05 15:33:42 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Jenny   Math
Jenny   Chinese
Hardy   Math
Hardy   Music
Bradley Physic
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值