c001.txt

------------------------------

filetype|commid|commname|addressid
comm|1|罗湖小区1|1
comm|2|罗湖小区2|1
comm|3|宝安小区1|4
comm|4|南山小区1|3
comm|5|南山小区2|3
comm|6|福田小区1|2
comm|7|福田小区2|2
comm|8|宝安2|4
comm|9|南山3|3

c002.txt

----------------------------

filetype|commid|commname|addressid
comm|10|罗湖小区7|1
comm|11|罗湖小区8|1
comm|12|宝安小区5|4
comm|13|南山小区6|3
comm|14|南山小区7|3
comm|15|福田小区6|2
comm|16|福田小区8|2

a001.txt

-------------------------

filetype|addressid|address
addr|1|罗湖
addr|2|福田
addr|3|南山
addr|4|宝安

输出结果:

-----------------------

commid commname addr
15 福田小区6 福田
16 福田小区8 福田
6 福田小区1 福田
7 福田小区2 福田
13 南山小区6 南山
14 南山小区7 南山
4 南山小区1 南山
5 南山小区2 南山
9 南山3 南山
3 宝安小区1 宝安
8 宝安2 宝安
12 宝安小区5 宝安

----------------------------

代码:

package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
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;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.fs.Path; public class TestUnion { public static int count=0;
public static class TestUnionMapper extends Mapper<Object,Text,Text,Text>
{
public void map(Object key,Text values,Context context) throws IOException,InterruptedException
{
if(values.toString().indexOf("filetype")>=0)
{
return;
}
StringTokenizer itr=new StringTokenizer(values.toString(),"|");
String fileType="";
String fileTypeId="";
while(itr.hasMoreTokens())
{
fileType=itr.nextToken();
if(fileType.compareToIgnoreCase("addr")==0)
{
String addressId=itr.nextToken();
String addressName=itr.nextToken();
fileTypeId="2"; //标记为地址
context.write(new Text(addressId),new Text(fileTypeId+"|"+addressName));
}
else if(fileType.compareToIgnoreCase("comm")==0)
{
String commId=itr.nextToken();
String commName=itr.nextToken();
String addressId=itr.nextToken();
fileTypeId="1"; //标记为小区
context.write(new Text(addressId),new Text(fileTypeId+"|"+commId+"|"+commName));
}
}
}
}
public static class TestUnionReducer extends Reducer<Text,Text,Text,Text>
{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException
{
List<String> addrs=new ArrayList<String>();
List<String> comms=new ArrayList<String>();
if(count<=0)
{
count++;
context.write(new Text("commid"),new Text("commname addr"));
return;
}
else
{       
for(Text val:values)
{
String []astr=val.toString().trim().split("\\|"); // | 为特殊字符,必须转义
String fileTypeId=astr[0];
if(fileTypeId.compareToIgnoreCase("1")==0) //comm
{
String commId=astr[1];
String commName=astr[2];
comms.add(commId+" "+commName);
}
else if(fileTypeId.compareToIgnoreCase("2")==0) //addr
{
String addr=astr[1];
addrs.add(addr);
}
}
}
if(comms.size()>0 && addrs.size()>0)
{
for(int m=0;m<comms.size();m++)
for(int n=0;n<addrs.size();n++) //其实只有一条记录对应上面的
context.write(new Text(comms.get(m)),new Text(addrs.get(n)));
}
}
} public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
if(args.length!=2)
{
System.err.println("please input two agrs:<in> <out>");
System.exit(2);
}
Configuration conf=new Configuration();
Job job=new Job(conf,"union data");
job.setJarByClass(TestUnion.class);
job.setMapperClass(TestUnionMapper.class);
job.setReducerClass(TestUnionReducer.class);
//job.setNumReduceTasks(0);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job,new Path(args[0]));
FileOutputFormat.setOutputPath(job,new Path(args[1]));
System.exit(job.waitForCompletion(true)?0:1);
} }

主要利用了reduce函数相同的KEY值聚合在一起的规则。

hadoop关联文件处理的更多相关文章

  1. eclipse中关联文件设置方法

    在前几次的试验中,只是做了处于应用程序最上层的界面设计,其实还不知程序在运行过程中到底调用了哪些函数,这些函数是怎么实现的,由于搭建环境时没有进行文件关联,所以在环境中无法实现ctrl键+左击鼠标的方 ...

  2. Hadoop HDFS文件常用操作及注意事项

    Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...

  3. Delphi 7使用自定义图标关联文件类型

    Delphi 7使用自定义图标关联文件类型 5.2 Delphi编程(40)  版权声明:本文为博主原创文章,未经博主允许不得转载. 在开发过程中,我们经常需要属于自己的文件类型,自定义的后缀名不仅可 ...

  4. Hadoop的文件读写操作流程

    以下主要讲解了Hadoop的文件读写操作流程: 读文件 读文件时内部工作机制参看下图: 客户端通过调用FileSystem对象(对应于HDFS文件系统,调用DistributedFileSystem对 ...

  5. hadoop 提高hdfs删文件效率----hadoop删除文件流程解析

    前言 这段时间在用hdfs,由于要处理的文件比较多,要及时产出旧文件,但是发现hdfs的blocks数一直在上涨,经分析是hdfs写入的速度较快,而block回收较慢,所以分心了一下hadoop删文件 ...

  6. win10 uwp 关联文件

    有时候应用需要打开后缀名为x的文件,那么如何从文件打开应用? 首先,需要打开 Package.appxmanifest 添加一个功能,需要添加最少有名称,文件类型. 上面的图就是我添加jpg 的方法, ...

  7. <Hadoop><SequenceFile><Hadoop小文件>

    Origin 我们首先理解一下SequenceFile试图解决什么问题,然后看SeqFile怎么解决这些问题. In HDFS 序列文件是解决Hadoop小文件问题的一个方法: 小文件是显著小于HDF ...

  8. 一图看懂hadoop分布式文件存储系统HDFS工作原理

    一图看懂hadoop分布式文件存储系统HDFS工作原理

  9. 64位Win7中7zip无法关联文件的问题

    问题1:win7x64中安装了7zip,在解压文件右键打开无法关联文件. 解决方法1:在开始菜单中打开7-zip File Manage->工具 ->选项 ->7-zip 勾选“添加 ...

随机推荐

  1. Axiom3D:Ogre中Mesh网格分解成点线面。

    这个需求可能比较古怪,一般Mesh我们组装好顶点,索引数据后,直接放入索引缓冲渲染就好了.但是如果有些特殊需要,如需要标注出Mesh的顶点,线,面这些信息,以及特殊显示这些信息. 最开始我想的是自己分 ...

  2. 使用tomcat搭建centos的yum源

    最近在折腾大数据,需要搭建一个yum源.一般的做法是在CentOS中安装httpd,然后将rpm包放入/var/www/html下面,再执行[createrepo .]即可. 不过虚拟机对传文件终归是 ...

  3. tpshop商品属性表关系

    TPshop 里面的商品属性, 首先看看TPshop商品详情中的属性介绍, 纯展示给用户看的. 再来tpshop看看商品列表帅选页面的属性,可以根据属性帅选不同的商品 再来看看tpshop后台属性管理 ...

  4. Websphere设置JVM时区解决程序、日志时间快8小时问题

    原文链接:http://www.itpub.net/thread-1204714-1-1.html 相信很多使用Websphere的朋友会经常在Windows操作系统中遇到程序时间快8小时的问题 如果 ...

  5. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]

    From: http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-remo ...

  6. linux中service的问题

    1.描述问题 2.解决方案 systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services ...

  7. 磁盘格式化/磁盘挂载/手动增加swap空间

    4.5/4.6 磁盘格式化 4.7/4.8 磁盘挂载 4.9 手动增加swap空间 磁盘格式化 查看centos7支持的文件系统格式 cat  /etc/filesystem,centos7默认的文件 ...

  8. Css 去除浮动

    清除浮动的方法 清除浮动方法大致有两类,一类是clear:both | left | right ,另一类则是创建BFC,细分又可以分为多种. 通过在浮动元素末尾添加一个空的标签例如并设置样式为cle ...

  9. EJB简介

    EJB是sun的服务器端组件模型,最大的用处是部署分布式应用程序,类似微软的.net技术.凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台.  EJB (Enterprise ...

  10. Linq 查询某个字段为null的数据

    如tb_flag 数据结构如下:flag int null 不能使用:flag==null 生成的SQL语句为 where flag=null   建议使用:可空类型 用Nullable<T&g ...