(大数据方向)分布式实验六:mapreduce编程实践

注意:此次编程实践,在伪分布式上实践,所以首先需要启动伪分布式的节点

进阶任务代码已上传资源中

start-dfs.sh

目录

一.新建项目

 二.编写代码(包:com.jxxy.mr.test)

(1)WordCount主类

(2) MyMapper类

三.制作jar包

 四.运行程序


 

一.新建项目

使用xftp导入相关配置文件

配置文件 mapred-site.xml

yarn-site.xml

hdfs-site.xml core-site.xml
jar包 hadoop_jars(在实验eclipse中已创建好)
                                使用xftp导入到自己所建项目的src目录下

 二.编写代码(包:com.jxxy.mr.test)

(1)WordCount主类

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.Text;
### 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
发出的红包

打赏作者

Java从跨平台到跨行业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值