MapReduce初级编程实践

一、实验目的

  • 通过实验掌握基本的MapReduce编程方法;
  • 掌握用MapReduce解决一些常见的数据处理问题,包括数据去重、数据排序和数据挖掘等。

二、实验平台

  • 操作系统:Linux
  • Hadoop版本:2.6.0

三、实验步骤

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

对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C。下面是输入文件和输出文件的一个样例供参考。

输入文件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
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class mr {
    public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            context.write(value, 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();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fs = FileSystem.get(conf);
        Job job = Job.getInstance(conf, "Merge and duplicate removal");
        job.setJarByClass(mr.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setInputFormatClass(TextInputFormat.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input1/A"));
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input1/B"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output1"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

在这里插入图片描述

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

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

注意:hadoop setCombinerClass 与 setReducerClass同时使用会存在的问题

如果同时使用这两个类会造成 reduce 输出的 key value会当成 map 阶段的输出 key value 再次输入到 reduce 中进行处理。

import com.amazonaws.services.dynamodbv2.xspec.S;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class mr {
    public static class TokenizerMapper extends Mapper<Object, Text, IntWritable, IntWritable> {
        public static IntWritable data = new IntWritable();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            if (str != null && !str.equals("")) {
                data.set(Integer.parseInt(str));
                context.write(data, new IntWritable(1));
            }
        }
    }

    public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
        public static IntWritable rank = new IntWritable(1);

        public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            for (IntWritable num : values) {
                context.write(rank, key);
                rank = new IntWritable(rank.get() + 1);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fs = FileSystem.get(conf);

        Job job = Job.getInstance(conf, "Sort");
        job.setJarByClass(mr.class);
        job.setMapperClass(TokenizerMapper.class);
//        job.setCombinerClass(Reduce.class);
//        注意此处不能定义Combiner,详情可查看https://www.cnblogs.com/duking1991/p/6069048.html
        
        job.setReducerClass(Reduce.class);

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

        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        job.setInputFormatClass(TextInputFormat.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input2/A"));
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input2/B"));
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input2/C"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output2"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

在这里插入图片描述

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

下面给出一个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
import com.amazonaws.services.dynamodbv2.xspec.S;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class mr {
    public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {
        public static IntWritable data = new IntWritable();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            if (str != null && !str.equals("") && !str.equals("child parent")) {
                String[] fa = str.split("\t");
                String son = fa[0], parent = fa[1];
                context.write(new Text(parent), new Text("son" + son));
                context.write(new Text(son), new Text("fa" + parent));
            }
        }
    }

    public static class Reduce extends Reducer<Text, Text, Text, Text> {
        public static boolean flag = false;

        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            if (!flag) {
                context.write(new Text("grandson"), new Text("grandparent"));
                flag = true;
            }

            ArrayList<Text> grandChild = new ArrayList<Text>();
            ArrayList<Text> grandParent = new ArrayList<Text>();

            for (Text val : values) {
                String s = val.toString();
                if (s.startsWith("son")) {
                    grandChild.add(new Text(s.substring(3)));
                } else {
                    grandParent.add(new Text(s.substring(2)));
                }
            }

            for (Text gc : grandChild)
                for (Text gp : grandParent)
                    context.write(gc, gp);

        }
    }

    public static void main(String[] args) throws Exception {
        String[] a = {"a", "b"};
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fs = FileSystem.get(conf);

        Job job = Job.getInstance(conf, "merge and duplicate removal");
        job.setJarByClass(mr.class);
        job.setMapperClass(TokenizerMapper.class);

        job.setReducerClass(Reduce.class);

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

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setInputFormatClass(TextInputFormat.class);
        FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input3/A"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output3"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

在这里插入图片描述

四、 实验总结及问题

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

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值