当前位置: 首页 > news >正文

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 TokenizerMapperextends 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 IntSumReducerextends 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

相关文章:

  • JavaEE--初识网络
  • OCR:开启财务数字化变革的魔法钥匙
  • 提示词设计模板(基于最佳实践)
  • springboot3+vue3融合项目实战-大事件文章管理系统-获取文章分类详情
  • BFS算法篇——从晨曦到星辰,BFS算法在多源最短路径问题中的诗意航行(上)
  • 【Android】下拉刷新组件Swiperefreshlayout
  • 力扣算法---总结篇
  • 分式注记种表达方式arcgis
  • Qubes os系统详解
  • Leetcode 3542. Minimum Operations to Convert All Elements to Zero
  • Android之横向滑动列表
  • 每日算法刷题计划Day5 5.13:leetcode数组3道题,用时1h
  • mac 10.15.7 svn安装
  • 《内网渗透测试:绕过最新防火墙策略》
  • C#高级编程:设计模式原则
  • 数据分析预备篇---Pandas的Series
  • langChain存储文档片段,并进行相似性检索
  • 【神经网络与深度学习】通俗易懂的介绍非凸优化问题、梯度消失、梯度爆炸、模型的收敛、模型的发散
  • GITLAB跑gradle项目 不借助maven-publish直接上传到nexus私人仓库
  • C++STL——map和set的使用
  • 牛市早报|中方调整对美加征关税措施,五部门约谈外卖平台企业
  • 人才争夺战,二三线城市和一线城市拼什么?洛阳官方调研剖析
  • 郑州通报涉“健康证”办理有关问题查处进展情况
  • 民企老板被错羁212天续:申请国赔千万余元,要求恢复名誉赔礼道歉
  • 韩国总统大选候选人登记结束,共7人参选
  • 著名文物鉴赏家吴荣光逝世,享年78岁