Hadoop表连接

首先介绍一下我的作业要求和Hadoop开发环境:

  • 作业目的:实现两表连接。
  • 开发环境:IntelliJ IDEA
  • JAVA环境:JDK 9
  • Hadoop:2.8.1
  • 依赖管理工具:Maven

项目github地址

作业描述:将Perons表和Order表根据id连接起来,类似于数据库中的join操作。Persons和Order表结构如下图:

这里写图片描述

实际操作过程如下图:

这里写图片描述

代码实现如下:

pom.xml配置如下:

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>```

JoinTable.java文件如下:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
import java.util.Iterator;

public class JoinTable {


    public static class MapTable extends Mapper<Object, Text, Text, Text>{


        private Text key = new Text();
        private Text value = new Text();
        private String[] keyValue = null;

        public void map(Object key, Text value, Mapper.Context context) throws IOException, InterruptedException {

            String line = value.toString();

            keyValue = line.split(",",2);

            //以用户id为key
            this.key.set(keyValue[0]);
            this.value.set(keyValue[1]);

            context.write(this.key,this.value);

        }
    }

    public static class ReduceTable extends Reducer<Text, Text, Text, Text> {

        private Text value = new Text();

        public void reduce(Text key, Iterable<Text> values,
                           Context context
        ) throws IOException, InterruptedException {


            String valueStr ="";
            String[] save = new String[2];

            int i=0;
            int j=2;

            Iterator its =values.iterator();

            while(its.hasNext())
            {

                save[i++] = its.next().toString();
                j--;
            }

            String tableOne = save[0];
            String tableTwo = save[1];


            char flag = tableOne.charAt(1);

            System.out.println("++++" + flag);

            if(flag>='A'&&flag<='Z')
            {
                valueStr += tableOne;
                valueStr += " ,";

                String[] words = tableTwo.split(",");
                tableTwo = words[1] + "," + words[0];

                valueStr += tableTwo;
                valueStr += " ";
            }
            else
            {
                valueStr += tableTwo;
                valueStr += " ,";

                String[] words = tableOne.split(",");
                tableOne = words[1] + "," + words[0];

                valueStr += tableOne;
                valueStr += " ";
            }

            this.value.set(valueStr);

            context.write(key,this.value);


        }
    }

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

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Join table");

        job.setJarByClass(JoinTable.class);
        job.setMapperClass(MapTable.class);
        job.setReducerClass(ReduceTable.class);

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

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileInputFormat.addInputPath(job, new Path(args[1]));
        FileOutputFormat.setOutputPath(job,new Path(args[2]));

//        FileInputFormat.addInputPath(job, new Path("/Users/wangxiaofa/Desktop/Hadoop/src/input/Persons"));
//        FileInputFormat.addInputPath(job, new Path("/Users/wangxiaofa/Desktop/Hadoop/src/input/Order"));

//        FileOutputFormat.setOutputPath(job, new Path("/Users/wangxiaofa/Desktop/Hadoop/src/output"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值