当前位置: 首页 > 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
http://www.dtcms.com/a/187695.html

相关文章:

  • 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的使用
  • 使用DevEco Studio性能分析工具高效解决鸿蒙原生应用内存问题
  • 免费专业级 PDF 处理!SolidPDF OCR 识别 + 精准转换批量处理
  • 【源码+文档+调试讲解】驾校报名小程序2
  • 嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
  • 在scala中sparkSQL连接masql并添加新数据
  • 分割一切(SAM) 论文阅读:Segment Anything
  • c++ std库中的文件操作学习笔记
  • QEMU模拟32位ARM实现自定义系统调用
  • CodeBuddy 中国版 Cursor 实战:Redis+MySQL双引擎驱动〈王者荣耀〉战区排行榜
  • RAG之大规模解析 PDF 文档全流程实战