KeyValueTextInputFormat 案例实现| 学习笔记

简介: 快速学习 KeyValueTextInputFormat 案例实现

开发者学堂课程【Hadoop 分布式计算框架 MapReduc:KeyValueTextInputFormat 案例实现】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/94/detail/1505


KeyValueTextInputFormat案例实现


目录:

一、编写 Mapper 类

二、编写 Reducer 类

三、编写 Driver 类

 

一、编写 Mapper 类

package com. atguigu . mapreduce . KeyValueTextInputFormat;.import java. io. IOException;·

import org . apache . hadoop. io. LongWritable;·

import org . apache . hadoop. io. Text;.

import org. apache . hadoop . mapreduce . Mapper;.

public classKVTextMapper extends Mapper<Text, Text,Text,

LongWritable>

//  1  设置 value.

LongWritable V = new LongWritable (1) ;


二、编写 Reducer 类

package com. atguigu . mapreduce . KeyValueTextInputFormat;.import java. io. IOException;·

import org . apache . hadoop. io. LongWritable;·

import org . apache . hadoop. io. Text;.

import org. apache . hadoop . mapreduce . Mapper;.

public classKVTextMapper extends Mapper<Text, Text,Text,

LongWritable>

LongWritable V = new LongWritable (1)

@override,

protected voidmap (Text key, Text value,Context context)

Reducer<Text, IntWritable, Text, IntWritable> . Context context) throws IOException, InterruptedException

< banzhang,1>

< banzhang,1>

int sum = 0;

// 1累加求和

for (IntWritable value : values)

sum += value. get();]

v. set(sum);

// 2 写出

context .write(key, v);


三、编写 Driver 类

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class KVTextDriver (

public static void main(String[] args) throws IOException, ClassNotFoundException, Interrup args = new String[]f"e:/input/inputkv", "e:/output1");

Configuration conf = new Configuration();

conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, "卜);

// 1获取 job 对象 I

Job job = Job.getInstance(conf);

// 2设置 jar 存储路径

job.setJarByClass(KVTextDriver.class);

// 3 关联 mapper 和 reducer 类

job.setMapperClass(KVTextMapper.class);

job.setReducerClass(KVTextReducer.class);

//4设置 mapper 输出的 key 和 value 类型

job.setMap0utputKeyClass(Text.class);

// 5 设置最终输出的 key 和 value 类型

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

job.setInputFormatClass(KeyValueTextInputFormat.class);

// 6设置输入输出路径

FileInputFormat.setInputPaths(job, new Path(args[0]));File0utputFormat.setOutputPath(job, new Path(args[1]));

// 7提交 job

boolean result = job.waitForCompletion(true);

相关文章
|
4月前
|
Python
如何使用Python的Pandas库进行数据缺失值处理?
Pandas在Python中提供多种处理缺失值的方法:1) 使用`isnull()`检查;2) `dropna()`删除含缺失值的行/列;3) `fillna()`用常数、前/后一个值填充;4) `interpolate()`插值填充。根据需求选择合适的方法处理数据缺失值。
67 0
|
人工智能 达摩院 物联网
应用案例|学习笔记
快速学习应用案例
102 0
应用案例|学习笔记
|
SQL 安全 Java
案例介绍|学习笔记
快速学习案例介绍
案例介绍|学习笔记
|
开发者
案例_1|学习笔记
快速学习案例_1
案例_1|学习笔记
|
缓存 网络架构 开发者
案例_2|学习笔记
快速学习案例_2
案例_2|学习笔记
|
机器学习/深度学习 Kubernetes 安全
案例实战二| 学习笔记
快速学习案例实战二。
106 0
案例实战二| 学习笔记
|
Kubernetes 安全 应用服务中间件
案例实战| 学习笔记
快速学习案例实战。
125 0
案例实战| 学习笔记
|
SQL HIVE 开发者
SearchType 案例介绍|学习笔记
快速学习 SearchType 案例介绍。
|
JSON 算法 数据格式
SearchType 案例_介绍 | 学习笔记
快速学习 SearchType 案例_介绍
105 0
|
存储 人工智能 达摩院
应用案例| 学习笔记
快速学习应用案例