我们都知道1个产品唯1对应1个单价,本案例将通过两种类型输入文件:product类(产品)和price类(价格)进行1对1的关联查询,得到产品名(例如:手表)与产品价格(例如:$100)的关联信息。
硬件环境:Centos 6.5 服务器4台(1台为Master节点,3台为Slave节点)
软件环境:Java 1.7.0_45、hadoop⑴.2.1
首先使用默许的TextInputFormat类对输入文件进行处理,得到文本中每行的偏移量及其内容并存入
Reduce进程首先对输入
驱动核心代码实现以下,详细源码请参考:ProductJoinPricesrccomzonesion ablejoinProductJoinPrice.java。
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length != 3){
System.err.println("Usage:Join <productTableDir> <priceTableDir> <output>");
}
//定义Job
Job job = new Job(conf,"join productTable and priceTable");
//定义输入文件路径:商品表 & 价格表
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//<productTableDir>
FileInputFormat.addInputPath(job, new Path(otherArgs[1]));//<priceTableDir>
//定义输出文件路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[2]));//<output>
//设置Jar运行入口类和Mapper类和Reducer类
job.setJarByClass(ProductJoinPrice.class);
job.setMapperClass(MapperClass.class);
job.setReducerClass(ReducerClass.class);
//设置输出文件的key和value类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
System.exit(job.waitForCompletion(true) ? 0 : ⑴);
}
[hadoop@K-Master ~]$ start-dfs.sh
[hadoop@K-Master ~]$ start-mapred.sh
[hadoop@K-Master ~]$ jps
5283 SecondaryNameNode
5445 JobTracker
5578 Jps
5109 NameNode
#设置工作环境
[hadoop@K-Master ~]$ mkdir -p /usr/hadoop/workspace/MapReduce
#部署源码
将ProductJoinPrice文件夹拷贝到/usr/hadoop/workspace/MapReduce/ 路径下;
… 你可以直接 下载 ProductJoinPrice
#切换工作目录
[hadoop@K-Master ~]$ cd /usr/hadoop/workspace/MapReduce/ProductJoinPrice
#编译文件
[hadoop@K-Master ProductJoinPrice]$ javac -classpath /usr/hadoop/hadoop-core⑴.2.1.jar:/usr/hadoop/lib/commons-cli⑴.2.jar -d bin src/com/zonesion/tablejoin/ProductJoinPrice.java
[hadoop@K-Master ProductJoinPrice]$ ls bin/com/zonesion/tablejoin/ -la
总用量 12
drwxrwxr-x 2 hadoop hadoop 110 7月 31 17:19 .
drwxrwxr-x 3 hadoop hadoop 22 7月 31 17:19 ..
-rw-rw-r-- 1 hadoop hadoop 1770 7月 31 17:39 ProductJoinPrice.class
-rw-rw-r-- 1 hadoop hadoop 2134 7月 31 17:39 ProductJoinPrice$CommonReduce.class
-rw-rw-r-- 1 hadoop hadoop 2375 7月 31 17:39 ProductJoinPrice$PreMapper.class
[hadoop@K-Master ProductJoinPrice]$ jar -cvf ProductJoinPrice.jar -C bin/ .
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/zonesion/(in = 0) (out= 0)(stored 0%)
adding: com/zonesion/tablejoin/(in = 0) (out= 0)(stored 0%)
adding: com/zonesion/tablejoin/ProductJoinPrice$MapperClass.class(in = 2277) (out= 967)(deflated 57%)
adding: com/zonesion/tablejoin/ProductJoinPrice.class(in = 1897) (out= 970)(deflated 48%)
adding: com/zonesion/tablejoin/ProductJoinPrice$ReducerClass.class(in = 1999) (out= 880)(deflated 55%)
#创建product输入文件夹
[hadoop@K-Master ProductJoinPrice]$ hadoop fs -mkdir ProductJoinPrice/input/product/
#创建price输入文件夹
[hadoop@K-Master ProductJoinPrice]$ hadoop fs -mkdir ProductJoinPrice/input/price/
#上传文件到product文件夹
[hadoop@K-Master ProductJoinPrice]$ hadoop fs -put input/product* ProductJoinPrice/input/product/
#上传文件到price文件夹
[hadoop@K-Master ProductJoinPrice]$ hadoop fs -put input/price* ProductJoinPrice/input/price/
[hadoop@K-Master ProductJoinPrice]$ hadoop jar ProductJoinPrice.jar com.zonesion.tablejoin.ProductJoinPrice ProductJoinPrice/input/product/ ProductJoinPrice/input/price/ ProductJoinPrice/output
14/08/01 09:32:53 INFO input.FileInputFormat: Total input paths to process : 4
14/08/01 09:32:53 INFO util.NativeCodeLoader: Loaded the native-hadoop library
14/08/01 09:32:53 WARN snappy.LoadSnappy: Snappy native library not loaded
14/08/01 09:32:54 INFO mapred.JobClient: Running job: job_201408010921_0004
14/08/01 09:32:55 INFO mapred.JobClient: map 0% reduce 0%
14/08/01 09:32:57 INFO mapred.JobClient: map 50% reduce 0%
14/08/01 09:32:59 INFO mapred.JobClient: map 100% reduce 0%
14/08/01 09:33:06 INFO mapred.JobClient: map 100% reduce 100%
14/08/01 09:33:06 INFO mapred.JobClient: Job complete: job_201408010921_0004
14/08/01 09:33:06 INFO mapred.JobClient: Counters: 29
......
[hadoop@K-Master ProductJoinPrice]$ hadoop fs -ls ProductJoinPrice/output
Found 3 items
-rw-r--r-- 1 hadoop supergroup 0 2014-08-01 09:33 /user/hadoop/ProductJoinPrice/output/_SUCCESS
drwxr-xr-x - hadoop supergroup 0 2014-08-01 09:32 /user/hadoop/ProductJoinPrice/output/_logs
-rw-r--r-- 1 hadoop supergroup 107 2014-08-01 09:33 /user/hadoop/ProductJoinPrice/output/part-r-00000
[hadoop@K-Master ProductJoinPrice]$ hadoop fs -cat ProductJoinPrice/output/part-r-00000
手表 $100
休闲帽 $200
移动硬盘 $300
u盘 $900
3星手机 $800
联想笔记本 $2000
钱包 $100
【Hadoop基础教程】5、Hadoop之单词计数
【Hadoop基础教程】6、Hadoop之单表关联查询
【Hadoop基础教程】7、Hadoop之1对1关联查询
【Hadoop基础教程】8、Hadoop之1对多关联查询
【Hadoop基础教程】9、Hadoop之倒排索引