我们的输入文件 hello0, 内容如下:

xiaowang 28 shanghai@_@zhangsan 38 beijing@_@someone 100 unknown

逻辑上有3条记录, 它们以@_@分隔.

我们看看数据是如何被map读取的...

1. 默认配置

/*
New API
*/ //conf.set("textinputformat.record.delimiter", "@_@"); /*
job.setInputFormatClass(Format0.class);
//job.setInputFormatClass(Format1.class); error here //or,
job.setInputFormatClass(Format3.class); //job.setInputFormatClass(Format4.class); error here job.setInputFormatClass(Format5.class); */ import java.io.IOException; 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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class Test0 { public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
public void map(Object key, Text value, Context context) throws IOException, InterruptedException
{
String line = value.toString();
System.out.println(line);
}
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf); job.setJarByClass(Test0.class);
job.setJobName("myjob"); job.setMapperClass(MyMapper.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } }

Debug我们可以看到value的值是获取了文件的整个内容作为这一条记录的值的, 因为默认情况下是以换行符作为记录分割符的, 而文件内容中没有换行符. map只被调用1次

2. 配置textinputformat.record.delimiter

我们为Configuration设置textinputformat.record.delimiter参数-

conf.set("textinputformat.record.delimiter", "@_@");

这样map按照我们的预期读取记录, map被调用3次

3. 自定义TextInputFormat

自定义TextInputFormat, 在其RecordReader方法中设置需要的record delimiter

import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; public class Format5 extends TextInputFormat { public RecordReader createRecordReader (InputSplit split, TaskAttemptContext tac) {
byte[] recordDelimiterBytes = "@_@".getBytes();
return new LineRecordReader(recordDelimiterBytes);
} }

应用到job上-

 job.setInputFormatClass(Format5.class);

这样得到和方法2一样的效果.

MapReduce: map读取文件的过程的更多相关文章

  1. 【原创】大数据基础之Spark(7)spark读取文件split过程(即RDD分区数量)

    spark 2.1.1 spark初始化rdd的时候,需要读取文件,通常是hdfs文件,在读文件的时候可以指定最小partition数量,这里只是建议的数量,实际可能比这个要大(比如文件特别多或者特别 ...

  2. linux中为什么删除文件比创建文件要快,读取文件和删除文件的过程是什么?

    一.为什么删除文件比创建文件要快? 因为删除文件只是将bitmap位图表中将文件所占据的inode 和dacablock的使用状态从1变成0,相当于释放了这些快的使用权. 二.读取文件和删除文件的过程 ...

  3. HTML5 文件域+FileReader 读取文件(一)

    在HTML5以前,HTML的文件上传域的功能具有很大的局限性,这种局限性主要体现在如下两点: 每次只能选择一个文件进行上传 客户端代码只能获取被上传文件的文件路径,无法访问实际的文件内容 一.File ...

  4. FileReader读取文件里文乱码问题

    有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=newString(str.getBytes(),"UTF-8");结果大部分中文显 ...

  5. MapReduce剖析笔记之五:Map与Reduce任务分配过程

    在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...

  6. 关于一些对map和整行读取文件操作

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  7. 登录shell与非登录shell读取文件过程

    登录shell与非登录shell读取文件过程登录:/etc/profile→/etc/profile.d/*.sh        ~/.bash_profile非登录:~/.bash_profile→ ...

  8. 【转】MapReduce:详解Shuffle过程

    ——转自:{http://langyu.iteye.com/blog/992916} Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle ...

  9. hadoop的mapReduce和Spark的shuffle过程的详解与对比及优化

    https://blog.csdn.net/u010697988/article/details/70173104 大数据的分布式计算框架目前使用的最多的就是hadoop的mapReduce和Spar ...

随机推荐

  1. shell基础(七)-条件语句

    条件语句在编写脚本经常遇到:用于处理逻辑问题. 一 IF 语句 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句 ...

  2. 什么是LTE?

    LTE是英文Long Term Evolution的缩写.LTE也被通俗的称为3.9G,具有100Mbps的数据下载能力,被视作从3G向4G演进的主流技术.它改进并增强了3G的空中接入技术,采用OFD ...

  3. JVM学习(一)

    JVM自身的物理结构:

  4. 切换sprite

    using UnityEngine; using System.Collections; public class BTN : MonoBehaviour { void Awake ()  { //s ...

  5. 使用jq的ajax实现对xml文件的读取

    之前一直在用json来传递数据,但是xml也是不可缺少的,于是开始了xml的征程.xml的一些属性啊之类的在菜鸟教程上列举的已经很详细了,但是却没有前段部分的获取教程,查询资料,遂懂: index.x ...

  6. Android 7.1 SystemUI--任务管理--场景一:长按某个缩略图,拖动分屏的流程

    TaskView 类的长按事件 onLongClick 方法内发送了 DragStartEvent 事件消息,该 DragStartEvent 事件消息由 RecentsView,TaskStackV ...

  7. android系统自带图标

      android:src="@android:drawable/ic_media_rew"    

  8. oracle rank over partition by

    转自:https://www.cnblogs.com/wingsless/archive/2012/02/04/2338292.html rank() over(partition)的使用   有的时 ...

  9. ROM和RAM的故事

    在公众号里看到一篇很好的文章讲解rom和ram,之前也是一直不能理解两者的区别,今天就转载记下来吧.也方便大家学习. 因为我刚开始学习的时候总喜欢刨根问底,一个问题要是不搞清楚,后面学习都会很吃力的. ...

  10. [shell]用shell脚本将本地文件夹与ftp上的文件夹同步

    需求说明 最近在AIX上做开发,开发机器在office网段,测试机器在lab网段,不能互相通讯,只能通过特定的ftp来传文件. 每次上传的机器都要做:登录ftp,进入我的目录,上传:下载的机器都要做: ...