Hadoop入门(十七)Mapreduce的多表关联程序

多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息

1 实例描述

输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列;另一个代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名和地址名的对应关系,输出"工厂名——地址名"表

样例输入如下所示:
1)factory.txt

factoryname        addressed 
Beijing Red Star        1
Shenzhen Thunder        3
Guangzhou Honda        2
Beijing Rising        1
Guangzhou Development Bank        2
Tencent        3
Back of Beijing        1

2)address.txt

addressID        addressname 
1        Beijing
2        Guangzhou
3        Shenzhen
4        Xian

期望输出:

factoryname                                        addressname 
Back of Beijing                          Beijing
Beijing Red Star                        Beijing
Beijing Rising                          Beijing
Guangzhou Development Bank          Guangzhou
Guangzhou Honda                    Guangzhou
Shenzhen Thunder                    Shenzhen
Tencent                            Shenzhen

2 问题分析

多表关联和单表关联相似,都类似于数据库中的自然连接。相比单表关联,多表关联的左右表和连接列更加清楚。所以可以采用和单表关联的相同的处理方式,map识别出输入的行属于哪个表之后,对其进行分割,将连接的列值保存在key中,另一列和左右表标识保存在value中,然后输出。reduce拿到连接结果之后,解析value内容,根据标志将左右表内容分开存放,然后求笛卡尔积,最后直接输出。

 

3.关键代码

package com.mk.mapreduce;import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
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;import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;public class JoinOther {public static class JoinOtherMapper extends Mapper<LongWritable, Text, Text, TableInfo> {@Overrideprotected void setup(Context context) throws IOException, InterruptedException {}@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {if (StringUtils.isBlank(value.toString())) {System.out.println("空白行");return;}String[] values = value.toString().split("\\s{2,}");if (values.length < 2 || values[0].equals("factoryname") || values[0].equals("addressID")) {System.out.println("长度不够的行:" + value.toString());return;}FileSplit fileInputSplit = (FileSplit) context.getInputSplit();if(fileInputSplit.getPath().toString().endsWith("/factory.txt")) {context.write(new Text(values[1]), new TableInfo(TableInfo.FACTORY, values[0]));}else {context.write(new Text(values[0]), new TableInfo(TableInfo.ADDRESS, values[1]));}}}public static class JoinOtherReducer extends Reducer<Text, TableInfo, Text, Text> {@Overrideprotected void setup(Context context) throws IOException, InterruptedException {context.write(new Text("factoryname"), new Text("addressname"));}@Overrideprotected void reduce(Text key, Iterable<TableInfo> values, Context context) throws IOException, InterruptedException {List<String> addresses = new LinkedList<>();List<String> factories = new LinkedList<>();for (TableInfo v : values) {if (v.getTable() == TableInfo.ADDRESS) {addresses.add(v.value.toString());} else {factories.add(v.value.toString());}}if (!addresses.isEmpty() && !factories.isEmpty()) {for (String factory :factories)for (String address : addresses)context.write(new Text(factory), new Text(address));}}}public static class TableInfo implements WritableComparable<TableInfo> {public static final int FACTORY = 1;public static final int ADDRESS = 2;private int table;private Text value;public TableInfo() {}public TableInfo(int table, String value) {this.table = table;this.value = new Text(value);}public int getTable() {return table;}public void setTable(int table) {this.table = table;}public void setValue(Text value) {this.value = value;}@Overridepublic int compareTo(TableInfo o) {int c = table - o.table;if (c != 0)return c;return value.compareTo(o.value);}@Overridepublic void write(DataOutput out) throws IOException {out.writeInt(table);this.value.write(out);}@Overridepublic void readFields(DataInput in) throws IOException {this.table = in.readInt();if (this.value == null)this.value = new Text();this.value.readFields(in);}@Overridepublic String toString() {return "TableInfo{" +"table=\'" + table +"\', value=\'" + value +"\'}";}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {String uri = "hdfs://192.168.150.128:9000";String input = "/joinOther/input";String output = "/joinOther/output";Configuration conf = new Configuration();if (System.getProperty("os.name").toLowerCase().contains("win"))conf.set("mapreduce.app-submission.cross-platform", "true");FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);Path path = new Path(output);fileSystem.delete(path, true);Job job = new Job(conf, "JoinOther");job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");job.setJarByClass(JoinOther.class);job.setMapperClass(JoinOtherMapper.class);job.setReducerClass(JoinOtherReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(TableInfo.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileInputFormat.addInputPaths(job, uri + input);FileOutputFormat.setOutputPath(job, new Path(uri + output));boolean ret = job.waitForCompletion(true);System.out.println(job.getJobName() + "-----" + ret);}
}


  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322983.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jzoj4209-已经没有什么好害怕的了【差分】

正题 题目大意 ansians_iansi​表示包含字符iii的括号匹配子串个数 求∑i1n(ansi∗imod&ThinSpace;&ThinSpace;(1e97))\sum_{i1}^n(ans_i*i\mod (1e97))i1∑n​(ansi​∗imod(1e97)) 解题思路 计算出每个匹配括号的前一个括号位置和后一个括号位置。 一个差分数组 先…

使用Identity Server 4建立Authorization Server (4)

预备知识: 学习Identity Server 4的预备知识 第一部分: 使用Identity Server 4建立Authorization Server (1) 第二部分: 使用Identity Server 4建立Authorization Server (2) 第三部分: 使用Identity Server 4建立Authorization Server (3) 上一篇讲了使用OpenId Connect进行Au…

linux操作命令

uname -r 显示正在使用的内核版本、 docker exec -it mytomcat bash 进入tomcat界面 pwd 显示工作路径 ls 查看当前目录中的文件 docker docker run -d -p 8080:8080 tomcat 启动tomcat镜像 docker start id 编辑文件 touch 文件名 创建文件 mkdir dir1 创建一个叫做 ‘dir1…

Hadoop入门(十八)Mapreduce的倒排索引程序

一、简介 "倒排索引"是文档检索系统中最常用的数据结构&#xff0c;被广泛地应用于全文搜索引擎。它主要是用来存储某个单词&#xff08;或词组&#xff09;在一个文档或一组文档中的存储位置的映射&#xff0c;即提供了一种根据内容来查找文档的方式。由于不是根据…

.NET Core跨平台的奥秘[中篇]:复用之殇

在《.NET Core跨平台的奥秘[上篇]&#xff1a;历史的枷锁》中我们谈到&#xff1a;由于.NET是建立在CLI这一标准的规范之上&#xff0c;所以它天生就具有了“跨平台”的基因。在微软发布了第一个针对桌面和服务器平台的.NET Framework之后&#xff0c;它开始 “乐此不疲” 地对…

jozj4010-我才不是萝莉控呢【哈夫曼树】

正题 题目大意 从(n,1)(n,1)(n,1)到(1,1)(1,1)(1,1)&#xff0c;一个数组AAA&#xff0c;满足Ai≥Ai1A_i\geq A_i1Ai​≥Ai​1 每次有两个选择走到(x−1,y1)(x-1,y1)(x−1,y1)&#xff0c;或(x,⌊y/2⌋)(x,\lfloor y/2\rfloor)(x,⌊y/2⌋)。后者需要消耗∑ixnAi\sum_{ix}^nA_i…

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized

在application.yml中 spring:datasource:username: rootpassword:url: jdbc:mysql://127.0.0.1:3306/testdriver-class-name: com.mysql.cj.jdbc.Driver结果报错 java.sql.SQLException: The server time zone value ‘&#xfffd;й&#xfffd;&#xfffd;&#xfffd;׼ʱ…

Hadoop入门(十一)Mapreduce的InputFomrat各种子类

一、TextInputFormat extends FileInputFomrat<LongWritable,Text> 是默认读取文件的切分器&#xff0c;其内的LineRecordReader:用来读取每一行的内容&#xff0c; LineRecordReader:内的 nextKeyValue(){}中&#xff0c;key的赋值在&#xff1a; initialize()方法内&…

欢乐纪中某B组赛【2019.1.21】

前言 成功翻车 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC1414142017hzb2017hzb2017hzb8080803030300005050501414142017wyc2017wyc2017wyc8080800003030305050501414142017xxy2017xxy2017xxy8080803030300005050504444442017lw2…

极简版ASP.NET Core学习路径

拒绝承认这是一个七天速成教程&#xff0c;即使有这个效果&#xff0c;我也不愿意接受这个名字。嗯。 这个路径分为两块&#xff1a; 实践入门 理论延伸 有了ASP.NET以及C#的知识以及项目经验&#xff0c;我们几乎可以不再需要了解任何新的知识就开始操练&#xff0c;实践才是…

spring boot连接数据库

applicat.yml spring:datasource:username: rootpassword:url: jdbc:mysql://localhost:3306/test?useUnicodetrue&useJDBCCompliantTimezoneShifttrue&useLegacyDatetimeCodefalse&serverTimezoneUTCdriver-class-name: com.mysql.cj.jdbc.Drivertest文件夹下测…

依存句法分析的任务以及形式化定义

转载自 依存句法分析的任务以及形式化定义 依存句法分析的任务以及形式化定义 1、依存句法分析的形式化定义 在依存句法中&#xff0c;共同的基本假设是&#xff1a;句法结构本质上包含词和词对之间的关系。这种关系就是依存关系&#xff08;dependency relations&#xff…

jzoj3084-超级变变变【数学】

正题 题目大意 定义函数 f(x){x−1(x%21)x/2(x%20)f(x)\left\{\begin{matrix} &amp;x-1(x\%21)\\ &amp; x/2(x\%20) \end{matrix}\right.f(x){​x−1(x%21)x/2(x%20)​ 一次变化是将xf(x)xf(x)xf(x) 求A∼BA\sim BA∼B之间有多少个数可以变化到kkk 解题思路 其实就是…

使用Identity Server 4建立Authorization Server (5)

预备知识: 学习Identity Server 4的预备知识 第一部分: 使用Identity Server 4建立Authorization Server (1) 第二部分: 使用Identity Server 4建立Authorization Server (2) 第三部分: 使用Identity Server 4建立Authorization Server (3) 第四部分: 使用Identity Server 4建立…

idea如何安装lombok

https://github.com/mplushnikov/lombok-intellij-plugin/releases &#xff0c;Plugins -> Install plugin from disk… 选择下载的zip包安装&#xff0c;重启idea即可。 依赖包 <dependency><groupId>org.projectlombok</groupId><artifactId>lom…

好好说说Java中的常量池之Class常量池

转载自 好好说说Java中的常量池之Class常量池 在Java中&#xff0c;常量池的概念想必很多人都听说过。这也是面试中比较常考的题目之一。在Java有关的面试题中&#xff0c;一般习惯通过String的有关问题来考察面试者对于常量池的知识的理解&#xff0c;几道简单的String面试…

jzoj3085-图的计数【组合数,数论】

正题 题目大意 求有多少个m条边的有向图使得1到n的最短路长度为n-1 解题思路 首先长度为n−1n-1n−1那么就是1到n得先是一条链。在链上加m−n1m-n1m−n1条边且不能加如捷径边。 捷径边的条数为Cn−12C_{n-1}^2Cn−12​&#xff0c;然后可以加的边数就是n∗n−Cn−12n*n-C_{n-…

spring cloud+.net core搭建微服务架构:Api授权认证(六)

前言 这篇文章拖太久了&#xff0c;因为最近实在太忙了&#xff0c;加上这篇文章也非常长&#xff0c;所以花了不少时间&#xff0c;给大家说句抱歉。好&#xff0c;进入正题。目前的项目基本都是前后端分离了&#xff0c;前端分Web&#xff0c;Ios,Android。。。,后端也基本是…

如何用spring boot写一个注册页面

环境准备&#xff1a; java集成开发环境&#xff1a;IDEA 数据库&#xff1a;Mysql Maven 最好在安装有个navicat&#xff08;数据库可视化界面&#xff09; 安装好上述几个软件后 总结下&#xff1a;五步 1、创建新的工程 2、创建建applicatiom.yml 3、创建entity层 4、创建r…

Oracle入门(一)之入门级知识详解

转载自 Oracle入门级知识详解 一. Oracle基本介绍 1. 什么时候用Oracle数据库&#xff1f; SQL SERVER 号称百万级数据&#xff08;一个表的数据&#xff09;&#xff0c;但是其实做多20万条数据 超过20万条数据就用Oracle 2. Oracle的版本 Oracle8i/9i(internet)基于网络…