本文是对Hive中【内连接】的Java-API的实现,具体的HQL语句详见Hive查询Join

package join.map;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
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; public class JoinOn { public static void main(String[] args) throws Exception { //临时配置windows的环境变量
System.setProperty("hadoop.home.dir", "D:\\workspace\\hadoop-2.2.0"); Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(JoinOn.class); job.setMapperClass(JOMapper.class);
job.setReducerClass(JOReducer.class); job.setMapOutputKeyClass(VLongWritable.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true)? 0:1);
} public static class JOMapper extends Mapper<LongWritable, Text, VLongWritable, Text>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { //获取当前分片所对应的文件名(优化:放在setup()方法中)
String name = ((FileSplit)context.getInputSplit()).getPath().getName(); String[] splited = value.toString().split("\t"); if(name.endsWith("sales")){ //sales表(注意:第二个参数中name和value之间的分隔符不能和value中个字段分隔符一样)
//<key,value> --> <id, things+':'+name+'\t'+id>
context.write(new VLongWritable(Long.parseLong(splited[1])), new Text(name+":"+value.toString()));
}else if(name.endsWith("things")) {
//<key,value> --> <id, sales+'\t'+id+'\t'+name>
context.write(new VLongWritable(Long.parseLong(splited[0])), new Text(name+":"+value.toString()));
}
}
} public static class JOReducer extends Reducer<VLongWritable, Text, Text, Text>{
@Override
protected void reduce(VLongWritable key, Iterable<Text> v2s, Context context)
throws IOException, InterruptedException { //分别存储sales和things两表的name
List<String> sales=new ArrayList<String>();
List<String> things=new ArrayList<String>(); for(Text text : v2s){
String[] splited = text.toString().split(":"); //sales表中的数据
if(splited[0].endsWith("sales")){ //加入集合
sales.add(splited[1]);
}
//things表中数据
else if(splited[0].endsWith("things")){
things.add(splited[1]);
}
}
//笛卡尔积
if(sales.size()!=0 && things.size()!=0){
for(String sale : sales){
for(String thing : things){
context.write(new Text(sale), new Text(thing));
}
}
}
}
}
}

总结:

 1).程序中获取FileName应放置中setup()方法中,因为每个文件只需执行一次此方法

 2).Map输出的第二个参数中name和value之间的分隔符不能和value中个字段分隔符一样

MR案例:内连接代码实现的更多相关文章

  1. MR案例:外连接代码实现

    [外连接]是在[内连接]的基础上稍微修改即可.具体HQL语句详见Hive查询Join package join.map; import java.io.IOException; import java ...

  2. Ajax案例:三级联动查询员工的信息(三张表进行内连接)

    需求分析: 通过数据库连接池,可以查询到数据库中员工的各种信息,通过点击下拉框的方式实现三级联动,即:没有点击city下拉框,后面两个下拉框中没有值,这样,点击city下拉框,department下拉 ...

  3. 2_05_MSSQL课程_查询_where,group by,having的区别以及内连接、案例练习

    SQL查询顺序 select distinct/top ..._ (5) 投影 from_(1)找到表  where_ (2)过滤掉我们不需要的数据    group by_ (3)分组操作    h ...

  4. [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. 【SQL】SQL中笛卡尔积、内连接、外连接的数据演示

    SQL的查询语句中,常使用到内连接.外连接,以及连接的基础--笛卡尔积运算. 在简单的SQL中,也许我们还分辨清楚数据如何连接,一旦查询复杂了,脑子也犯浆糊了,迷迷糊糊的. 本文,简单以数据形式记录连 ...

  6. sql左连接,右连接,内连接

    1.sql查询时什么叫左连接和右连接    左连接和右连接都是外部连接,也就是区别于内部连接,它对不满足连接条件的行并不是象内部连接一样将数据完全过滤掉,而是保留一部分数据,行数不会减少.    左或 ...

  7. 【转】mysql数据库中实现内连接、左连接、右连接

    [转]mysql数据库中实现内连接.左连接.右连接 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把对应数据查出来 首先创建数据库中的表,数据库代码如下: /* Navicat MyS ...

  8. SQL连接方式(内连接,外连接,交叉连接)

    1.内连接.左连接.右连接.全连接介绍 內连接仅选出两张表中互相匹配的记录.因此,这会导致有时我们需要的记录没有包含进来.内部连接是两个表中都必须有连接字段的对应值的记录,数据才能检索出来.   左连 ...

  9. Linq入门演练---(1)基本用法-分组,排序,内连接

    这一节大家共同学习下LINQ的基本用法,主要包括LINQ的分组,排序,和内外连接. 1.分组 基本语法:  group element by key element 表示查询结果返回的元素,key表示 ...

随机推荐

  1. spring boot 加载jsp

    1.spring boot启动类继承SpringBootServletInitializer ,并且重写configure方法 package com.springapp.mvc;import jav ...

  2. Web端测试

    一.功能测试 1.链接测试      1)所有链接是否按指示的那样,链接正确?      2)所有链接是否存在?      3)保证Web应用系统上没有孤立的页面? 在线链接测试地址:http://v ...

  3. textarea文本输入区内实现换行

    在文本间输入 即可成功换行 <textarea class="mytextarea">1.第一行 2.第二行 </textarea>

  4. Buy a home in AU

    澳洲留学生买房的几点注意事项: 1. 新房.楼花.或者买地建房,完全不受限制,国民待遇,是政府鼓励的. 2. 留学生签证剩余超12个月,可以购买二手房,但是只能自住不能出租. 3. 银行可以提供50% ...

  5. python基础-第十篇-10.1HTML基础

    htyper text markup language 即超文本标记语言 超文本:就是指页面内可以包含图片.链接,甚至音乐,程序等非文字元素 标记语言:标记(标签)构成的语言 网页==HTML文档,由 ...

  6. SpringBoot安装和创建简单的Web应用

    SpringBoot安装 方式一: Eclipese->Help->Eclipse Marketplace ->Finde STS -> Install 注意:安装过程中挺慢, ...

  7. R语言之多重共线性的判别以及解决方法

    多重共线性(Multicollinearity)是指线性回归模型中的解释变量之间由于存在精确相关关系或高度相关关系而使模型估计失真或难以估计准确.   1.可以计算X矩阵的秩qr(X)$rank,如果 ...

  8. PAT 1115 Counting Nodes in a BST[构建BST]

    1115 Counting Nodes in a BST(30 分) A Binary Search Tree (BST) is recursively defined as a binary tre ...

  9. Java中树和树的几种常规遍历方法

    其中包含有先序遍历.中序遍历.后序遍历以及广度优先遍历四种遍历树的方法: package com.ietree.basic.datastructure.tree.binarytree; import ...

  10. 创建发布Webservice以及wsimport工具

    一. 通过wsimport生成本地代理调用WebService 1.推荐的访问服务方式 WebService已纳入w3c规范,其他的平台都支持该规范 :J2EE\Php\.NET都支持wsimport ...