MapReduce:输入是两个文件,file1代表工厂表,包含工厂名列和地址编号列;file2代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名和地址名的对应关系,输出"工厂名----地址名"表
文件如下:
file1:
- Beijing Red Star
- Shenzhen Thunder
- Guangzhou Honda
- Beijing Rising
- Guangzhou Development Bank
- Tencent
- Back of Beijing
file2:
- Beijing
- Guangzhou
- Shenzhen
- Xian
代码如下(由于水平有限,不保证完全正确,如果发现错误欢迎指正):
- package com;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- 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.output.FileOutputFormat;
- public class Test {
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
- Configuration config = new Configuration();
- config.set("fs.defaultFS", "hdfs://192.168.0.100:9000");
- config.set("yarn.resourcemanager.hostname", "192.168.0.100");
- FileSystem fs = FileSystem.get(config);
- Job job = Job.getInstance(config);
- job.setJarByClass(Test.class);
- //设置所用到的map类
- job.setMapperClass(myMapper.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(Text.class);
- //设置用到的reducer类
- job.setReducerClass(myReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- //设置s输入输出地址
- FileInputFormat.addInputPath(job, new Path("/FactoryName/"));
- Path path = new Path("/output2/");
- if(fs.exists(path)){
- fs.delete(path, true);
- }
- //指定文件的输出地址
- FileOutputFormat.setOutputPath(job, path);
- //启动处理任务job
- boolean completion = job.waitForCompletion(true);
- if(completion){
- System.out.println("Job Success!");
- }
- }
- public static class myMapper extends Mapper<Object, Text, Text, Text> {
- // 实现map函数
- public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
- String temp=new String();// 左右表标识
- String values=value.toString();
- String words[]=values.split("\t");
- String mapkey = new String();
- String mapvalue = new String();
- //右表:1 Beijing
- if (words[].charAt() >= '' && words[].charAt() <= '') {
- mapkey = words[];
- mapvalue =words[];
- temp = "";
- }else{
- //左表:Beijing Red Star 1
- mapkey = words[];
- mapvalue =words[];
- temp = "";
- }
- // 输出左右表
- //左表:(1,1+Beijing Red Star)
- //右表:(1,2+Beijing)
- context.write(new Text(mapkey), new Text(temp + "+"+ mapvalue));
- }
- }
- //reduce解析map输出,将value中数据按照左右表分别保存
- public static class myReducer extends Reducer<Text, Text, Text, Text> {
- // 实现reduce函数
- public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
- List<String> factory = new ArrayList<String>();
- List<String> address = new ArrayList<String>();
- for (Text value : values) {
- // 取得左右表标识
- char temp=(char) value.charAt();
- String words[]=value.toString().split("[+]");//1,Beijing Red Star
- if(temp==''){
- factory.add(words[]);// 左表
- }
- if(temp==''){
- address.add(words[]);// 右表
- }
- }
- //求出笛卡尔积,并输出
- for (String f : factory) {
- for (String a : address) {
- context.write(new Text(f), new Text(a));
- }
- }
- }
- }
- }
输出结果如下:
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击下方的【好文要顶】按钮【精神支持】,因为这两种支持都是使我继续写作、分享的最大动力!
MapReduce:输入是两个文件,file1代表工厂表,包含工厂名列和地址编号列;file2代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名和地址名的对应关系,输出"工厂名----地址名"表的更多相关文章
- 使用Vim比较两个文件的内容
原文地址:http://blog.chinaunix.net/uid-22548820-id-3477464.html 1. 使用vim的比较模式打开两个文件: vim -d file1 file2 ...
- LINUX Shell 下求两个文件交集和差集的办法
http://blog.csdn.net/autofei/article/details/6579320 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d ...
- 在数组中找几个数的和等于某个数[LeetCode]
首先明确一点,这个方面的问题设计到的知识点是数组的查找的问题.对于类似的这样的查找操作的具体办法就是三种解决方法: 1.暴力算法,多个for循环,很高的时间复杂度 2.先排序,然后左右夹逼,但是这样会 ...
- [Python]根据地址从maps文件中找相应的库名
/proc/PID/maps提供了进程的memory layout,下面脚本根据给定地址找出相应的库名: #!/usr/bin/python from __future__ import print_ ...
- 给定a、b两个文件,各存放50亿个url,每个url各占用64字节,内存限制是4G,如何找出a、b文件共同的url?
给定a.b两个文件,各存放50亿个url,每个url各占用64字节,内存限制是4G,如何找出a.b文件共同的url? 可以估计每个文件的大小为5G*64=300G,远大于4G.所以不可能将其完全加载到 ...
- diff - 找出两个文件的不同点
总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - 由标准输入设备读入的文本. 作为特别的情况是, dif ...
- diff 比较两个文件的差异
功能:比较两个文件的差异,并把不同地方的信息显示出来.默认diff格式的信息. diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件.也使用patch命令对 ...
- diff比较两个文件 linux
功能:比较两个文件的差异,并把不同地方的信息显示出来.默认diff格式的信息. diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件.也使用patch命令对 ...
- 【pyhon】理想论坛爬虫1.05版,将读取和写DB分离成两个文件
下午再接再厉仿照Nodejs版的理想帖子爬虫把Python版的也改造了下,但美中不足的是完成任务的线程数量似乎停滞在100个左右,让人郁闷.原因还待查. 先把代码贴出来吧,也算个阶段性成果. 爬虫代码 ...
随机推荐
- (转)使用 python Matplotlib 库绘图
运行一个简单的程序例子: import matplotlib.pyplot as plt plt.plot([1,2,3]) plt.ylabel('some numbers') plt.show() ...
- windows 中 Eclipse 打开当前文件所在文件夹
默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explorer 中的节点选择属性,然后复制路径,再打开资源管理器,然后再把路径粘贴进去.而MyEclipse一 ...
- 自定义ScrollView 支持添加头部
自定义ScrollView 支持添加头部并且对头部ImageView支持放大缩小,上滑头部缩小,下滑头部显示放大 使用方式: scrollView = (MyScrollView) findViewB ...
- git和svn的详细对比
近期就[版本管理工具是否进行切换SVN->Git]的问题进行了讨论,于是对svn和Git进行了相关研究,进而梳理出Git的特点(优.缺点),最后将Git与SVN进行了对比,对比结果详见下方内容. ...
- IOS 计算本周的起至日期
unsigned units=NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit; NSCal ...
- 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 ...
- angualar入门学习-- 自定义指令 指令编译执行过程
3个阶段: 一.加载阶段 加载angular.js的源码,找到ng-app确定应用边界范围. 二.编译阶段 compile 查找所有指令,保存在一个列表中 对所有指令按优先级(property属性值) ...
- Vue.js_判断与循环
一.判断,条件语句 1.一元表达式判断 {{ ok ? 'show' : 'hide' }} 2.if判断 v-if='ok' <ol id="ifGrammar"> ...
- 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 ...
- grep、egrep命令用法
何谓正则表达式 正则表达式,又称正规表示法.常规表示法(Regular Expression,在代码中常简写为regex.regexp或RE),是一类字符所书写的模式,其中许多字符不表示其字面意义,而 ...