hadoop实例程序——wordcount以jar包形式执行

本文介绍如何使用Maven构建并打包Hadoop项目,包括配置Job、Mapper和Reducer类,最终生成可部署到Hadoop集群的jar包。

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

使用Maven构建项目方便打包
项目结构

wordcount.java`

package mr;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class wordcount {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        // Job 类是一个单例模式:
        Job job = Job.getInstance(conf);  // job -> yarn 生成job
        //打包为jar去运行
        job.setJarByClass(wordcount.class);

        // 分别指定Mapper 和Reducer 是哪个类
        job.setMapperClass(WordsMapper.class);
        job.setReducerClass(WordsReducer.class);
        // 制定 Mapper 的输出Key 和 Value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        // 指定 Reducer的输出Key 和Value 的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // 设置输入的文件
        Path inputPath = new Path(args[0]);
        // 设置输出的路径 : 注意这个路径一定不要存在
        Path outputPath = new Path(args[1]);
        
        FileInputFormat.setInputPaths(job,inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);


        // 执行 true -> console打印信息,  false-> 不打印
        job.waitForCompletion(true);
    }
}

WordsMapper.java

package mr;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WordsMapper extends Mapper<LongWritable,Text,Text,LongWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] strs = value.toString().split(" ");
        for(String s : strs) {
            context.write(new Text(s),new LongWritable(1));
        }
    }
}

WordsReducer.java

package mr;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

class WordsReducer extends Reducer<Text, LongWritable,Text,LongWritable> {

    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long count = 0; // 用来计算每一个单词所出现的次数
        for (LongWritable value : values) {
            count = count + value.get();
        }
        context.write(key, new LongWritable(count));
    }
}

使用Maven插件打包

控制台出现BUILD SUCCESS即为打包成功
在这里插入图片描述
打包成功后,jar包会出现在target目录下

右键点击 Show in Explorer 打开jar包所在位置

将此jar包通过SFTP工具上传至Hadoop master虚拟机上

1、首先启动hadoop,start-all.sh(必须正常运行)
2、新建hdfs文件夹 input

hdfs dfs -mkdir -p /input

3、上传word.txt文件至 /input 文件夹下

hdfs dfs -put word.txt /input

4、运行wordcount实例

  1. 首先进入存放jar包的文件夹

  2. 执行命令

 hadoop jar Hadoop-1.0-SNAPSHOT.jar mr.wordcount /input /output

jar包名称根据自己的来,mr.wordcount是 包名.类名

  1. 回车运行
    在这里插入图片描述
    出现successfully,即为运行成功
  2. 查看hdfs文件 、output(运行成功自动生成)
    在这里插入图片描述
    使用cat命令查看 part-r-00000 文件内容
    在这里插入图片描述
    执行wordcount成功
### Hadoop WordCount MapReduce 实现 Hadoop中的WordCount是一个经典的MapReduce示例,用于统计文本文件中单词的频率。以下是完整的实现过程以及代码。 #### 1. 输入数据准备 假设有一个简单的输入文件 `input.txt`,其内容如下: ``` hello world hello hadoop map reduce word count example ``` 该文件会被放置到HDFS上的某个目录下,例如 `/input/wordcount/input.txt`[^1]。 --- #### 2. 编写Mapper类 Mapper的任务是从输入记录中提取键值对 `(key, value)`。对于WordCount来说,每行被拆分为多个单词,每个单词作为键,而值始终为1。 ```java import java.io.IOException; import org.apache.hadoop.mapreduce.Mapper; public class WordCountMapper 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(); String[] words = line.split("\\s+"); for (String w : words) { word.set(w); context.write(word, one); // 输出 (word, 1) } } } ``` --- #### 3. 编写Reducer类 Reducer接收来自Mapper的中间结果并执行聚合操作。在这里,它计算每个单词出现的总次数。 ```java import java.io.IOException; import org.apache.hadoop.mapreduce.Reducer; public class WordCountReducer extends Reducer<Text, IntWritable, Text, 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(); // 对所有值求和 } context.write(key, new IntWritable(sum)); // 输出 (word, total_count) } } ``` --- #### 4. 配置Job驱动程序 编写一个Java类来定义整个MapReduce作业的参数。 ```java import org.apache.hadoop.conf.Configuration; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.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); } } ``` 上述代码中,`args[0]` 是输入路径,`args[1]` 是输出路径。需要注意的是,在运行之前应确保输出路径不存在,否则会抛出异常。 --- #### 5. 运行作业 将编译后的JAR提交给Hadoop集群: ```bash [hadoop@cdh5namenode ~]$ hadoop jar wordcount.jar com.example.WordCountDriver /input/wordcount /output/wordcount_result ``` 完成后可以查看输出结果: ```bash [hadoop@cdh5namenode ~]$ hadoop fs -cat /output/wordcount_result/part-r-00000 ``` 输出可能类似于以下形式: ``` example 1 hadoolp 1 hello 2 map 1 reduce 1 world 1 ``` --- #### 6. YARN配置注意事项 为了使MapReduce作业能够在YARN上正常运行,需正确设置 `yarn-site.xml` 文件中的属性,例如指定辅助服务和资源管理器主机名[^2]。 --- #### 总结 通过以上步骤实现了基于HadoopWordCount MapReduce应用。此案例展示了如何利用Mapper和Reducer处理大规模数据集,并将其结果保存至HDFS。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值