Hadoop 多表 join:map side join 范例

xt value, Context context)
076                throws IOException, InterruptedException {
077 
078            kv = value.toString().split("\t");
079            // map join: 在map阶段过滤掉不需要的数据
080            if (userMap.containsKey(kv[0]) && sexMap.containsKey(kv[1])) {
081                oKey.set(userMap.get(kv[0]) + "\t" + sexMap.get(kv[1]));
082                oValue.set("1");
083                context.write(oKey, oValue);
084            }
085        }
086 
087    }
088 
089    public static class Reduce extends Reducer<Text, Text, Text, Text> {
090 
091        private Text oValue = new Text();
092 
093        public void reduce(Text key, Iterable<Text> values, Context context)
094                throws IOException, InterruptedException {
095            int sumCount = 0;
096 
097            for (Text val : values) {
098                sumCount += Integer.parseInt(val.toString());
099            }
100            oValue.set(String.valueOf(sumCount));
101            context.write(key, oValue);
102        }
103 
104    }
105 
106    public int run(String[] args) throws Exception {
107        Job job = new Job(getConf(), "MultiTableJoin");
108 
109        job.setJobName("MultiTableJoin");
110        job.setJarByClass(MultiTableJoin.class);
111        job.setMapperClass(MapClass.class);
112        job.setReducerClass(Reduce.class);
113 
114        job.setInputFormatClass(TextInputFormat.class);
115        job.setOutputFormatClass(TextOutputFormat.class);
116 
117        job.setOutputKeyClass(Text.class);
118        job.setOutputValueClass(Text.class);
119 
120        String[] otherArgs = new GenericOptionsParser(job.getConfiguration(),
121                args).getRemainingArgs();
122 
123        // 我们把第1、2个参数的地址作为要缓存的文件路径
124        DistributedCache.addCacheFile(new Path(otherArgs[1]).toUri(), job
125                .getConfiguration());
126        DistributedCache.addCacheFile(new Path(otherArgs[2]).toUri(), job
127                .getConfiguration());
128 
129        FileInputFormat.addInputPath(job, new Path(otherArgs[3]));
130        FileOutputFormat.setOutputPath(job, new Path(otherArgs[4]));
131 
132        return job.waitForCompletion(true) ? 0 : 1;
133    }
134 
135    public static void main(String[] args) throws Exception {
136        int res = ToolRunner.run(new Configuration(), new MultiTableJoin(),
137                args);
138        System.exit(res);
139    }
140 
141}

运行命令:

1hadoop jar MultiTableJoin.jar MultiTableJoin /test/decli/sex /test/decli/user /test/decli/login /test/decli/output

4、结果:

运行结果:

root@master 192.168.120.236 02:47:18 ~/test/table >
hadoop fs -cat /test/decli/output/*|column -t
cat: File does not exist: /test/decli/output/_logs
张三  男  4
李四  男  2
王五  女  2
赵六  女  2
root@master 192.168.120.236 02:47:26 ~/test/table >

TIPS:  转自 http://my.oschina.net/leejun2005/blog/111963

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值