文件如下:

file1:

  1. Beijing Red Star
  2. Shenzhen Thunder
  3. Guangzhou Honda
  4. Beijing Rising
  5. Guangzhou Development Bank
  6. Tencent
  7. Back of Beijing

file2:

  1. Beijing
  2. Guangzhou
  3. Shenzhen
  4. Xian

代码如下(由于水平有限,不保证完全正确,如果发现错误欢迎指正):

  1. package com;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import org.apache.hadoop.conf.Configuration;
  9. import org.apache.hadoop.fs.FileSystem;
  10. import org.apache.hadoop.fs.Path;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17.  
  18. public class Test {
  19. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  20. Configuration config = new Configuration();
  21. config.set("fs.defaultFS", "hdfs://192.168.0.100:9000");
  22. config.set("yarn.resourcemanager.hostname", "192.168.0.100");
  23.  
  24. FileSystem fs = FileSystem.get(config);
  25.  
  26. Job job = Job.getInstance(config);
  27.  
  28. job.setJarByClass(Test.class);
  29.  
  30. //设置所用到的map类
  31. job.setMapperClass(myMapper.class);
  32. job.setMapOutputKeyClass(Text.class);
  33. job.setMapOutputValueClass(Text.class);
  34.  
  35. //设置用到的reducer类
  36. job.setReducerClass(myReducer.class);
  37. job.setOutputKeyClass(Text.class);
  38. job.setOutputValueClass(Text.class);
  39.  
  40. //设置s输入输出地址
  41. FileInputFormat.addInputPath(job, new Path("/FactoryName/"));
  42.  
  43. Path path = new Path("/output2/");
  44.  
  45. if(fs.exists(path)){
  46. fs.delete(path, true);
  47. }
  48.  
  49. //指定文件的输出地址
  50. FileOutputFormat.setOutputPath(job, path);
  51.  
  52. //启动处理任务job
  53. boolean completion = job.waitForCompletion(true);
  54. if(completion){
  55. System.out.println("Job Success!");
  56. }
  57. }
  58.  
  59. public static class myMapper extends Mapper<Object, Text, Text, Text> {
  60.  
  61. // 实现map函数
  62. public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  63. String temp=new String();// 左右表标识
  64. String values=value.toString();
  65. String words[]=values.split("\t");
  66.  
  67. String mapkey = new String();
  68. String mapvalue = new String();
  69.  
  70. //右表:1 Beijing
  71. if (words[].charAt() >= '' && words[].charAt() <= '') {
  72. mapkey = words[];
  73. mapvalue =words[];
  74. temp = "";
  75.  
  76. }else{
  77. //左表:Beijing Red Star 1
  78. mapkey = words[];
  79. mapvalue =words[];
  80. temp = "";
  81. }
  82.  
  83. // 输出左右表
  84. //左表:(1,1+Beijing Red Star)
  85. //右表:(1,2+Beijing)
  86. context.write(new Text(mapkey), new Text(temp + "+"+ mapvalue));
  87. }
  88. }
  89.  
  90. //reduce解析map输出,将value中数据按照左右表分别保存
  91. public static class myReducer extends Reducer<Text, Text, Text, Text> {
  92. // 实现reduce函数
  93. public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
  94.  
  95. List<String> factory = new ArrayList<String>();
  96. List<String> address = new ArrayList<String>();
  97.  
  98. for (Text value : values) {
  99. // 取得左右表标识
  100. char temp=(char) value.charAt();
  101. String words[]=value.toString().split("[+]");//1,Beijing Red Star
  102.  
  103. if(temp==''){
  104. factory.add(words[]);// 左表
  105. }
  106.  
  107. if(temp==''){
  108. address.add(words[]);// 右表
  109. }
  110. }
  111.  
  112. //求出笛卡尔积,并输出
  113. for (String f : factory) {
  114. for (String a : address) {
  115. context.write(new Text(f), new Text(a));
  116. }
  117. }
  118. }
  119. }
  120. }

输出结果如下:

如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击下方的【好文要顶】按钮【精神支持】,因为这两种支持都是使我继续写作、分享的最大动力!

MapReduce:输入是两个文件,file1代表工厂表,包含工厂名列和地址编号列;file2代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名和地址名的对应关系,输出"工厂名----地址名"表的更多相关文章

  1. 使用Vim比较两个文件的内容

    原文地址:http://blog.chinaunix.net/uid-22548820-id-3477464.html 1. 使用vim的比较模式打开两个文件: vim -d file1 file2 ...

  2. LINUX Shell 下求两个文件交集和差集的办法

    http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d ...

  3. 在数组中找几个数的和等于某个数[LeetCode]

    首先明确一点,这个方面的问题设计到的知识点是数组的查找的问题.对于类似的这样的查找操作的具体办法就是三种解决方法: 1.暴力算法,多个for循环,很高的时间复杂度 2.先排序,然后左右夹逼,但是这样会 ...

  4. [Python]根据地址从maps文件中找相应的库名

    /proc/PID/maps提供了进程的memory layout,下面脚本根据给定地址找出相应的库名: #!/usr/bin/python from __future__ import print_ ...

  5. 给定a、b两个文件,各存放50亿个url,每个url各占用64字节,内存限制是4G,如何找出a、b文件共同的url?

    给定a.b两个文件,各存放50亿个url,每个url各占用64字节,内存限制是4G,如何找出a.b文件共同的url? 可以估计每个文件的大小为5G*64=300G,远大于4G.所以不可能将其完全加载到 ...

  6. diff - 找出两个文件的不同点

    总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - 由标准输入设备读入的文本. 作为特别的情况是, dif ...

  7. diff 比较两个文件的差异

    功能:比较两个文件的差异,并把不同地方的信息显示出来.默认diff格式的信息. diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件.也使用patch命令对 ...

  8. diff比较两个文件 linux

    功能:比较两个文件的差异,并把不同地方的信息显示出来.默认diff格式的信息. diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件.也使用patch命令对 ...

  9. 【pyhon】理想论坛爬虫1.05版,将读取和写DB分离成两个文件

    下午再接再厉仿照Nodejs版的理想帖子爬虫把Python版的也改造了下,但美中不足的是完成任务的线程数量似乎停滞在100个左右,让人郁闷.原因还待查. 先把代码贴出来吧,也算个阶段性成果. 爬虫代码 ...

随机推荐

  1. (转)使用 python Matplotlib 库绘图

    运行一个简单的程序例子: import matplotlib.pyplot as plt plt.plot([1,2,3]) plt.ylabel('some numbers') plt.show() ...

  2. windows 中 Eclipse 打开当前文件所在文件夹

    默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explorer 中的节点选择属性,然后复制路径,再打开资源管理器,然后再把路径粘贴进去.而MyEclipse一 ...

  3. 自定义ScrollView 支持添加头部

    自定义ScrollView 支持添加头部并且对头部ImageView支持放大缩小,上滑头部缩小,下滑头部显示放大 使用方式: scrollView = (MyScrollView) findViewB ...

  4. git和svn的详细对比

    近期就[版本管理工具是否进行切换SVN->Git]的问题进行了讨论,于是对svn和Git进行了相关研究,进而梳理出Git的特点(优.缺点),最后将Git与SVN进行了对比,对比结果详见下方内容. ...

  5. IOS 计算本周的起至日期

    unsigned units=NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit; NSCal ...

  6. maven 的构建异常 Could not find artifact ... and 'parent.relativePath'

    完整的异常提示: Non-resolvable parent POM: Could not find artifact com.ecp:ecp-main:pom:0.0.1-SNAPSHOT and ...

  7. angualar入门学习-- 自定义指令 指令编译执行过程

    3个阶段: 一.加载阶段 加载angular.js的源码,找到ng-app确定应用边界范围. 二.编译阶段 compile 查找所有指令,保存在一个列表中 对所有指令按优先级(property属性值) ...

  8. Vue.js_判断与循环

    一.判断,条件语句 1.一元表达式判断 {{ ok ? 'show' : 'hide' }} 2.if判断 v-if='ok' <ol id="ifGrammar"> ...

  9. devmapper: Thin Pool has 162394 free data blocks which is less than minimum required 163840 free data blocks

    问题: 制作镜像的时候报错 devmapper: Thin Pool has 162394 free data blocks which is less than minimum required 1 ...

  10. grep、egrep命令用法

    何谓正则表达式 正则表达式,又称正规表示法.常规表示法(Regular Expression,在代码中常简写为regex.regexp或RE),是一类字符所书写的模式,其中许多字符不表示其字面意义,而 ...