map端join
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的更多相关文章
- hadoop的压缩解压缩,reduce端join,map端join
hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别 ...
- Hadoop_22_MapReduce map端join实现方式解决数据倾斜(DistributedCache)
1.Map端Join解决数据倾斜 1.Mapreduce中会将map输出的kv对,按照相同key分组(调用getPartition),然后分发给不同的reducetask 2.Map输出结果的时候 ...
- 第2节 mapreduce深入学习:16、17、map端的join算法的实现
map端的join算法,适用于小表join大表的时候,一次性把小表的数据全部装载到内存当中来: 代码: MapJoinMain: package cn.itcast.demo5.mapJoin; im ...
- hadoop 多表join:Map side join及Reduce side join范例
最近在准备抽取数据的工作.有一个id集合200多M,要从另一个500GB的数据集合中抽取出所有id集合中包含的数据集.id数据集合中每一个行就是一个id的字符串(Reduce side join要在每 ...
- Hadoop.2.x_高级应用_二次排序及MapReduce端join
一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...
- Hadoop的Map侧join
写了关于Hadoop下载地址的Map侧join 和Reduce的join,今天我们就来在看另外一种比较中立的Join. SemiJoin,一般称为半链接,其原理是在Map侧过滤掉了一些不需要join的 ...
- 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的源码有些出入.下面作个简单的记录,方便起见,引用自书本的语句都用斜体表 ...
- MapReduce在Map端的Combiner和在Reduce端的Partitioner
1.Map端的Combiner. 通过单词计数WordCountApp.java的例子,如何在Map端设置Combiner... 只附录部分代码: /** * 以文本 * hello you * he ...
- Hadoop2.4.1 MapReduce通过Map端shuffle(Combiner)完成数据去重
package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...
随机推荐
- 洛谷——P1706 全排列问题
P1706 全排列问题 题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入输出格式 输入格式: n(1≤n≤9) 输出格式: 由1-n组成 ...
- 洛谷——P1469 找筷子
P1469 找筷子 题目描述 经过一段时间的紧张筹备,电脑小组的“RP餐厅”终于开业了,这天,经理LXC接到了一个定餐大单,可把大家乐坏了!员工们齐心协力按要求准备好了套餐正准备派送时,突然碰到一个棘 ...
- NMAP输出结果中CPE的含义
NMAP输出结果中CPE的含义 CPE全称是Common Platform Enumeration,意思是通用平台枚举项.它是NMAP对识别出来的软件.操作系统和硬件的一种命名方式.它的格式如下: ...
- SQL Server 2008 R2 Build List
By Steve Jones, 2014/09/30 (first published: 2010/05/25) This is a list of the builds for SQL Server ...
- A Beginner’s Guide to the OUTPUT Clause in SQL Server
原文 A Beginner’s Guide to the OUTPUT Clause in SQL Server T-SQL supports the OUTPUT clause after the ...
- AutoCAD中导入图片
导入图片涉及到两个关键的问题:如何将图片放置到指定的位置或范围内:如何修改图片的路径类型,如相对路径.绝对路径. 本文以AutoCAD 2018位演示截图来源. 1 将图片放置到指定的位置或范围内 ( ...
- iOS教程:如何使用Core Data – 预加载和引入数据
这是接着上一次<iOS教程:Core Data数据持久性存储基础教程>的后续教程,程序也会使用上一次制作完成的. 再上一个教程中,我们只做了一个数据模型,之后我们使用这个数据模型中的数据创 ...
- react.js Warning: Failed form propType: You provided a value prop to a form field without an onChange handler. This will render a read-only field.
错误信息: eact.js:20483 Warning: Failed form propType: You provided a value prop to a form field without ...
- Elasticsearch 索引实例
1.简述 ElasticSearch包含了一系列的感念,比如索引(indexing).搜索(search)以及聚合(aggregations),现在我们主要介绍indexing. 在Elasticse ...
- Spring MVC 解读——<mvc:annotation-driven/>
Spring MVC 解读——<mvc:annotation-driven/> 一.AnnotationDrivenBeanDefinitionParser 通常如果我们希望通过注解的方式 ...