大数据实验 实验五:MapReduce 初级编程实践

本文详细介绍了在Hadoop环境下,使用MapReduce编程技术实现文件合并、去重操作以及对输入文件的排序,包括Java代码示例和问题解决过程。

大数据实验 实验五:MapReduce 初级编程实践

实验环境

  • 操作系统 centos7
  • Hadoop版本:3,3,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


创建文件A.txt和B.txt
在这里插入图片描述
将两个文件上传到HDFS中

在这里插入图片描述
Java程序

package Main;

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;

import java.io.IOException;

public class main {
   
   
    //重载map函数,直接将输入中的value复制到输出数据的key上
    public static class Map extends Mapper<Object, Text, Text, Text> {
   
   
        private static Text text = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
   
   
            text = value;
            context.write(text, new Text(""));
        }
    }

    //重载reduce函数,直接将输入中的key复制到输出数据的key上
    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");
        conf.set("fs.defaultFS", "hdfs://localhost:8020");
        String[] otherArgs = new String[]{
   
   "/input/test", "/output/test"};
        if (otherArgs.length != 2) {
   
   
            System.err.println("Usage: wordcount <in><out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "Merge and duplicate removal");
        job.setJarByClass(main.class);
        job.setMapperClass(Map.class);
        job.
### MapReduce 编程初级实践指南 #### 实验目标 - 理解MapReduce的基本概念工作原理。 - 掌握使用新旧Java API编写MapReduce程序的方法。 - 能够在Hadoop环境中运行简单的MapReduce任务,如WordCount。 #### 实验环境准备 1. **安装Hadoop**:确保已安装Hadoop,配置好环境变量。可以使用伪分布式模式进行测试[^3]。 2. **启动Hadoop集群**: - 格式化文件系统:`$ bin/hdfs namenode -format` - 启动NameNodeDataNode守护进程:`$ sbin/start-dfs.sh` - 启动ResourceManagerNodeManager:`$ sbin/start-yarn.sh` #### 新旧Java API对比 1. **旧API (org.apache.hadoop.mapred)**: - 使用实现接口的方式编写MapperReducer。 - 示例代码片段: ```java public class OldMapReduceExample { public static class MyMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { // Mapper逻辑 } public static class MyReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { // Reducer逻辑 } } ``` 2. **新API (org.apache.hadoop.mapreduce)**: - 使用继承抽象基类的方式编写MapperReducer。 - 示例代码片段: ```java public class NewMapReduceExample { public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> { // Mapper逻辑 } public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { // Reducer逻辑 } } ``` #### WordCount 实验教程 1. **编写Mapper类**: - 输入键值对为`(LongWritable, Text)`,输出键值对为`(Text, IntWritable)`。 - 代码示例: ```java public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split("\\s+"); for (String w : words) { word.set(w); context.write(word, one); } } } ``` 2. **编写Reducer类**: - 输入键值对为`(Text, Iterable<IntWritable>)`,输出键值对为`(Text, IntWritable)`。 - 代码示例: ```java public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } ``` 3. **编写Driver类**: - 配置Job设置Mapper、Reducer类。 - 代码示例: ```java public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } ``` 4. **编译打包**: - 使用Maven或Ant工具将代码编译打包成JAR文件。 5. **运行MapReduce任务**: - 将输入文件上传到HDFS。 - 运行JAR文件:`hadoop jar your-jar-file.jar WordCount input-path output-path` 6. **查看结果**: - 使用HDFS命令查看输出目录中的结果文件:`hadoop fs -cat output-path/part-r-00000` #### 注意事项 - 在编写MapReduce程序时,注意处理异常关闭资源。 - 确保输入输出路径正确无误。 - 可以通过调整Hadoop配置文件来优化性能。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值