MapReduce编程实例6
前提准备:
1.hadoop安装运行正常。Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装
2.集成开发环境正常。集成开发环境配置请参考 :Ubuntu 搭建Hadoop源码阅读环境
MapReduce编程实例:
MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析
MapReduce编程实例(五),MapReduce实现单表关联
BMW Factory 2
Benz Factory 3
Voivo Factory 4
LG Factory 5
2 Beijing
3 Guangzhou
4 Shenzhen
5 Sanya
- package com.t.hadoop;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- 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;
- import org.apache.hadoop.util.GenericOptionsParser;
- /**
- * 多表排序
- * @author daT dev.tao@gmail.com
- *
- */
- public class MTJoin {
- public static int times = 1;
- public static class MTMapper extends Mapper<Object, Text, Text, Text>{
- @Override
- protected void map(Object key, Text value, Context context)
- throws IOException, InterruptedException {
- String relation = new String();
- String line = value.toString();
- if(line.contains("factoryname")||line.contains("addressID")) return;
- int i = 0;
- while(line.charAt(i)<'0'||line.charAt(i)>'9'){
- i++;
- }
- if(i>0){//左表
- relation = "1";
- context.write(new Text(String.valueOf(line.charAt(i))),new Text(relation + line.substring(0,i-1)));
- }else{//右表
- relation = "2";
- context.write(new Text(String.valueOf(line.charAt(i))),new Text(relation +line.substring(i+1)));
- }
- }
- }
- public static class MTReducer extends Reducer<Text, Text, Text, Text>{
- @Override
- protected void reduce(Text key, Iterable<Text> value,Context context)
- throws IOException, InterruptedException {
- if(times==1){
- context.write(new Text("factoryName"), new Text("Address"));
- times ++;
- }
- int factoryNum = 0;
- int addressNum = 0;
- String[] factorys = new String[10];
- String[] addresses = new String[10];
- for(Text t:value){
- if(t.charAt(0)=='1'){//左表
- factorys[factoryNum]=t.toString().substring(1);
- factoryNum++;
- }else{//右表
- addresses[addressNum]=t.toString().substring(1);
- addressNum++;
- }
- }
- for(int i = 0;i<factoryNum;i++){
- for(int j=0;j<addressNum;j++){
- context.write(new Text(factorys[i]), new Text(addresses[j]));
- }
- }
- }
- }
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
- Configuration conf = new Configuration();
- String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
- if(otherArgs.length<2){
- System.out.println("Parameters error");
- System.exit(2);
- }
- Job job =new Job(conf,"MTjoin");
- job.setJarByClass(MTJoin.class);
- job.setMapperClass(MTMapper.class);
- job.setReducerClass(MTReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
- System.exit(job.waitForCompletion(true)?0:1);
- }
- }
BMW Factory Beijing
Benz Factory Guangzhou
Voivo Factory Shenzhen
LG Factory Sanya
MapReduce编程实例6的更多相关文章
- MapReduce编程实例5
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
- MapReduce编程实例4
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- MapReduce编程实例3
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- MapReduce编程实例2
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- 三、MapReduce编程实例
前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- MapReduce编程实例
MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...
- hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)
刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...
- Hadoop--mapreduce编程实例1
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
随机推荐
- 解决 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type的问题
具体错误如下: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying be ...
- 使用百度地图API实现轨迹回放
调用百度地图API实现路线的轨迹回放功能其实很简单,只要搞懂以下几点即可: 1.需要用Polyline方法先绘制好路线图 2.用Marker添加标注点 3.关键一步,通过结合定时器,使用Marker创 ...
- SqlServer 删除重复记录
在给一个客户上线的系统里发现有一张表里出现了重复的数据,结果通过排查代码发现确实业务逻辑有问题,在修改了代码后需要将为数据库里的重复数据删除 在CSDN上找到解决方案,对线上的数据库尽量不要执行删除操 ...
- (剑指Offer)面试题41:和为s的两个数字
题目: 输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s,如果有多对数字的和等于s,输出任意一对即可. 思路: 1.枚举 固定一个数字,然后依次判断数组中该数字后面的数字与 ...
- 时间记录APP———Time Meter
关注过时间管理的人可能都听过大名鼎鼎的柳比歇夫的时间记录法,在几年前,大多人都推荐纸笔的记录方法,但是纸笔总是会忘,越来越智能的手机可是总不会忘得,所以我始终在寻找一款手机端好用的APP. 不管是时间 ...
- 2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数
NPOI教程:http://www.cnblogs.com/atao/archive/2009/11/15/1603528.html 之所有说NPOI强大,是因为常用的Excel操作她都可以通过编程的 ...
- java.lang.Void and void
java.lang.Void is analogous to java.lang.Integer. Integer is a way of boxing values of the primitive ...
- iOS GZWaterfall任何形式的瀑布流
概述 使用UICollectionView可以布局各种各样的瀑布流,下面我写了几种不同布局的瀑布流样式 详细 代码下载:http://www.demodashi.com/demo/11018.html ...
- moment
var now = moment(1410181234567)var formatted = now.format('YYYY-MM-DD HH:mm:ss')console.log(formatte ...
- ASP.NET MVC之Html.RenderAction(无操作方法 传参数)
WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...