package my.hadoop.hdfs.mapreduceJoin;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map; 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; /**
* 当商品表比较小只有几十个(比如小米手机),但是订单表比较大(一年卖几千万)此时
* 如果将每个产品用一个reduce处理时那就可能出现小米书包只有几万,数据,但是小米手机就有100万的数据,
* 出现负载不均衡,数据倾斜的情况。
* @author lq
*
*/
public class MapsideJoin { public static class FindFriendMapper extends
Mapper<LongWritable, Text, AllInfoBean, NullWritable> { FileSplit fileSplit = null;
String filename = null; Map<String,String> pdinfo = new HashMap<String,String>(); @Override
protected void setup(
Mapper<LongWritable, Text, AllInfoBean, NullWritable>.Context context)
throws IOException, InterruptedException {
//文件和程序已经在同一个路径(splist。xml。wc,)
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("product")));
String line = null;
while ((line = br.readLine())!=null){
String[] split = line.split(",");
pdinfo.put(split[0], split[1]);
}
// 关闭流
br.close();
}
AllInfoBean bean = new AllInfoBean();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// 获取文件名字的方法
// 判断用的是哪个文件
String[] cols = value.toString().split(",");
bean.setOderid(Integer.parseInt(cols[0]));
bean.setDate(cols[1]);
bean.setPid(cols[2]);
bean.setAmount(Integer.parseInt(cols[3]));
bean.setPname(pdinfo.get(cols[2])==null? "" : pdinfo.get(cols[2]));
bean.setPrice("");
bean.setCategory_id(""); context.write(bean, NullWritable.get());
}
} //不要reduce
/*public static class FindFriendReducer extends
Reducer<Text, AllInfoBean, AllInfoBean, NullWritable> { @Override
protected void reduce(Text Keyin, Iterable<AllInfoBean> values,
Context context) throws IOException, InterruptedException { for(AllInfoBean bean : values){
context.write(bean, NullWritable.get());
} }
}*/ public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException, URISyntaxException { Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(MapsideJoin.class); job.setMapperClass(FindFriendMapper.class);
//不指定reduce
//job.setReducerClass(FindFriendReducer.class);
//指定最终输出的数据kv类型 //job.setMapOutputKeyClass(Text.class);
//job.setMapOutputValueClass(AllInfoBean.class);
job.setNumReduceTasks(0);//设置不运行reduce
job.setOutputKeyClass(AllInfoBean.class);
job.setOutputValueClass(NullWritable.class);
//第三方jar包使用这个路径指定,本地和hdfs都可以
//job.addArchiveToClassPath(archive);
//job
job.addCacheFile(new URI("hdfs://mini2:9000/Rjoin/dat2/product"));//缓存其他节点 FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean res = job.waitForCompletion(true);
System.exit(res ? 0 :1);
} }

map端join的更多相关文章

  1. hadoop的压缩解压缩,reduce端join,map端join

    hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别 ...

  2. Hadoop_22_MapReduce map端join实现方式解决数据倾斜(DistributedCache)

    1.Map端Join解决数据倾斜   1.Mapreduce中会将map输出的kv对,按照相同key分组(调用getPartition),然后分发给不同的reducetask 2.Map输出结果的时候 ...

  3. 第2节 mapreduce深入学习:16、17、map端的join算法的实现

    map端的join算法,适用于小表join大表的时候,一次性把小表的数据全部装载到内存当中来: 代码: MapJoinMain: package cn.itcast.demo5.mapJoin; im ...

  4. hadoop 多表join:Map side join及Reduce side join范例

    最近在准备抽取数据的工作.有一个id集合200多M,要从另一个500GB的数据集合中抽取出所有id集合中包含的数据集.id数据集合中每一个行就是一个id的字符串(Reduce side join要在每 ...

  5. Hadoop.2.x_高级应用_二次排序及MapReduce端join

    一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...

  6. Hadoop的Map侧join

    写了关于Hadoop下载地址的Map侧join 和Reduce的join,今天我们就来在看另外一种比较中立的Join. SemiJoin,一般称为半链接,其原理是在Map侧过滤掉了一些不需要join的 ...

  7. Hadoop on Mac with IntelliJ IDEA - 10 陆喜恒. Hadoop实战(第2版)6.4.1(Shuffle和排序)Map端 内容整理

    下午对着源码看陆喜恒. Hadoop实战(第2版)6.4.1  (Shuffle和排序)Map端,发现与Hadoop 1.2.1的源码有些出入.下面作个简单的记录,方便起见,引用自书本的语句都用斜体表 ...

  8. MapReduce在Map端的Combiner和在Reduce端的Partitioner

    1.Map端的Combiner. 通过单词计数WordCountApp.java的例子,如何在Map端设置Combiner... 只附录部分代码: /** * 以文本 * hello you * he ...

  9. Hadoop2.4.1 MapReduce通过Map端shuffle(Combiner)完成数据去重

    package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...

随机推荐

  1. Binary Tree Vertical Order Traversal -- LeetCode

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  2. luogu P2828 Switching on the Lights(开关灯)

    题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.(1<n<100) 然而bessie十分怕黑,他想计算可以把 ...

  3. 玩转Nuget服务器搭建(一)

    背景                                                                                      公司项目是分模块进行架构 ...

  4. intellij idea 分屏设置 与快捷键

    1.找到分屏功能 File -> setting -> keymap,搜索(注意大小写):   Split Vertically 水平分屏   Split Horizontally 垂直分 ...

  5. 使用 SVG 来实现波浪 (wave) 动画效果

    如下图所示的波浪动画效果,实现方法有很多,比如CSS或者是js等方法都可以实现.不过,要是使用SVG来实现的,我觉得比其它两种方法都要简单.这篇文章就来讲讲使用SVG来实现类似这样的波浪动画效果是多么 ...

  6. CM3大礼包

  7. [置顶] docker web-GUI DockerUI和Shipyard对比

    DockerUI和Shipyard对比 相似 基于Docker API,提供等同Docker命令行的大部分功能,支持container管理,image管理. web页面查看和管理容器和镜像,均能批量管 ...

  8. sed命令使用示例

    sed -i '/mirrorlist/d' CentOS-Base-163.repo 把有mirrorlist的行删除sed -i '/\[addons\]/,/^$/d' CentOS-Base- ...

  9. git只拉取github部分代码的方法

    需求:github某个项目所有代码太大,有600+M,甚至更大:只需要拉取部分代码,一是可以降低网络消耗,二是可以降低磁盘占用 分析了下空间占用情况:发现每个项目下的.git/objects/pack ...

  10. NeatBean下ssh 私钥格式问题

    1. SecureCRT 生成的private key 的格式是其私有的格式, 2. 标准格式为 openssl 格式