“21 天好习惯”第一期-1

实验三:MapReduce初级编程实践

一、实验目的

  • 通过实验掌握基本的MapReduce编程方法;

  • 掌握用MapReduce解决一些常见的数据处理问题,包括数据去重、数据排序和数据挖掘等。

二、实验平台

  • 操作系统:Linux

  • Hadoop版本:2.6.0

三、实验步骤

(一)编程实现文件合并和去重操作

对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C。下面是输入文件和输出文件的一个样例供参考。(注:输入的时候注意空格,直接复制过去好像有看不见的空格,我复制后去掉了那些空格才去重成功)

去重的思想:利用了reduce输出的key值的唯一性对文件去重,也就是说把map任务的输出全部当作reduce的key,value我这里设置的时空,可以达到以下效果。

输入文件A的样例如下:

20170101	x      
20170102	y      
20170103	x      
20170104	y      
20170105	z  
20170106	x   

输入文件B的样例如下:

20170101	y  
20170102	y  
20170103	x  
20170104	z  
20170105	y  

根据输入文件A和B合并得到的输出文件C的样例如下:

20170101	x  
20170101	y  
20170102	y  
20170103	x  
20170104	y  
20170104	z 
20170105	y      
20170105	z  
20170106	x 
package mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import mapreduce.WordCount.MyMapper;
import mapreduce.WordCount.MyReduce;

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 java.io.IOException;

public class Mapreduce {

	public static class Map extends Mapper<Object, Text, Text, Text> {
		private static Text text = new Text();
		public void map(Object key, Text value, Context content) throws IOException, InterruptedException {
			text = value;
			content.write(text, new Text(""));
		}
	}

	public static class Reduce extends Reducer<Text, Text, Text, Text> {
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
			context.write(key, new Text(""));
		}
	}

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

		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);// 设置环境参数

		job.setJarByClass(Mapreduce.class);// 设置程序别名
		job.setMapperClass(Map.class);// 添加mapper类
		job.setReducerClass(Reduce.class);// 添加Reduce类

		job.setMapOutputKeyClass(Text.class);//设置map的key输出类型
		job.setMapOutputValueClass(Text.class);//设置map的value输出类型

		job.setOutputKeyClass(Text.class); // 设置reduce的key输出类型
		job.setOutputValueClass(Text.class);//设置reduce的value输出类型

		FileInputFormat.setInputPaths(job, new Path("hdfs://hadoop1:8020/data1/*"));
		FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop1:8020/output"));
		try {
			boolean res = job.waitForCompletion(true);
			System.exit(res ? 0 : 1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

image-20211020184056125

(二)编写程序实现对输入文件的排序

现在有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,进行升序排序后,输出到一个新的文件中,输出的数据格式为每行两个整数,第一个数字为第二个整数的排序位次,第二个整数为原待排列的整数。下面是输入文件和输出文件的一个样例供参考。

输入文件1的样例如下:

33
37
12
40

输入文件2的样例如下:

4
16
39
5

输入文件3的样例如下:

1
45
25

根据输入文件1、2和3得到的输出文件如下:

1 1
2 4
3 5
4 12
5 16
6 25
7 33
8 37
9 39
10 40
11 45
package mapreduce;

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.lib.output.FileOutputFormat;

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 java.io.IOException;

public class Mapreduce {

	public static class Map extends Mapper<Object, Text, IntWritable,IntWritable > {
		private static IntWritable data = new IntWritable();
		public void map(Object key, Text value, Context content) throws IOException, InterruptedException {
			String line = value.toString();
			data.set(Integer.parseInt(line));
			content.write(data, new IntWritable(1));
		}
	}

	public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
		private static IntWritable index = new IntWritable(1);
		public void reduce(IntWritable key, Iterable<IntWritable> values, Context content) throws IOException, InterruptedException {
			for(IntWritable num : values){
				content.write(index, key);
				index = new IntWritable(index.get()+1);
			}
		}
	}

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

		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);// 设置环境参数

		job.setJarByClass(Mapreduce.class);// 设置程序别名
		job.setMapperClass(Map.class);// 添加mapper类
		job.setReducerClass(Reduce.class);// 添加Reduce类

		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setOutputKeyClass(IntWritable.class); // 设置输出类型
		job.setOutputValueClass(IntWritable.class);

		FileInputFormat.setInputPaths(job, new Path("hdfs://hadoop1:8020/data1/*"));
		FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop1:8020/output"));
		try {
			boolean res = job.waitForCompletion(true);
			System.exit(res ? 0 : 1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

image-20211021142857219

(三)对给定的表格进行信息挖掘

下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格。

输入文件内容如下:

child	parent
Steven	Lucy
Steven	Jack
Jone	Lucy
Jone	Jack
Lucy	Mary
Lucy	Frank
Jack	Alice
Jack	Jesse
David	Alice
David	Jesse
Philip	David
Philip	Alma
Mark	David
Mark	Alma

输出文件内容如下:

grandchild    grandparent
Steven        Alice
Steven        Jesse
Jone          Alice
Jone          Jesse
Steven        Mary
Steven        Frank
Jone          Mary
Jone          Frank
Philip        Alice
Philip        Jesse
Mark          Alice
Mark          Jesse
package mapreduce;

import java.io.IOException;
import java.util.*;
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;
 
public class WordCount{
    public static class Mymapper extends Mapper<Object, Text, Text, Text>{
        public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
             String[] cap=value.toString().split("[\\s|\\t]+");//分割数据
              if (!"child".equals(cap[0])) {
                  String cName = cap[0];
                  String pName = cap[1];
                  context.write(new Text(pName), new Text("l#"+cName));//打标签
                  context.write(new Text(cName), new Text("r#"+pName));
              }
        }
    }
    public static class Myreduce extends Reducer<Text, Text, Text, Text>{
    	public static int runtime = 0;
        public void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException{
            if (runtime == 0) {
                context.write(new Text("grandchild"), new Text("grandparent"));
                runtime++;
            }
            List<String> grandChild = new ArrayList<>();
            List<String> grandParent = new ArrayList<>();
            for (Text text : values) {
                String[] relation = text.toString().split("#");
                if ("l".equals(relation[0])) {
                    grandChild.add(relation[1]);
                } else {
                    grandParent.add(relation[1]);
                }
            }
            for (String l:grandChild) {
                for (String r:grandParent) {
                	context.write(new Text(l), new Text(r));
                }
            }
        }
    }
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.Mymapper.class);
        job.setReducerClass(WordCount.Myreduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://hadoop1:8020/data1/"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop1:8020/output1"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

image-20211021233220126

四、 实验总结及问题

1、学会使用什么做什么事情;

学会了mapreduce的基本操作以及性质

2、在实验过程中遇到了什么问题?是如何解决的?

去重的时候失败了好多次,最后发现时复制输入文件的时候带了好多看不见的空格进去,手动删除后成功

3、还有什么问题尚未解决?可能是什么原因导致的。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值