美文网首页
MapReduce单表连接

MapReduce单表连接

作者: 月巴巴 | 来源:发表于2017-08-16 09:40 被阅读0次

例如给出表child-parent表,要求输出grandchildren-grandparent表
输入
Tom Lucy
Tom Jack
Jone Lucy
Jone Jack
Lucy Mary
Lucy Ben
Jack Alice
Jack Jesse

输出
Tom Alice
Tom Jesse
Jone Alice
Jone Jesse
Tom Mary
Tom Ben
Jone Mary
Jone Ben

用输入的单表构建两个表,即child-parent表和parent-child表,将两个表自然连接,就可以得到结果。程序的关键在于在map中构建出左右两表。

代码

package com.hy.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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 org.apache.log4j.BasicConfigurator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SingleJoin {

    public static class TokenizerMapper
            extends Mapper<Object, Text, Text, Text> {
        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            String[] lines=value.toString().split(" ");
            String parentName=lines[1];
            String childName=lines[0];
            context.write(new Text(parentName),new Text(""+1+" "+childName)); //右表
            context.write(new Text(childName),new Text(""+2+" "+parentName)); //左表
        }
    }

    public static class IntSumReducer
            extends Reducer<Text, Text, Text, Text> {
        public void reduce(Text key, Iterable<Text> values,
                           Context context
        ) throws IOException, InterruptedException {
            List<String> grandchild=new ArrayList<String>();
            List<String> grandparent=new ArrayList<String>();
            for (Text val : values) {
                String[] tmp=val.toString().split(" ");
                if(tmp[0].equals("1"))
                grandchild.add(tmp[1]);
                else
                    grandparent.add(tmp[1]);
            }
            if(grandchild.size()!=0&&grandparent.size()!=0){
                for (String gc:grandchild){
                    for(String gp:grandparent){
                        context.write(new Text(gc),new Text(gp));
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        BasicConfigurator.configure();
        Configuration conf = new Configuration();
        conf.set("mapreduce.cluster.local.dir", "/Users/hy/hadoop/var");
        Job job = Job.getInstance(conf, "Single join");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        FileSystem fs = FileSystem.get(conf);
        //如果输出文件夹存在,则删除
        if (fs.exists(new Path(args[1]))) {
            fs.delete(new Path(args[1]),true);
        }
        if(!job.waitForCompletion(true))
            System.exit(1);

        //输出结果
        BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(new Path(args[1]+"/part-r-00000"))));
        try {
            String line;
            line=br.readLine();
            while (line != null){
                System.out.println(line);
                line = br.readLine();
            }
        } finally {
            br.close();
        }
    }
}

相关文章

  • MapReduce单表连接

    例如给出表child-parent表,要求输出grandchildren-grandparent表输入:Tom L...

  • Hadoop6- MapReduce join

    Hadoop MapReduce join MapReduce提供了表连接操作其中包括Map端join、Reduc...

  • hive多表查询

    多表连接时,hive总是按照从左到右的顺序执行的,当3个表连接时,如表a b c,a b的输出mapreduce ...

  • MapReduce实现‘单表关联’

    "单表关联"要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘 样例输入:(孩子-父母)注意,单词...

  • MapReduce join

    一、说明 MapReduce提供的表连接操作包括:Map端join、Reduce端join、semi join(半...

  • 2018-05-30

    1 . jion 的时候把大表放后面 2. join on 使用相同连接键位 ,避免产生多个mapreduce 3...

  • MapReduce实现‘多表关联’

    多表关联和单表关联相似,都类似于数据库中的自然连接。相比单表关联,多表关联的左右表和连接列更加清楚。所以可以采用和...

  • [转载]Spark的Shuffle机制

    MapReduce中的Shuffle 在MapReduce框架中,shuffle是连接Map和Reduce之间的桥...

  • 【Spark】Spark的Shuffle机制

    MapReduce中的Shuffle 在MapReduce框架中,shuffle是连接Map和Reduce之间的桥...

  • MyBatis多表连接查询

    使用MyBatis进行单表查询十分简单,多表连接查询相比单表查询操作稍微复杂,不过相较于使用JDBC方式简单得多:...

网友评论

      本文标题:MapReduce单表连接

      本文链接:https://www.haomeiwen.com/subject/ghnbrxtx.html