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 ...
随机推荐
- shell常用命令及正则辅助日志分析统计
https://www.cnblogs.com/wj033/p/3451618.html 正则日志分析统计 3 grep 'onerror' v3-0621.log | egrep -v '(\d ...
- Jodd - Java界的瑞士军刀轻量级工具包!
Jodd介绍 Jodd是对于Java开发更便捷的开源迷你框架,包含工具类.实用功能的集合,总包体积不到1.7M. Jodd构建于通用场景使开发变得简单,但Jodd并不简单!它能让你把事情做得更好,实现 ...
- Pycharm VS VS Code(个人使用感受)
Pycharm IDE (community enough!) 简单介绍:Pycharn的确是我刚开始学习python时,除了Visual Studio之外,上手的第二个IDE,最初是因其好看的界面, ...
- 大型SQL文件导入mysql方案
一. 场景 现有俩个体积较大的单表sql文件,一个为8G,一个为4G,要在一天内完整导入到阿里云的mysql中,需要同时蛮子时间和空间这俩种要求. 二. 思路 搜索了网上一堆的方案,总结了如下几个: ...
- centos6 nginx安装好以后,添加拓展ssl
前言 安装nginx的时候,只是执行最简单的安装,--user=nobody --group=nobody --prefix=/usr/local/nginx_1.8.1,没有安装http_ssl_m ...
- 操作系统命令工具Util
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- 关于不同数据库的连接配置(MySql和Oracle)
mysql: driverClass=com.mysql.jdbc.Driver #在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8,插入数据库防止中文乱码 ur ...
- jmeter参数化之用户参数
1. 用badboby进行录制,录制完成后保存,用JMeter格式进行保存,如:登陆.jmx 2. 在jmeter中打开保存的文件登陆.jmx. 3.在step1上右击-添加-前置处理 ...
- VC控件DateTimePicker使用方法
出自http://www.cnblogs.com/52yixin/articles/2111299.html 使用DateTimePicker控件一般是获 取其时间替代手工输入带来的不便,而DateT ...
- MFC安装与部署(程序打包)
(发现csdn传照片实在是太麻烦, 不能够直接拖拽进来;所以我直接使用云笔记生成一张图片 直接完成!) (懒癌晚期-)