Mapreduce《案例之内连接》

本文介绍了一个使用MapReduce实现的自连接案例,该案例通过处理家族成员数据来找出每个孙子和其对应的祖父之间的关系。文章提供了完整的Java代码实现,包括Mapper和Reducer的详细逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mapreduce《案例之内连接》

 

数据源:

child parent

Tom Lucy

Tom Jack

Jone Lucy

Jone Jack

Lucy Mary

Lucy Ben

Jack Alice

Jack Jesse

Terry Alice

Terry Jesse

Philip Terry

Philip Alma

Mark Terry

Mark Alma

 

输出结果为:

grandChildgrandParent

TomAlice

TomJesse

JoneAlice

JoneJesse

TomMary

TomBen

JoneMary

JoneBen

PhilipAlice

PhilipJesse

MarkAlice

MarkJesse

 

===================================JAVA  CODE ======================

package gq;

 

 

import java.io.IOException;

import java.util.Iterator;

import java.util.StringTokenizer;

 

import org.apache.hadoop.conf.Configuration;

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;

 

/**

 * 

 * Class Description:自连接,爷找孙子测试类

 *

 * Author:gaoqi  

 *

 * Date:2015年6月5日 下午2:03:08  

 *

 */

public class OwnerJoin {

 

public static int TIME = 0;

 

public static class Map extends Mapper<Object, Text, Text, Text>{

public void map(Object key,Text value,Context context) throws IOException, InterruptedException{

String line = value.toString();

String childname = "";

String parentname="";

String flag = "";

 

StringTokenizer stk = new StringTokenizer(line);

String[] _values = new String[2];

int i= 0;

while(stk.hasMoreElements()){

_values[i]=stk.nextToken();

i++;

}

if(_values[0].compareTo("child") !=0){

childname = _values[0];

parentname = _values[1];

 

flag = "1";//left table

context.write(new Text(parentname), new Text(flag+"-"+childname+"-"+parentname));

 

flag="2";//right table

context.write(new Text(childname), new Text(flag+"-"+childname+"-"+parentname));

}

 

}

 

}

public static class Reduce extends Reducer<Text, Text, Text, Text>{

 

public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{

 

 

String[] pArray = new String[10];

String[] cArray = new String[10];

int pnum = 0;

int cnum = 0;

 

Iterator<Text> vals = values.iterator();

while(vals.hasNext()){

String recod = vals.next().toString();

String[] ss = recod.split("-");

String flag = ss[0];

String childname = ss[1];

String parentname = ss[2];

System.out.println(flag+"-"+childname+"--"+parentname);

if(flag.equals("1")){

cArray[cnum] = childname;

cnum++;

}elseif(flag.equals("2")){

pArray[pnum] = parentname;

pnum++;

}

}

if(TIME ==0){

context.write(new Text("grandChild"), new Text("grandParent"));

  TIME++;

}

if(pnum != 0 && cnum !=0 ){

for(int j=0;j<cnum;j++){

for(int k=0;k<pnum;k++){

context.write(new Text(cArray[j]), new Text(pArray[k]));

}

 

}

}

 

}

}

 

 

public static void main(String[] args) throws Exception {

 

Configuration conf = new Configuration();

 

Job job = new Job(conf,"OwnerJoin");

job.setJarByClass(OwnerJoin.class);

 

job.setMapperClass(Map.class);

//job.setCombinerClass(Reduce.class);

job.setReducerClass(Reduce.class);

 

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

 

FileInputFormat.addInputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/inputOwnerJoin"));

FileOutputFormat.setOutputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/outputOwnerJoin"));

System.exit(job.waitForCompletion(true)?0:1);

 

}

 

}

 

 

### MapReduce在气象数据清洗中的综合应用案例 #### 案例概述 通过MapReduce技术实现气象数据的高效清洗是一项常见的大数据处理任务。在此过程中,主要目标是对原始气象数据进行预处理,使其满足特定的质量标准并便于后续分析[^1]。 #### 数据清洗规则 为了确保数据质量,在此项目中关涉到的具体清洗规则如下: - **分隔符转换**:将源文件中的任意分隔符统一替换为逗号以便于解析和进一步处理。 - **非法数据剔除**:移除不符合逻辑范围的数据项,具体包括但不限于以下条件之一的情况: - 风向数值超出合理区间[0,360]; - 风速或气压存在负值; - 天气状况编码未处于指定集合[0,10]之内; - 湿度百分比偏离正常范围[0,100]; - 温度记录低于绝对最低温(-40℃)或者高于极端高温(50℃)。 #### 文件Join操作 完成初步筛选之后,需执行一次基于天气状态码的连接(join)运算。即将`a.txt`文档内的基础观测资料与描述天空覆盖程度(`cloud cover`)详情的辅助表`sky.txt`相结合,从而把抽象化的天气代号映射成直观易懂的文字表述形式——即所谓的“云属”。 #### 排序策略制定 针对经过上述步骤整理出来的合格条目集,还需按照既定顺序重新排列组合起来,形成最终输出版本。具体的排序依据设定如下优先级次序依次递减的原则来进行安排: 1. 年份、月份以及日期完全一致者视为同一组键(key),以此作为首要分类标志; 2. 对应每组内部成员再依其当日实测气温高低实施升序编列; 3. 若遇到两笔或多笔记录拥有相等体温读数,则继续考察它们各自的风力强度大小关系,并采取从小至大方式排队; 4. 当前述三项指标均无法区分先后位置时,则最后参照大气压力水平做反方向调整(由高往低),达成彻底区分开来的目的。 #### 路径配置说明 整个作业流程涉及两个重要目录地址定义: - 输入端指向存放待加工素材的位置 `/user/test/input/a.txt` (位于HDFS分布式文件系统之上); - 输出成果则会被保存进另一处专门设立用于接收结果的地方 `/user/test/output`(同样处在HDFS环境当中). ```python from operator import itemgetter def mapper(line): fields = line.strip().split(',') try: year, month, day = map(int, fields[:3]) temperature = float(fields[7]) wind_speed = float(fields[8]) pressure = int(fields[9]) humidity = int(fields[10]) weather_code = int(fields[11]) if not all([ 0 <= wind_direction <= 360, wind_speed >= 0, pressure >= 0, 0 <= weather_code <= 10, 0 <= humidity <= 100, -40 <= temperature <= 50]): return None cloud_cover = sky_lookup.get(weather_code,"Unknown") key = f"{year}{month:0>2d}{day:0>2d}" value_tuple = ( round(temperature,2), round(wind_speed,2), pressure, ','.join(map(str,[year,month,day]+fields[3:] + [cloud_cover])) ) yield key,value_tuple except Exception as e: pass def reducer(grouped_data): sorted_group = sorted( grouped_data,key=itemgetter(0,1,-1),reverse=[False,False,True] ) for _,values in sorted_group: yield values[-1] if __name__ == "__main__": from hadoop.io import Text from hadoop.mapreduce.lib.input import TextInputFormat from hadoop.conf import Configuration conf=Configuration() job = Job(conf) job.setJarByClass(MapReduceExample.class) job.setInputFormatClass(TextInputFormat) job.setOutputKeyClass(Text) job.setOutputValueClass(Text) input_path="/user/test/input/a.txt" output_path="/user/test/output" job.waitForCompletion(True) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值