MapReduce编程初级实践

本文介绍了MapReduce编程的初级实践,涵盖文件合并与去重、数据排序及信息挖掘。通过实验,学习如何使用MapReduce解决数据处理问题,包括在Hadoop环境中上传、复制和删除文件,以及编写程序实现对输入文件的排序和表格信息挖掘。

MapReduce编程初级实践

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

1.编程实现文件合并和去重操作

参考 MapReduce编程初级实践
记住要让A.txt和B.txt都放在 usr/local/hadoop/input 里面,然后确保 usr/local/hadoop 下面没有output这个文件
如下图所示:
在这里插入图片描述

1.上传文件到hadoop
hadoop fs -put scr dst
2.复制文件到hadoop
hadoop fs -copyFromLocal -f scr dst
3.删除hadoop的某个文件
hadoop fs -rm file
4.删除hadoop的某个文件及里面的文件和文件夹(递归删除)
hadoop fs -rm -r file

2. 编写程序实现对输入文件的排序

参考 MapReduce编程初级实践
记住要让1.txt和2.txt都放在 usr/local/hadoop/input 里面,然后确保 usr/local/hadoop 下面没有 output 这个文件

此处省略图片

3. 对给定的表格进行信息挖掘

参考 Hadoop实验——MapReduce编程
记住要让child-parent.txt都放在 usr/local/hadoop/input3 里面,然后确保 usr/local/hadoop 下面没有 output3 这个文件
此处也有图

### MapReduce 编程初级实践指南 #### 实验目标 - 理解MapReduce的基本概念和工作原理。 - 掌握使用新旧Java API编写MapReduce程序的方法。 - 能够在Hadoop环境中运行简单MapReduce任务,如WordCount。 #### 实验环境准备 1. **安装Hadoop**:确保已安装Hadoop,并配置好环境变量。可以使用伪分布式模式进行测试[^3]。 2. **启动Hadoop集群**: - 格式化文件系统:`$ bin/hdfs namenode -format` - 启动NameNode和DataNode守护进程:`$ sbin/start-dfs.sh` - 启动ResourceManager和NodeManager:`$ sbin/start-yarn.sh` #### 新旧Java API对比 1. **旧API (org.apache.hadoop.mapred)**: - 使用实现接口的方式编写Mapper和Reducer。 - 示例代码片段: ```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)**: - 使用继承抽象基类的方式编写Mapper和Reducer。 - 示例代码片段: ```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配置文件来优化性能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值