[转帖]Hadoop输入与输出_Hadoop,ERP及大数据讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Hadoop,ERP及大数据讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2890 | 回复: 0   主题: [转帖]Hadoop输入与输出        下一篇 
huizai
注册用户
等级:少校
经验:933
发帖:83
精华:0
注册:2013-6-18
状态:离线
发送短消息息给huizai 加好友    发送短消息息给huizai 发消息
发表于: IP:您无权察看 2013-6-19 15:52:33 | [全部帖] [楼主帖] 楼主

转自:http://blog.csdn.net/u010347517/article/details/9086479

这一问题本书只在第三章简单说了一下读写HDFS,虽然能说明问题,但是本着第一遍读书应该把书读厚的原则,我觉得很有必要自行展开一番。再说凡是万变不离其宗嘛,任何程序都是从“输入-->计算-->输出”。先说输入,Hadoop的默认的输入方式是将输入的每一行视为一条记录,该行文件偏移量为key,内容为value。这样当然不一定能满足所有的业务需要。因此,一方面Hadoop也提供了很多其他的输入格式,另一方面,更自由的,提供了自定义方式。先摆出几个概念:InputFiles : 这个好说,简单。InputFormat : 这个得说说,虽然也简单,这个接口(Java interface)决定了Mapper实例将从Hadoop框架中得到什么样的数据,即什么样的Key-ValueInputSplit : 这个在应用里不会直接接触到,但是这个概念值得了解,YDN上有这么一段话:(注:以下标为原文是为了在日记中进行突出显示,非原文字句,请作者及读者见谅,如果存在版权问题请指出~)

Another important job of the InputFormat is to divide the input data sources (e.g., input files) into fragments that make up the inputs to individual map tasks.  These fragments are called "splits" and are encapsulated in instances of the InputSplit interface.


一般说来,InputSplit决定了每个Mapper要处理的数据集;而InputFormat则决定了每一个Split里面的数据格式/数据结构;不知道这样一说有没有说清楚,大体可以理解为InputSplit是物理性的输入,InputFormat是逻辑性的输入。Hadoop系统提供以下几种:(注:以下标为原文是为了在日记中进行突出显示,非原文字句,请作者及读者见谅,如果存在版权问题请指出~)

    TextInputFormat:文件偏移量 :整行数据

    KeyValueTextInputFormat:第一个"\t"前的数据 : 后面的整行数据

    SequenceFileInputFormat:因为这是二进制文件,所以Key-Value都是由用户指定

    NLineInputFormat:与TextInputFormat一样,就是NLine的区别了
标准的InputFormat接口如下:

  1. public interface InputFormat<K, V>
  2. {
  3.   InputSplit[] getSplits(JobConf job, int numSplits) throws IOException;
  4.   RecordReader<K, V> getRecordReader(InputSplit split,
  5.   JobConf job,
  6.   Reporter reporter) throws IOException;
  7. }

;
如果要自定制输入,就是要继承这个接口。两个函数分别的用途是:

■ Identify all the files used as input data and divide them into input splits. Eachmap task is assigned one split.■ Provide an object (RecordReader) to iterate through records in a given split,and to parse each record into key and value of predefined types.
根据本书的建议,如果一定要自定制输入,最好派生FileInputFormat,而不是直接实现InputFormat接口,原因是对于getSplits()方法,它已经实现好了,足够绝大多数实际开发的需求。下面给出一个例子:假设你的输入数据格式是这样的:ball, 3.5, 12.7, 9.0car, 15, 23.76, 42.23device, 0.0, 12.4, -67.1每个点的名字,后面是在坐标系里面的坐标值。

  1. /* 仅仅实现了getRecordReader()方法 */
  2. public class ObjectPositionInputFormat extends FileInputFormat<Text, Point3D> {
  3.   public RecordReader<Text, Point3D> getRecordReader(InputSplit input, JobConf job, Reporter reporter) throws IOException {
  4.   reporter.setStatus(input.toString());
  5.   return new ObjPosRecordReader(job, (FileSplit)input);
  6.   }
  7. }
  8. /* 下面是实现了ObjPosRecordReader类 */
  9. class ObjPosRecordReader implements RecordReader<Text, Point3D> {
  10.   private LineRecordReader lineReader;
  11.   private LongWritable lineKey;
  12.   private Text lineValue;
  13.   public ObjPosRecordReader(JobConf job, FileSplit split) throws IOExpection {
  14.   lineReader = new LineRecordReader(job, conf);
  15.   lineKey = lineReader.createKey();
  16.   lineValue = lineReader.createValue();
  17.   }
  18.   public boolean next(Text Key, Point3D value) throws IOEcpection {
  19.   if(!lineReader.next(lineKey, lineValue)){
  20.   return false;
  21.   }
  22.  
  23.   String[] pieces = lineValue.toString().split(",");
  24.   if(pieces.length != 4) {
  25.   throw new IOExpection("Invalid record received");
  26.   }
  27.  
  28.   float fx, fy, fz;
  29.   try {
  30.   fx = Float.parseFloat(pieces[1].trim());
  31.   fy = Float.parseFloat(pieces[2].trim());
  32.   fz = Float.parseFloat(pieces[3].trim());
  33.   } catch(NumberFormatExecption nfe) {
  34.   throw new IOException("Error parsing floating point value in record");
  35.   }
  36.  
  37.   key.set(pieces[0].trim());
  38.  
  39.   value.x = fx;
  40.   value.y = fy;
  41.   value.z = fz;
  42.  
  43.   return true;
  44. }
  45. public Text createKey() {
  46. return new Text("");
  47. }
  48. public Text createValue() {
  49.   return new Point3D();
  50. }
  51. public long getPos() throws IOExpection {
  52.   return lineReader.getPos();
  53. }
  54. public void close() throws IOExpection {
  55.   lineReader.close();
  56. }
  57. public float getProgress() throws IOExpection {
  58.   return lineReader.getProcess();
  59. }
  60. }

;


关于输出,一般都是对输出格式进行控制,比如要输出XML或是JSON类型等等,这一部分不说了,少敲几个字,因为总体与输入差不多。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论