mapreduce join操作
数据准备
- create table if not exists m_ys_lab_jointest_a (
- id bigint,
- name string
- )
- row format delimited
- fields terminated by '9'
- lines terminated by '10'
- stored as textfile;
| id name 1 北京 2 天津 3 河北 4 山西 5 内蒙古 6 辽宁 7 吉林 8 黑龙江 |
- create table if not exists m_ys_lab_jointest_b (
- id bigint,
- statyear bigint,
- num bigint
- )
- row format delimited
- fields terminated by '9'
- lines terminated by '10'
- stored as textfile;
数据:
| id statyear num 1 2010 1962 1 2011 2019 2 2010 1299 2 2011 1355 4 2010 3574 4 2011 3593 9 2010 2303 9 2011 2347 |
我们的目的是,以id为key做join操作,得到以下表:
| id name statyear num 1 北京 2011 2019 1 北京 2010 1962 2 天津 2011 1355 2 天津 2010 1299 4 山西 2011 3593 4 山西 2010 3574 |
计算模型
代码
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Vector;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.io.Writable;
- import org.apache.hadoop.mapred.FileSplit;
- import org.apache.hadoop.mapred.JobConf;
- import org.apache.hadoop.mapred.MapReduceBase;
- import org.apache.hadoop.mapred.Mapper;
- import org.apache.hadoop.mapred.OutputCollector;
- import org.apache.hadoop.mapred.RecordWriter;
- import org.apache.hadoop.mapred.Reducer;
- import org.apache.hadoop.mapred.Reporter;
- /**
- * MapReduce实现Join操作
- */
- public class MapRedJoin {
- public static final String DELIMITER = "\u0009"; // 字段分隔符
- // map过程
- public static class MapClass extends MapReduceBase implements
- Mapper<LongWritable, Text, Text, Text> {
- public void configure(JobConf job) {
- super.configure(job);
- }
- public void map(LongWritable key, Text value, OutputCollector<Text, Text> output,
- Reporter reporter) throws IOException, ClassCastException {
- // 获取输入文件的全路径和名称
- String filePath = ((FileSplit)reporter.getInputSplit()).getPath().toString();
- // 获取记录字符串
- String line = value.toString();
- // 抛弃空记录
- if (line == null || line.equals("")) return;
- // 处理来自表A的记录
- if (filePath.contains("m_ys_lab_jointest_a")) {
- String[] values = line.split(DELIMITER); // 按分隔符分割出字段
- if (values.length < 2) return;
- String id = values[0]; // id
- String name = values[1]; // name
- output.collect(new Text(id), new Text("a#"+name));
- }
- // 处理来自表B的记录
- else if (filePath.contains("m_ys_lab_jointest_b")) {
- String[] values = line.split(DELIMITER); // 按分隔符分割出字段
- if (values.length < 3) return;
- String id = values[0]; // id
- String statyear = values[1]; // statyear
- String num = values[2]; //num
- output.collect(new Text(id), new Text("b#"+statyear+DELIMITER+num));
- }
- }
- }
- // reduce过程
- public static class Reduce extends MapReduceBase
- implements Reducer<Text, Text, Text, Text> {
- public void reduce(Text key, Iterator<Text> values,
- OutputCollector<Text, Text> output, Reporter reporter)
- throws IOException {
- Vector<String> vecA = new Vector<String>(); // 存放来自表A的值
- Vector<String> vecB = new Vector<String>(); // 存放来自表B的值
- while (values.hasNext()) {
- String value = values.next().toString();
- if (value.startsWith("a#")) {
- vecA.add(value.substring(2));
- } else if (value.startsWith("b#")) {
- vecB.add(value.substring(2));
- }
- }
- int sizeA = vecA.size();
- int sizeB = vecB.size();
- // 遍历两个向量
- int i, j;
- for (i = 0; i < sizeA; i ++) {
- for (j = 0; j < sizeB; j ++) {
- output.collect(key, new Text(vecA.get(i) + DELIMITER +vecB.get(j)));
- }
- }
- }
- }
- protected void configJob(JobConf conf) {
- conf.setMapOutputKeyClass(Text.class);
- conf.setMapOutputValueClass(Text.class);
- conf.setOutputKeyClass(Text.class);
- conf.setOutputValueClass(Text.class);
- conf.setOutputFormat(ReportOutFormat.class);
- }
- }
技术细节
mapreduce join操作的更多相关文章
- 使用MapReduce实现join操作
在关系型数据库中,要实现join操作是非常方便的,通过sql定义的join原语就可以实现.在hdfs存储的海量数据中,要实现join操作,可以通过HiveQL很方便地实现.不过HiveQL也是转化成 ...
- MapReduce 实现数据join操作
前段时间有一个业务需求,要在外网商品(TOPB2C)信息中加入 联营自营 识别的字段.但存在的一个问题是,商品信息 和 自营联营标示数据是 两份数据:商品信息较大,是存放在hbase中.他们之前唯一的 ...
- Hadoop基础-MapReduce的Join操作
Hadoop基础-MapReduce的Join操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.连接操作Map端Join(适合处理小表+大表的情况) no001 no002 ...
- 0 MapReduce实现Reduce Side Join操作
一.准备两张表以及对应的数据 (1)m_ys_lab_jointest_a(以下简称表A) 建表语句: create table if not exists m_ys_lab_jointest_a ( ...
- 案例-使用MapReduce实现join操作
哈喽-各位小伙伴们中秋快乐,好久没更新新的文章啦,今天分享如何使用mapreduce进行join操作. 在离线计算中,我们常常不只是会对单一一个文件进行操作,进行需要进行两个或多个文件关联出更多数据, ...
- Mapreduce中的join操作
一.背景 MapReduce提供了表连接操作其中包括Map端join.Reduce端join还有半连接,现在我们要讨论的是Map端join,Map端join是指数据到达map处理函数之前进行合并的,效 ...
- hive:join操作
hive的多表连接,都会转换成多个MR job,每一个MR job在hive中均称为Join阶段.按照join程序最后一个表应该尽量是大表,因为join前一阶段生成的数据会存在于Reducer 的bu ...
- mapreduce join
MapReduce Join 对两份数据data1和data2进行关键词连接是一个很通用的问题,如果数据量比较小,可以在内存中完成连接. 如果数据量比较大,在内存进行连接操会发生OOM.mapredu ...
- SQL join中级篇--hive中 mapreduce join方法分析
1. 概述. 本文主要介绍了mapreduce框架上如何实现两表JOIN. 2. 常见的join方法介绍 假设要进行join的数据分别来自File1和File2. 2.1 reduce side jo ...
随机推荐
- 3列滚动抽奖 jquery.slotmachine
效果图: 需引入js文件: <script src="js/jquery-3.2.0.js"></script> <script src=" ...
- 锋利的Jquery(Table,Checkbox)
1.Table奇数偶数行 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- [洛谷P3672]小清新签到题
题目描述 题目还是简单一点好. 给定自然数n.k.x,你要求出第k小的长度为n的逆序对对数为x的1~n的排列a1,a2...an,然后用仙人图上在线分支定界启发式带花树上下界最小费用流解决问题,保证存 ...
- 洛谷P3834【模板】可持久化线段树 1(主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- 试做Chrome插件——whatweb的chrome插件(从老博客转)
引子 最近一个月每天早上在学Javascript,刚学完基础语法和一点点jQuery,今天忍不住写个Chrome玩玩看看自己对JavaScript的掌握怎么样了. 目标 考虑了一下,打算做个小东西,但 ...
- 新安装一个eclipse,导入一个web项目,右键点击项目选择Properties,找不到project facets和Server选项。
解决方式: 1.点击:eclipse导航栏中点击Help->Install New Software 2.点击Add添加 3在弹出框中填写以下信息 name:keep(名字随便取) locati ...
- leetccode-130-被围绕的区域
题目描述: 方法一:dfs class Solution: def solve(self, board: List[List[str]]) -> None: """ ...
- 2016.10.7初中部上午NOIP普及组比赛总结
2016.10.7初中部上午NOIP普及组比赛总结 这次的题还可以,重新入了比赛的前十. 进度: 比赛:90+10+70+30=200 改题:AC+AC+AC+AC=AK 找试场 这题很简单,但是被欺 ...
- centos7 搭建 php7 + nginx (1)
前言 曾今,写过几篇类似的文章,但是发现几个月后,自己回头再看的时候,有种支离破碎的感觉.自己写的并不全,所以今天打算写一篇比较详细的文档.争取下次环境的减的时候,只需要拷贝复制粘贴即可完成环境搭建. ...
- 使用phpStudy自带的mysql-front学习建库建表以及基本的mysql语句
1.鼠标左键phpStudy图标,点击mysql管理器,如下图 2.选择Mysql-Front,选择localhost进入,可以看到本地建立的数据库.然后新建一个数据库,如下图: 3.在新建的数据库上 ...