大数据实战(下)_MapReduce实战

本文详细介绍了一个简单的Hadoop MapReduce实例——Linecount程序的实现过程。该程序用于统计输入文件中的行数,并通过MapReduce框架进行计算。文章提供了完整的Java代码示例,包括Mapper、Reducer和Driver模块的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大纲

  • 演示实例讲解
  • 演示编写MapReduce实例

MapReduce代码

创建 linecount Java 项目
代码如下:

package com.trendwise.java;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
public class Linecount {
    //This is the Mapper Module, i.e. Map.java
    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
        private final static IntWritable obj = new IntWritable(1);
        private Text words = new Text("Total Lines are");

        public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
                throws IOException {
            output.collect(words, obj);
        }
    }
    // This is the Reducer Module, i.e. Reduce.java
    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
        public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output,
                Reporter reporter) throws IOException {
            int sum1 = 0;
            while (values.hasNext()) {
                sum1 += values.next().get();
            }
            output.collect(key, new IntWritable(sum1));
        }
    }
    //This is the Driver Module
    public static void main(String[] args) throws Exception {
        JobConf config = new JobConf(Linecount.class);
        config.setJobName("Linecount");
        config.setOutputKeyClass(Text.class);
        config.setOutputValueClass(IntWritable.class);
        config.setMapperClass(Map.class);
        config.setCombinerClass(Reduce.class);
        config.setReducerClass(Reduce.class);
        config.setInputFormat(TextInputFormat.class);
        config.setOutputFormat(TextOutputFormat.class);
        FileInputFormat.setInputPaths(config, new Path(args[0]));
        FileOutputFormat.setOutputPath(config, new Path(args[1]));
        JobClient.runJob(config);
    }
}

创建Java类,在一个类里面创建MapReduce程序,有利于直观查看代码,不利于java代码的管理,功能多就会混乱,对于简单测试环境可以使用。
eclipse,可以安装MapReduce插件便于本地化来开发测试

http://www.cnblogs.com/baixl/p/4154429.html 来自白大虾博客园hadoop2x版本
http://www.cnblogs.com/edisonchou/p/4297521.html 来自博客园另一位大神 hadoop1x版本
来自网易云课堂

### 关于Hadoop大数据项目的MapReduce实战案例 #### 词频统计项目简介 在Hadoop生态系统中,词频统计是一个经典的入门级MapReduce应用实例。此应用程序读取大量文本文件作为输入,并计算每个单词出现的次数。 #### 数据准备阶段 为了执行词频统计任务,首先需要准备好待分析的大规模文本数据集。这些文本通常会被放置在一个或多个位于HDFS上的目录内[^1]。 ```bash hdfs dfs -mkdir /input_data hdfs dfs -put local_text_files/* /input_data/ ``` #### 编写Mapper类 Mapper负责解析每一行记录并将每一对<key, value>发送给Reducer,在这里就是把每一个单词映射成键值对形式`<word, 1>`: ```java public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer itr = new StringTokenizer(line.toLowerCase()); while (itr.hasMoreTokens()) { word.set(itr.nextToken().replaceAll("[^a-z]", "")); if (!word.toString().isEmpty()){ context.write(word, one); } } } } } ``` #### 编写Reducer类 Reducer接收来自不同Mappers的结果并对相同Key对应的Values求和得到最终结果: ```java public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); @Override protected 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); } } ``` #### 提交作业至集群运行 完成上述编码工作之后就可以通过命令行提交该程序到Hadoop集群上运行了。在此之前可能还需要根据实际情况调整一些配置项比如内存分配等参数以确保最佳性能表现[^2]: ```bash hadoop jar your_wordcount_jar_file.jar com.example.WordCount /input_data /output_results ``` #### 结果验证与查看 最后可以在指定输出路径下找到由Reducer产生的部分文件,里面包含了所有被统计过的词语及其对应数量的信息。可以通过如下方式浏览前几条记录: ```bash hdfs dfs -cat /output_results/part-r-* | head -n 5 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值