我们都知道一个产品唯一对应一个单价,本案例将通过两种类型输入文件:product类(产品)和price类(价格)进行一对一的关联查询,得到产品名(例如:手表)与产品价格(例如:$100)的关联信息。
开发环境硬件环境:CentOS 6.5 服务器4台(一台为Master节点,三台为Slave节点)
软件环境:Java 1.7.0_45、Hadoop-1.2.1
首先使用默认的TextInputFormat类对输入文件进行处理,得到文本中每行的偏移量及其内容并存入
2、 Reduce过程Reduce过程首先对输入
3、 驱动实现驱动核心代码实现如下,详细源码请参考:ProductJoinPrice\src\com\zonesion\tablejoin\ProductJoinPrice.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 : -1); } 4、部署运行 1)启动Hadoop集群 [hadoop@K-Master ~]$ start-dfs.sh [hadoop@K-Master ~]$ start-mapred.sh [hadoop@K-Master ~]$ jps 5283 SecondaryNameNode 5445 JobTracker 5578 Jps 5109 NameNode 2)部署源码 #设置工作环境 [hadoop@K-Master ~]$ mkdir -p /usr/hadoop/workspace/MapReduce #部署源码 将ProductJoinPrice文件夹拷贝到/usr/hadoop/workspace/MapReduce/ 路径下;… 你可以直接 下载 ProductJoinPrice
3)编译文件 #切换工作目录 [hadoop@K-Master ~]$ cd /usr/hadoop/workspace/MapReduce/ProductJoinPrice #编译文件 [hadoop@K-Master ProductJoinPrice]$ javac -classpath /usr/hadoop/hadoop-core-1.2.1.jar:/usr/hadoop/lib/commons-cli-1.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 4)打包jar文件 [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%) 5)上传输入文件 #创建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/ 6)运行Jar文件 [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 ...... 7)查看输出结果 [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 三星手机 $800 联想笔记本 $2000 钱包 $100