Mapreduce小试牛刀(2)--java api

1.同hdfs 的java api,我们首先要在IDE中建立一个maven项目

pom.xml中配置如下:

复制代码
<dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-common</artifactId>
      <version>3.3.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
      <version>3.3.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>3.3.4</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.30</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  1. 编写java 代码,重写map与reduce类(我们以最经典的wordcount为例)

第一步,建立java目录,建立一个叫test的包(可写可不写,看自己喜好)

第二步,导入hadoop包,以及其他所需的包

复制代码
package test;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;

第三步,重写map,resuce方法

1.map方法,目的在于切割文章内容为一对对键值对,实现map-reduce中的map过程

复制代码
class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
    //前两个为输入类型,第一个数据类型表示首行偏移量;后两个为输出类型,输出为键值对类型
    private final static IntWritable one = new IntWritable(1);
    //IntWritable实例对象初始化为1
    private Text word = new Text();
    public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
        String line = value.toString();
        //将待测信息转化为字符串
        StringTokenizer tokenizer = new StringTokenizer(line);
        //以空格区分
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            output.collect(word, one);
        }
    }
}

2.reduce方法,目的在于处理map方法切割过的数据,实现reduce过程

复制代码
class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
    //前两个为map输出结果类型,后两个为reduce输出结果
    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
        //开始计数,为每一个键统计对应的值
        int sum = 0;
        while (values.hasNext()) {
            sum += values.next().get();
        }
        output.collect(key, new IntWritable(sum));
    }
}

3.编写主函数,利用作业类引入map与reduce类

复制代码
public class wordcount {
    public static void main(String[] args) throws Exception {
        JobConf conf = new JobConf(wordcount.class);
        conf.setJobName("my_word_count");
        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);
        conf.setMapperClass(Map.class);
        conf.setCombinerClass(Reduce.class);
        conf.setReducerClass(Reduce.class);
        conf.setInputFormat(TextInputFormat.class);
        conf.setOutputFormat(TextOutputFormat.class);
        FileInputFormat.setInputPaths(conf, new Path(args[0]));
        FileOutputFormat.setOutputPath(conf, new Path(args[1]));
        JobClient.runJob(conf);
    }
}

4.打包为jar文件

filr -> project structure -> artifacts -> + -> jar

按java包的结构,新建dictionary,必须一模一样!然后在合适的包下面,导入这三个文件

完成配置后,点击ok,再次点击Build Artifacts

完成后,在output dictionary中设置的地址中可以找到jar包,比如我设置的是桌面,那么就将在桌面找到jar包


3.导入hadoop主虚拟机

使用xshell中rz指令,将jar包导入到虚拟机中,输入ls检查是否上传成功

确保hdfs目录中已经存在待检测的文件,在这里我的待测文件名叫做input,jar包所在的位置是/home/hadoop,所以对应的指令就是:

复制代码
hadoop jar /home/hadoop/wordcount.jar test.wordcount /input /output

因为我的jar包没有打包MANIFEST.MF文件,所以必须要使用test.wordcount即包名+主类名的方法来定位主类,如果你上一步已经打包了,那就无需这个指令。结果将输出hdfs中output文件夹下,所以执行这一步之前必须保证hdfs中不存在一个叫ouput的文件夹。可以采用50070端口监控文件

使用cat命令查看统计结果:

复制代码
hadoop fs -cat /output/part-00000

这里的part-00000是output文件夹中统计文件文件名,以自己ouput文件夹中文件名为准

那么,这就代表着我们的程序运行正确且成功了!

相关推荐
西岸行者7 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意7 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码7 天前
嵌入式学习路线
学习
毛小茛7 天前
计算机系统概论——校验码
学习
babe小鑫7 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms7 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下7 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。7 天前
2026.2.25监控学习
学习
im_AMBER7 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J7 天前
从“Hello World“ 开始 C++
c语言·c++·学习