hadoop 用MR實現join操作
在MR中,類似於join類的操作非常常見。在關係型資料庫中,join就是最強大的功能之一。在hive中,jion操作也十分常見。現在,本博主就手把手教會大家怎麼在MR中實現join操作。為了方便起見,本文就以left join為視角來實現。
1.資料準備
關於什麼是join,什麼是left join,本文就先不討論了。先準備如下資料:
cat employee.txt
jd,david
jd,mike
tb,mike
tb,lucifer
elong,xiaoming
elong,ali
tengxun,xiaoming
tengxun,lilei
xxx,aaa
cat salary.txt
jd,1600
tb,1800
elong,2000
tengxun,2200
然後將兩個檔案分別put到hdfs上面
hadoop fs -put employee.txt /tmp/wanglei/employee/employee.txt
hadoop fs -put salary.txt /tmp/wanglei/salary/salary.txt
我們想用employee左關聯salary。至此,資料準備已經完畢。
2.新建一個maven專案
新建一個maven專案,pom檔案如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>leilei.bit.edu</groupId>
<artifactId>testjoin</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>testjoin</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jarfile.name>testjoin</jarfile.name>
<jar.out.dir>jar</jar.out.dir>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.mrunit</groupId>
<artifactId>mrunit</artifactId>
<version>1.0.0</version>
<classifier>hadoop2</classifier>
</dependency>
<dependency>
<groupId>org.anarres.lzo</groupId>
<artifactId>lzo-hadoop</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${jarfile.name}</finalName>
<outputDirectory>${jar.out.dir}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
然後開始實現left join的邏輯:
package leilei.bit.edu.testjoin;
import java.io.IOException;
import java.util.Vector;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class LeftJoin extends Configured implements Tool{
public static final String DELIMITER = ",";
public static class LeftJoinMapper extends
Mapper<LongWritable,Text,Text,Text> {
protected void map(LongWritable key, Text value, Context context)
throws IOException,InterruptedException {
/*
* 拿到兩個不同檔案,區分出到底是哪個檔案,然後分別輸出
*/
String filepath = ((FileSplit)context.getInputSplit()).getPath().toString();
String line = value.toString();
if (line == null || line.equals("")) return;
if (filepath.indexOf("employee") != -1) {
String[] lines = line.split(DELIMITER);
if(lines.length < 2) return;
String company_id = lines[0];
String employee = lines[1];
context.write(new Text(company_id),new Text("a:"+employee));
}
else if(filepath.indexOf("salary") != -1) {
String[] lines = line.split(DELIMITER);
if(lines.length < 2) return;
String company_id = lines[0];
String salary = lines[1];
context.write(new Text(company_id), new Text("b:" + salary));
}
}
}
public static class LeftJoinReduce extends
Reducer<Text, Text, Text, Text> {
protected void reduce(Text key, Iterable<Text> values,
Context context) throws IOException, InterruptedException{
Vector<String> vecA = new Vector<String>();
Vector<String> vecB = new Vector<String>();
for(Text each_val:values) {
String each = each_val.toString();
if(each.startsWith("a:")) {
vecA.add(each.substring(2));
} else if(each.startsWith("b:")) {
vecB.add(each.substring(2));
}
}
for (int i = 0; i < vecA.size(); i++) {
/*
* 如果vecB為空的話,將A裡的輸出,B的位置補null。
*/
if (vecB.size() == 0) {
context.write(key, new Text(vecA.get(i) + DELIMITER + "null"));
} else {
for (int j = 0; j < vecB.size(); j++) {
context.write(key, new Text(vecA.get(i) + DELIMITER + vecB.get(j)));
}
}
}
}
}
public int run(String[] args) throws Exception {
Configuration conf = getConf();
GenericOptionsParser optionparser = new GenericOptionsParser(conf, args);
conf = optionparser.getConfiguration();
Job job = new Job(conf,"leftjoin");
job.setJarByClass(LeftJoin.class);
FileInputFormat.addInputPaths(job, conf.get("input_dir"));
Path out = new Path(conf.get("output_dir"));
FileOutputFormat.setOutputPath(job, out);
job.setNumReduceTasks(conf.getInt("reduce_num",2));
job.setMapperClass(LeftJoinMapper.class);
job.setReducerClass(LeftJoinReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
conf.set("mapred.textoutputformat.separator", ",");
return (job.waitForCompletion(true) ? 0 : 1);
}
public static void main(String[] args) throws Exception{
int res = ToolRunner.run(new Configuration(),new LeftJoin(),args);
System.exit(res);
}
}
稍微說一下處理邏輯:
1.map階段,把所有輸入拆分為k,v形式。其中k是company_id,即我們要關聯的欄位。如果輸入是employee相關的檔案,那麼map階段的value加上識別符號”a”,表示是employee的輸出。對於salary檔案,加上識別符號”b”。
2.reduce階段,將每個k下的value列表拆分為分別來自employee和salary的兩部分,然後雙層迴圈做笛卡爾積即可。
3.注意的是,因為是left join,所以在reduce階段,如果employee對應的company_id有,而salary沒有,注意要輸出此部分資料。
3.打包
將上面的專案用maven打包,並上傳到伺服器上。
4.使用shell指令碼run起來
在伺服器上寫一個最簡單的shell指令碼,將程式碼run起來:
vim run_join.sh
#!/bin/bash
output=/tmp/wanglei/leftjoin
if hadoop fs -test -d $output
then
hadoop fs -rm -r $output
fi
hadoop jar testjoin.jar leilei.bit.edu.testjoin.LeftJoin \
-Dinput_dir=/tmp/wanglei/employee,/tmp/wanglei/salary \
-Doutput_dir=$output \
-Dmapred.textoutputformat.separator=","
執行此shell指令碼:
./run_join.sh
5.最終輸出
等job跑完以後,檢視最終的輸出結果:
hadoop fs -cat /tmp/wanglei/leftjoin/*
elong,ali,2000
elong,xiaoming,2000
tengxun,lilei,2200
tengxun,xiaoming,2200
jd,mike,1600
jd,david,1600
tb,lucifer,1800
tb,mike,1800
xxx,aaa,null
最終的輸出結果,準確無誤!至此,我們用mr完美實現了left join的功能!
相關文章
- 談談Hadoop MapReduce和Spark MR實現HadoopSpark
- ItermCF的MR並行實現並行
- MapReduce實現之Reduce端重分割槽Join操作最佳化!
- hadoop 多表join:Map side join及Reduce side join範例HadoopIDE
- Fork/Join框架實現原理框架
- 深入理解 Taier:MR on Yarn 的實現原理AIYarn
- Hadoop實戰:Hive操作使用HadoopHive
- 談談fork/join實現原理
- SQL中聯表查詢操作(LEFT JOIN, RIGHT JOIN, INNER JOIN)SQL
- 微軟:Windows10明年將新增混合現實MR微軟Windows
- MR hadoop streaming job的學習 combinerHadoop
- sql語言中join操作SQL
- MySQL Join的底層實現原理MySql
- Hadoop實驗——熟悉常用的HDFS操作Hadoop
- 資料庫實現原理#4(Hash Join)資料庫
- Hadoop-Map/Reduce實現實現倒排索引Hadoop索引
- Flink的join操作樣例
- hash join\nest loop join\sort merge join的實驗OOP
- 用切片操作實現的Python篩法Python
- 用Pthread實現多執行緒操作thread執行緒
- 用js實現返回上一步操作JS
- 用VB實現螢幕陰暗操作 (轉)
- 資料庫實現原理#1(Nested Loop Join)資料庫OOP
- 如何使用Spring Projections和Join實現DTO?SpringProject
- 使用Partitioned Outer Join實現稠化報表
- 用Asp實現對ORACLE資料庫的操作Oracle資料庫
- hadoop常用操作命令Hadoop
- 資料庫實現原理#3(Merge Join).md資料庫
- Flink SQL 如何實現資料流的 Join?SQL
- 用left outer join(sql)實現只顯示重複行最小id的記錄SQL
- 4.7 Hadoop+zookeeper實現HAHadoop
- 實驗二:順序表的基本操作實現及其應用
- Hadoop(三)通過C#/python實現Hadoop MapReduceHadoopC#Python
- 用Swift列舉完美實現3Dtouch快捷操作Swift3D
- 3.MR
- Hadoop的I/O操作Hadoop
- Hadoop(十)HDFS API操作HadoopAPI
- 使用docker安裝hadoop(已實現)DockerHadoop