前提准备:

1.hadoop安装运行正常。Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装

2.集成开发环境正常。集成开发环境配置请参考 :Ubuntu 搭建Hadoop源码阅读环境

MapReduce编程实例:

MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析

MapReduce编程实例(二),计算学生平均成绩

MapReduce编程实例(三),数据去重

MapReduce编程实例(四),排序

MapReduce编程实例(五),MapReduce实现单表关联

 
 
多表关联
描述:
两张表关联,如下:
左表:
factoryname address
BMW Factory 2
Benz Factory 3
Voivo Factory 4
LG Factory 5
 
右表:
addressID addressname
2 Beijing
3 Guangzhou
4 Shenzhen
5 Sanya
 
根据addressID关联求出factoryname-address表。很明显,左右关联即可,和单表关联一样。不多作表述,有需要可以查看单表关联的分析。
 
  1. package com.t.hadoop;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.util.GenericOptionsParser;
  15. /**
  16. * 多表排序
  17. * @author daT dev.tao@gmail.com
  18. *
  19. */
  20. public class MTJoin {
  21. public static int times = 1;
  22. public static class MTMapper extends Mapper<Object, Text, Text, Text>{
  23. @Override
  24. protected void map(Object key, Text value, Context context)
  25. throws IOException, InterruptedException {
  26. String relation = new String();
  27. String line = value.toString();
  28. if(line.contains("factoryname")||line.contains("addressID")) return;
  29. int i = 0;
  30. while(line.charAt(i)<'0'||line.charAt(i)>'9'){
  31. i++;
  32. }
  33. if(i>0){//左表
  34. relation = "1";
  35. context.write(new Text(String.valueOf(line.charAt(i))),new Text(relation + line.substring(0,i-1)));
  36. }else{//右表
  37. relation = "2";
  38. context.write(new Text(String.valueOf(line.charAt(i))),new Text(relation +line.substring(i+1)));
  39. }
  40. }
  41. }
  42. public static class MTReducer extends Reducer<Text, Text, Text, Text>{
  43. @Override
  44. protected void reduce(Text key, Iterable<Text> value,Context context)
  45. throws IOException, InterruptedException {
  46. if(times==1){
  47. context.write(new Text("factoryName"), new Text("Address"));
  48. times ++;
  49. }
  50. int factoryNum = 0;
  51. int addressNum = 0;
  52. String[] factorys = new String[10];
  53. String[] addresses = new String[10];
  54. for(Text t:value){
  55. if(t.charAt(0)=='1'){//左表
  56. factorys[factoryNum]=t.toString().substring(1);
  57. factoryNum++;
  58. }else{//右表
  59. addresses[addressNum]=t.toString().substring(1);
  60. addressNum++;
  61. }
  62. }
  63. for(int i = 0;i<factoryNum;i++){
  64. for(int j=0;j<addressNum;j++){
  65. context.write(new Text(factorys[i]), new Text(addresses[j]));
  66. }
  67. }
  68. }
  69. }
  70. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
  71. Configuration conf = new Configuration();
  72. String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
  73. if(otherArgs.length<2){
  74. System.out.println("Parameters error");
  75. System.exit(2);
  76. }
  77. Job job =new Job(conf,"MTjoin");
  78. job.setJarByClass(MTJoin.class);
  79. job.setMapperClass(MTMapper.class);
  80. job.setReducerClass(MTReducer.class);
  81. job.setOutputKeyClass(Text.class);
  82. job.setOutputValueClass(Text.class);
  83. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  84. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  85. System.exit(job.waitForCompletion(true)?0:1);
  86. }
  87. }
输出结果:
factoryName Address
BMW Factory Beijing
Benz Factory Guangzhou
Voivo Factory Shenzhen
LG Factory Sanya
 
欢迎同学们多多交流~

MapReduce编程实例6的更多相关文章

  1. MapReduce编程实例5

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  2. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  3. MapReduce编程实例3

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  4. MapReduce编程实例2

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  5. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  6. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  7. MapReduce编程实例

    MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...

  8. hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)

    刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...

  9. Hadoop--mapreduce编程实例1

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

随机推荐

  1. Python 谷歌翻译

    #coding=utf-8 import re import urllib import urllib2 url_google = 'http://translate.google.cn' reg_t ...

  2. 【中英】mac电脑清理软件 ToolWiz Mac Boost

    简单介绍: ToolWiz Mac Boost是一款适用于Mac电脑清理加速最好的终极应用, 使您的Mac电脑干净有条理, 执行飞速且稳定.始终保持最佳状态! ToolWiz Mac Boost 运用 ...

  3. [多校2015.02.1006 高斯消元] hdu 5305 Friends

    题意: 给你n个人m条关系 每条关系包括a,b 代表a和b能够是线上朋友也能够是线下朋友 然后保证每一个人的线上朋友数和线下朋友数相等 问你有多少种组成方法 思路: 官方题解是爆搜+剪枝,然而并不会写 ...

  4. [React] Use the useReducer Hook and Dispatch Actions to Update State (useReducer, useMemo, useEffect)

    As an alternate to useState, you could also use the useReducer hook that provides state and a dispat ...

  5. [笔记][Java7并发编程实战手冊]3.8 并发任务间的数据交换Exchanger

    [笔记][Java7并发编程实战手冊]系列文件夹 简单介绍 Exchanger 是一个同步辅助类.用于两个并发线程之间在一个同步点进行数据交换. 同意两个线程在某一个点进行数据交换. 本章exchan ...

  6. Flutter混合工程改造实践

    背景 6月下旬,我们首次尝试用Flutter开发AI拍app.开发的调研准备阶段没有参考业界实践,导致我们踩到一些填不上的坑.在这些坑中,最让我感到棘手的是Flutter和原生页面混合栈管理的问题. ...

  7. 解压zip,解决中文乱码

    Project p = new Project();        Expand e = new Expand();        e.setProject(p);        e.setSrc(f ...

  8. taro 打包微信小程序运行失败(一)

    1.报错信息 thirdScriptError sdk uncaught third Error Cannot read property 'createTextNode' of undefined ...

  9. [Done]FindBugs: boxing/unboxing to parse a primitive

    在开发过程中遇到了以下问题: FindBugs: boxing/unboxing to parse a primitive 查看代码(左边是老代码,右边是新的): 问题出在 自动装箱和拆箱的检查. 参 ...

  10. javascript 自定义动画函数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...