MapReduce打包运行

1. 编写 MapReduce 程序

首先需要编写 MapReduce 程序,通常包含 Mapper、Reducer 和 Driver 类。例如,一个简单的 WordCount 程序:

java

复制代码
import java.io.IOException;
import java.util.StringTokenizer;

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

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();

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    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);
        }
    }

    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);
    }
}

2. 创建 Maven 项目(推荐)

使用 Maven 管理依赖和打包,pom.xml示例:

xml

复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mapreduce-example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <hadoop.version>3.3.6</hadoop.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>WordCount</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3. 打包项目

使用 Maven 命令打包:

bash

复制代码
mvn clean package

这将生成一个包含所有依赖的 JAR 文件(通常位于target/mapreduce-example-1.0-SNAPSHOT.jar)。

4. 上传输入数据到 HDFS

假设输入文件为input.txt,上传到 HDFS:

bash

复制代码
hdfs dfs -mkdir -p /user/hadoop/input
hdfs dfs -put input.txt /user/hadoop/input/

5. 运行 MapReduce 作业

使用hadoop jar命令提交作业:

bash

复制代码
hadoop jar target/mapreduce-example-1.0-SNAPSHOT.jar WordCount /user/hadoop/input /user/hadoop/output
  • 参数说明
    • target/mapreduce-example-1.0-SNAPSHOT.jar:打包后的 JAR 文件路径。
    • WordCount:主类名(包含main方法的类)。
    • /user/hadoop/input:HDFS 输入路径。
    • /user/hadoop/output:HDFS 输出路径(需不存在,系统会自动创建)。

6. 查看结果

bash

复制代码
hdfs dfs -cat /user/hadoop/output/part-r-00000
相关推荐
Elastic 中国社区官方博客9 分钟前
Kibana 数据可视化的新配色方案 —— 我们如何以及为什么创建它
大数据·elasticsearch·搜索引擎·信息可视化·全文检索·kibana
福客AI智能客服33 分钟前
智能客服机器人:家居建材电商的场景化服务核心
大数据·人工智能·机器人
TG:@yunlaoda360 云老大1 小时前
如何评估华为云国际站代理商跨境合规要求?
大数据·数据库·华为云·云计算
CHrisFC1 小时前
汽车零配件检测实验室LIMS系统应用实践
大数据·人工智能·汽车
TG:@yunlaoda360 云老大1 小时前
如何了解华为云国际站代理商的GACS主要有什么作用呢?
大数据·华为云·云计算
CES_Asia1 小时前
立即行动,锁定2026增长引擎:报名CES Asia机器人展,同步押注“具身智能”与亚洲市场
大数据·人工智能·百度·机器人
青软青之LIMS1 小时前
破孤岛 统全局 智未来:King‘s LIMS集团版引领实验室数智化协同新生态
大数据·实验室信息管理系统·实验室数智化管理系统·实验室数字化管理系统·实验室综合管理平台
KG_LLM图谱增强大模型1 小时前
【102页最新综述】AI智能体时代的记忆系统:形式、功能与知识图谱长记忆动态机制全景解析
大数据·人工智能·agent
jkyy20141 小时前
从菜品识别到健康决策:AI技术如何赋能B端智慧饮食管理
大数据·人工智能·科技·健康医疗
Deepoch1 小时前
从“功能机”到“智能体”:服务机器人的认知革命与产业重构
大数据·人工智能·科技·机器人·未来·具身模型·deepoc