MapReduce的KeyValueTextInputFormat
来源:互联网 发布:编程教育 编辑:程序博客网 时间:2024/06/05 09:06
如果行中有分隔符,那么分隔符前面的作为key,后面的作为value;如果没有分隔符,那么整行作为key,value为空
当输入数据的每一行是两列,并用tab分离的形式的时候,KeyValueTextInputformat处理这种格式的文件非常适合。
代码示例:
package com.bigdata.hadoop.mapred;import java.io.IOException;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class MyKeyValueTextInputFormatApp {private static final String INPUT_PATH = "hdfs://hadoop1:9000/dir1/hello";private static final String OUTPUT_PATH = "hdfs://hadoop1:9000/dir1/out";public static void main(String[] args) throws Exception {Configuration configuration = new Configuration();//分隔符默认是\tconfiguration.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, "\t");Job job = new Job(configuration,MyKeyValueTextInputFormatApp.class.getSimpleName());final FileSystem fileSystem = FileSystem.get(new URI(OUTPUT_PATH), configuration);fileSystem.delete(new Path(OUTPUT_PATH),true);job.setJarByClass(MyKeyValueTextInputFormatApp.class);FileInputFormat.setInputPaths(job, INPUT_PATH);//指定使用KeyValueTextInputFormat解析内容 分隔key和value的分隔符默认是\tjob.setInputFormatClass(KeyValueTextInputFormat.class);job.setMapperClass(MyMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(LongWritable.class);job.setNumReduceTasks(0);FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));job.waitForCompletion(true);}//做简单输出public static class MyMapper extends Mapper<Text, Text, Text, LongWritable>{@Overrideprotected void map(Text key, Text value,Mapper<Text, Text, Text, LongWritable>.Context context)throws IOException, InterruptedException {context.write(new Text(key), new LongWritable(1));context.write(new Text(value), new LongWritable(1));}}}
0 0
- MapReduce的KeyValueTextInputFormat
- MapReduce的输入格式---KeyValueTextInputFormat---源码分析
- KeyValueTextInputFormat 重写
- KeyValueTextInputFormat实例
- KeyValueTextInputFormat用法
- KeyValueTextInputFormat 分割key value For hadoop 1.0
- KeyValueTextInputFormat 分割key value For hadoop 1.0
- Hadoop Problem : hadoop0.20.2中的KeyValueTextInputFormat
- hadoop 之 InputFormat类 --- KeyValueTextInputFormat 实例
- 基于MapReduce作业的MapReduce数据流优化
- MapReduce(三): MapReduce的主要功能模块
- MapReduce作业的MapReduce数据流优化
- MapReduce: 提高MapReduce性能的建议(1)
- MapReduce进阶:多MapReduce的链式模式
- mapreduce机制及mapreduce任务的分析
- 【MR】经典的MapReduce(MapReduce 1) 运行机制
- Google的MapReduce
- MapReduce的设计思想
- WSAEventSelect模型
- HTML5基础03----HTML5元素简介及使用方法
- WebRTC
- HDU3466
- 每天一个Linux命令-10(Linux find xargs详解)
- MapReduce的KeyValueTextInputFormat
- 最大子序列和的四种求解算法及其时间比较
- win7CMD route配置内外网双网卡操作
- HDU5308-脑补-对拍
- C语言结构体
- LTE调度算法(下行)
- bzoj3172: [Tjoi2013]单词
- C语言统计不同字符的字数
- Android 多分辨率自适应总结