赞
踩
一、实验内容
求关系R与关系S的交集。
假设有关系R与关系S,他们模式一致,要求设计MapReduce程序,找出两个关系中完全相同的记录,输出的每行是一条记录。
关系R与关系S的模式为:name,age,gender
(注:R和S的数据不放出来了,用自己的数据即可)
二、实验环境--Hadoop集群
三、实验过程
注意数据与数据间是用逗号隔开还是空格隔开
改第一段代码这里
String[] strs = ivalue.toString().split(",");//这里我原本的数据是使用逗号隔开,如果你的是空格的话,就删掉逗号
- package org.zkpk.hadoop.hdfs;
-
- import java.io.IOException;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Mapper;
-
- public class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
-
- public void map(LongWritable ikey,Text ivalue,Context context)
- throws IOException,InterruptedException{
- String[] strs = ivalue.toString().split(",");
- if(strs.length==3){
- context.write(ivalue, new IntWritable(1));
- }
- }
- }
-
- package org.zkpk.hadoop.hdfs;
-
- import java.io.IOException;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.NullWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Reducer;
-
- public class Reduce extends Reducer<Text,IntWritable,Text,NullWritable>{
- public void reduce(Text key,Iterable<IntWritable> values,Context context)
- throws IOException,InterruptedException{
- int count = 0;
- for(IntWritable value:values){
- ++count;
- }
- if(count == 2)
- context.write(key, NullWritable.get());
- }
- }
-
- package org.zkpk.hadoop.hdfs;
-
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.NullWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
-
- public class Dir {
- public static void main(String[] args) throws Exception{
- Configuration conf =new Configuration();
- Job job = Job.getInstance(conf);
- job.setJarByClass(Dir.class);
- job.setMapperClass(Map.class);
- job.setReducerClass(Reduce.class);
-
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(NullWritable.class);
-
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(IntWritable.class);
-
- FileInputFormat.setInputPaths(job,new Path("hdfs://master:9000/liangwanying/"));
- FileOutputFormat.setOutputPath(job,new Path("hdfs://master:9000/output"));
-
- if(!job.waitForCompletion(true));
- return;
- }
- }
结果截图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。