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 ...
随机推荐
- TensorFlow进阶(四)---名称域和共享变量
变量作用域 tensorflow提供了变量作用域和共享变量这样的概念,有几个重要的作用. 让模型代码更加清晰,作用分明 变量作用域域 通过tf.variable_scope(<scope_nam ...
- myBatis + SpringMVC上传、下载文件
摘自: http://limingnihao.iteye.com/blog/1069503 环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何 ...
- JavaScript中的递归函数问题
学过其它编程语言的都应该会知道递归这个问题,递归函数是在一个函数通过名字调用自身的情况下后构成的. function fac(num){ if(num<=1){ return 1; }else{ ...
- powershell 远程重启/关闭服务器
powershell 远程重启/关闭服务器 #启动winrm PS C:\Windows\system32> winrm quickconfig -q #设置信任主机 PS C:\Windows ...
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
We refactor a function that uses try/catch to a single composed expression using Either. We then int ...
- 微信小程序 - 为何setData到页面上有的加分号
Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { let _page = t ...
- spring测试实例
我们以前要进行单元测试,必须先得到ApplicationContext对象,再通过它得到业务对象,非常麻烦,重复代码也多.基于spring3的单元测试很好的解决了这个问题 基于spring3的单元测试 ...
- Unity for Windows: II – Publishing Unity games to Windows Store
原地址:http://digitalerr0r.wordpress.com/2013/08/27/unity-for-windows-ii-publishing-to-windows-8/ Windo ...
- JUnit 3.8 通过反射测试私有方法
测试私有(private)的方法有两种: 1)把目标类的私有方法(修饰符:private)修改为(public),不推荐,因为修改了源程序不佳 2)通过反射 (推荐) 代码演示: 目标程序 Priva ...
- OpenERP函數字段的應用
在ERP開發過程中經常會使用到某字段的值是由其他字段計算得來,並且有些還需要將計算的結果存入資料庫. 以上功能上OpenERP中是用field.function實現的 其中有種模式 a). 只計算,不 ...