当前位置:   article > 正文

大数据技术原理与应用实验报告--关系代数运算并行化_求关系r与关系s的交集。 假设有关系r与关系s,他们模式一致,要求设计mapreduce程序

求关系r与关系s的交集。 假设有关系r与关系s,他们模式一致,要求设计mapreduce程序

一、实验内容

求关系R与关系S的交集。

假设有关系R与关系S,他们模式一致,要求设计MapReduce程序,找出两个关系中完全相同的记录,输出的每行是一条记录。

关系R与关系S的模式为:name,age,gender

(注:R和S的数据不放出来了,用自己的数据即可)

二、实验环境--Hadoop集群

三、实验过程

注意数据与数据间是用逗号隔开还是空格隔开

改第一段代码这里

String[] strs = ivalue.toString().split(",");//这里我原本的数据是使用逗号隔开,如果你的是空格的话,就删掉逗号

  1. package org.zkpk.hadoop.hdfs;
  2. import java.io.IOException;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.LongWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Mapper;
  7. public class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
  8. public void map(LongWritable ikey,Text ivalue,Context context)
  9. throws IOException,InterruptedException{
  10. String[] strs = ivalue.toString().split(",");
  11. if(strs.length==3){
  12. context.write(ivalue, new IntWritable(1));
  13. }
  14. }
  15. }
  1. package org.zkpk.hadoop.hdfs;
  2. import java.io.IOException;
  3. import org.apache.hadoop.io.IntWritable;
  4. import org.apache.hadoop.io.NullWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Reducer;
  7. public class Reduce extends Reducer<Text,IntWritable,Text,NullWritable>{
  8. public void reduce(Text key,Iterable<IntWritable> values,Context context)
  9. throws IOException,InterruptedException{
  10. int count = 0;
  11. for(IntWritable value:values){
  12. ++count;
  13. }
  14. if(count == 2)
  15. context.write(key, NullWritable.get());
  16. }
  17. }
  1. package org.zkpk.hadoop.hdfs;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.NullWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  10. public class Dir {
  11. public static void main(String[] args) throws Exception{
  12. Configuration conf =new Configuration();
  13. Job job = Job.getInstance(conf);
  14. job.setJarByClass(Dir.class);
  15. job.setMapperClass(Map.class);
  16. job.setReducerClass(Reduce.class);
  17. job.setOutputKeyClass(Text.class);
  18. job.setOutputValueClass(NullWritable.class);
  19. job.setMapOutputKeyClass(Text.class);
  20. job.setMapOutputValueClass(IntWritable.class);
  21. FileInputFormat.setInputPaths(job,new Path("hdfs://master:9000/liangwanying/"));
  22. FileOutputFormat.setOutputPath(job,new Path("hdfs://master:9000/output"));
  23. if(!job.waitForCompletion(true));
  24. return;
  25. }
  26. }

 结果截图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/650800
推荐阅读
相关标签
  

闽ICP备14008679号