hive-TextInputformat自定义分隔符

hive-TextInputformat自定义分隔符

原创  2015年06月03日 11:38:27
  • 3440

前言

在一次利用sqoop将关系型数据库Oracle中的数据导入到hive的测试中,出现了一个分割符的问题。oracle中有字段中含有\n换行符,由于hive默认是以’\n’作为换行分割符的,所以用sqoop将oracle中数据导入到hive中导致hive中的数据条目跟原始数据库不一致,当时的处理方式是数据在导入到HDFS之前,用sqoop的参数将字段中的换行符都替换掉。

Sqoop在将数据从关系型数据库导入到HDFS时,支持将\n替换成自定义换行符(支持单字符自定义换行符),但是在hive中建表时用语句<row format delimited lines terminated by>指定自定义换行符会提示如下错误:

< linesterminated by>参数目前仅支持’\n’。不能指定自定义换行符,这样自定义换行符的数据就不能导入到hive中,基于以上考虑,本文简单说明了如何让hive实现自定义多个字符的换行和字段分割符,供大家参考。如有不足请批评指正。

环境

  • Hadoop:2.2
  • Hive:0.12(星环inceptor,支持原生hive)

目标

  • 分析hive自定义多字符串换行符;
  • 实现hive自定义多字符串字段分隔符;
  • 实现hivetextinputformat自定义编码格式的设置。

1.hive的序列化与反序列化

Hive中,默认使用的是TextInputFormat,一行表示一条记录。在每条记录(一行中),默认使用^A分割各个字段。

在有些时候,我们往往面对多行,结构化的文档,并需要将其导入Hive处理,此时,就需要自定义InputFormat、OutputFormat,以及SerDe了。

首先来理清这三者之间的关系,我们直接引用Hive官方说法:

SerDe is a short name for “Serializer and Deserializer.”

Hive uses SerDe (and !FileFormat) to read and write table rows.

HDFS files –> InputFileFormat –> <key, value> –> Deserializer –> Row object

Row object –> Serializer –> <key, value> –> OutputFileFormat –> HDFS files

总结一下,面对一个HDFS上的文件,Hive将如下处理(以读为例):

(1) 调用InputFormat,将文件切成不同的文档。每篇文档即一行(Row)。

(2) 调用SerDe的Deserializer,将一行(Row),切分为各个字段。

 

当HIVE执行INSERT操作,将Row写入文件时,主要调用OutputFormat、SerDe的Seriliazer,顺序与读取相反。

针对含有自定义换行符和字段分隔符的HDFS文件,本文仅介绍hive读取的过程的修改。

2 Hive默认采用的TextInputFormat类

首先建一个简单的表,然后用<describe  extended >命令查看该表的详细信息。

[sql]  view plain  copy
  1. transwarp> create table test1(id int);  
  2. OK  
  3. Time taken: 0.062seconds  
  4. transwarp>describe extended test1;  
  5. OK  
  6. id                  int                   None                 
  7.                     
  8. Detailed Table Information       
  9. Table(tableName:test1, dbName:default, owner:root,createTime:1409300219, lastAccessTime:0, retention:0,  
  10. sd:StorageDescriptor(  
  11. cols:[FieldSchema(name:id, type:int,comment:null)],location:hdfs://leezq-vm3:8020/inceptor1/user/hive/warehouse/test1,  
  12. inputFormat:org.apache.hadoop.mapred.TextInputFormat,outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat,  
  13. compressed:false,  
  14. numBuckets:-1,  
  15. serdeInfo:SerDeInfo(  
  16. name:null,  
  17. serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe,  
  18. parameters:{serialization.format=1}),  
  19. bucketCols:[], sortCols:[], parameters:{},skewedInfo:SkewedInfo(skewedColNames:[], skewedColValues:[],skewedColValueLocationMaps:{}), storedAsSubDirectories:false),partitionKeys:[],  
  20. parameters:{transient_lastDdlTime=1409300219},  
  21. viewOriginalText:null, viewExpandedText:null,tableType:MANAGED_TABLE  
  22. )         
  23. Time taken: 0.121 seconds, Fetched: 3 row(s)  

从上面可以看出,默认状态下,hive的输入和输出调用的类分别为:

[java]  view plain  copy
  1. inputFormat:org.apache.hadoop.mapred.TextInputFormat,  
  2. outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat,  

虽然现在现在hadoop现在升级到2.X版本,hive依然采用老版的mapred接口。

我们要改写的就是类TextInputFormat

2.1类 TextInputFormat

类TextInputFormat在hadoop-mapreduce-client-core-2.2.0.jar中。

重点看类中getRecordReader方法,该方法返回LineRecordReader对象。并且该方法中已经实现了接收自定义字符串作为换行符的代码,只要建表前在hive的CLI界面上输入set textinputformat.record.delimiter=<自定义换行字符串>;即可实现自定义多字符换行符。


2.2类LineRecordReader

为了进一步查看其实现原理,我们进一步看LineRecordReader(package org.apache.hadoop.mapred. LineRecordReader)类。


查看该类的构造函数,该类调用org.apache.hadoop.util.LineReader(在包hadoop-common-2.2.0.jar中)获取每行的数据,把参数recordDelimiter传给类对象LineReader,类LineReader中的readLine(Text str, int maxLineLength, intmaxBytesToConsume)方法负责按照用户自定义分隔符返回每行的长度,如果用户不设定 textinputformat.record.delimiter的值, recordDelimiter的值为null,这时readLine方法就会按照默认’\n’分割每行。readLine的代码如下:


通过读源码可以看到,原始的hive可以通过设置参数的方法实现多字符自定义换行符(textFile的存储方式),通过上图中readCustomLine方法获得用户自定义换行符的字符串实现自动换行,每行最大可支持2147483648大小。但是要想实现自定义多字符的字段分隔符和自定义编码格式的设置,还需要对源码进行改写。下面就讲一下改写的步骤。

3 自定义TextInputFormat

  • 实现自定义多字符串的字段分割符
  • 实现自定义编码格式的设置

首先建一个空的java工程,添加必须的五个包


然后新建两个类SQPTextInputFormat和SQPRecordReader,将TextInputFormat和LineRecordReader的代码分别拷贝过来。

在SQPTextInputFormat中添加对自定义编码格式的设置。(对换行符的参数进行了更名,将textinputformat.record.delimiter改成了textinputformat.record.linesep)

[java]  view plain  copy
  1. //======================================================  
  2.   String delimiter = job.get("textinputformat.record.linesep");  
  3.   this.encoding = job.get("textinputformat.record.encoding",defaultEncoding);  
  4.   byte[] recordDelimiterBytes = null;  
  5.   if (null != delimiter) {//Charsets.UTF_8  
  6.     recordDelimiterBytes = delimiter.getBytes(this.encoding);  
  7.   }  
  8.   return new SQPRecordReader(job, (FileSplit)genericSplit, recordDelimiterBytes);  
  9.    

在SQPRecordReader构造函数中添加对字段分隔符和编码格式的设置。

[java]  view plain  copy
  1. //======================================================  
  2.     this.FieldSep = job.get("textinputformat.record.fieldsep",defaultFSep);  
  3. this.encoding = job.get("textinputformat.record.encoding",defaultEncoding);  
  4.    

在SQPRecordReader的next()方法中添加对字段分割符的替换和对编码格式的设置。

[java]  view plain  copy
  1. //======================================================  
  2.     if (encoding.compareTo(defaultEncoding) != 0) {  
  3.               String str = new String(value.getBytes(), 0,value.getLength(), encoding);  
  4.               value.set(str);  
  5.          }  
  6.       if (FieldSep.compareTo(defaultFSep) != 0) {  
  7.               String replacedValue = value.toString().replace(FieldSep, defaultFSep);  
  8.               value.set(replacedValue);  

详细的代码如下:

[java]  view plain  copy
  1. package com.learn.util.hadoop;  
  2.   
  3. //import com.google.common.base.Charsets;  
  4.   
  5. import java.io.IOException;  
  6.   
  7. import org.apache.hadoop.fs.FileSystem;  
  8. import org.apache.hadoop.fs.Path;  
  9. import org.apache.hadoop.io.LongWritable;  
  10. import org.apache.hadoop.io.Text;  
  11. import org.apache.hadoop.io.compress.CompressionCodec;  
  12. import org.apache.hadoop.io.compress.CompressionCodecFactory;  
  13. import org.apache.hadoop.io.compress.SplittableCompressionCodec;  
  14. import org.apache.hadoop.mapred.*;  
  15.   
  16. public class SQPTextInputFormat extends FileInputFormat<LongWritable, Text>  
  17. implements JobConfigurable  
  18. {  
  19. private CompressionCodecFactory compressionCodecs = null;  
  20. private final static String defaultEncoding = "UTF-8";//"US-ASCII""ISO-8859-1""UTF-8""UTF-16BE""UTF-16LE""UTF-16"  
  21. private String encoding = null;  
  22.   
  23. public void configure(JobConf conf) {  
  24.   this.compressionCodecs = new CompressionCodecFactory(conf);  
  25. }  
  26.   
  27. protected boolean isSplitable(FileSystem fs, Path file) {  
  28.   CompressionCodec codec = this.compressionCodecs.getCodec(file);  
  29.   if (null == codec) {  
  30.     return true;  
  31.   }  
  32.   return codec instanceof SplittableCompressionCodec;  
  33. }  
  34.   
  35. public RecordReader<LongWritable, Text> getRecordReader(InputSplit genericSplit, JobConf job, Reporter reporter)  
  36.   throws IOException  
  37. {  
  38.   reporter.setStatus(genericSplit.toString());  
  39.   String delimiter = job.get("textinputformat.record.linesep");  
  40.   this.encoding = job.get("textinputformat.record.encoding",defaultEncoding);  
  41.   byte[] recordDelimiterBytes = null;  
  42.   if (null != delimiter) {//Charsets.UTF_8  
  43.     recordDelimiterBytes = delimiter.getBytes(this.encoding);  
  44.   }  
  45.   return new SQPRecordReader(job, (FileSplit)genericSplit, recordDelimiterBytes);  
  46. }  
  47. }  


[java]  view plain  copy
  1. package com.learn.util.hadoop;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. import org.apache.commons.logging.Log;  
  7. import org.apache.commons.logging.LogFactory;  
  8. import org.apache.hadoop.classification.InterfaceAudience.LimitedPrivate;  
  9. import org.apache.hadoop.classification.InterfaceStability.Unstable;  
  10. import org.apache.hadoop.conf.Configuration;  
  11. import org.apache.hadoop.fs.FSDataInputStream;  
  12. import org.apache.hadoop.fs.FileSystem;  
  13. import org.apache.hadoop.fs.Path;  
  14. import org.apache.hadoop.fs.Seekable;  
  15. import org.apache.hadoop.io.LongWritable;  
  16. import org.apache.hadoop.io.Text;  
  17. import org.apache.hadoop.io.compress.CodecPool;  
  18. import org.apache.hadoop.io.compress.CompressionCodec;  
  19. import org.apache.hadoop.io.compress.CompressionCodecFactory;  
  20. import org.apache.hadoop.io.compress.Decompressor;  
  21. import org.apache.hadoop.io.compress.SplitCompressionInputStream;  
  22. import org.apache.hadoop.io.compress.SplittableCompressionCodec;  
  23. import org.apache.hadoop.io.compress.SplittableCompressionCodec.READ_MODE;  
  24. import org.apache.hadoop.util.LineReader;  
  25. import org.apache.hadoop.mapred.RecordReader;  
  26. import org.apache.hadoop.mapred.FileSplit;  
  27.   
  28. //@InterfaceAudience.LimitedPrivate({"MapReduce", "Pig"})  
  29. //@InterfaceStability.Unstable  
  30. public class SQPRecordReader  
  31.   implements RecordReader<LongWritable, Text>  
  32. {  
  33.   private static final Log LOG = LogFactory.getLog(SQPRecordReader.class.getName());  
  34.   
  35.   private CompressionCodecFactory compressionCodecs = null;  
  36.   private long start;  
  37.   private long pos;  
  38.   private long end;  
  39.   private LineReader in;  
  40.   private FSDataInputStream fileIn;  
  41.   private final Seekable filePosition;  
  42.   int maxLineLength;  
  43.   private CompressionCodec codec;  
  44.   private Decompressor decompressor;  
  45.   private String FieldSep;          //field separator  
  46.   private static final String defaultFSep="\001";  
  47.   private final static String defaultEncoding = "UTF-8";//"US-ASCII""ISO-8859-1""UTF-8""UTF-16BE""UTF-16LE""UTF-16"  
  48.   private String encoding = null;  
  49.   
  50.   public SQPRecordReader(Configuration job, FileSplit split)  
  51.     throws IOException  
  52.   {  
  53.     this(job, split, null);  
  54.   }  
  55.   
  56.   public SQPRecordReader(Configuration job, FileSplit split, byte[] recordDelimiter) throws IOException  
  57.   {  
  58.     this.maxLineLength = job.getInt("mapreduce.input.linerecordreader.line.maxlength"2147483647);  
  59.     this.FieldSep = job.get("textinputformat.record.fieldsep",defaultFSep);  
  60.     this.encoding = job.get("textinputformat.record.encoding",defaultEncoding);  
  61.     this.start = split.getStart();  
  62.     this.end = (this.start + split.getLength());  
  63.     Path file = split.getPath();  
  64.     this.compressionCodecs = new CompressionCodecFactory(job);  
  65.     this.codec = this.compressionCodecs.getCodec(file);  
  66.   
  67.     FileSystem fs = file.getFileSystem(job);  
  68.     this.fileIn = fs.open(file);  
  69.     if (isCompressedInput()) {  
  70.       this.decompressor = CodecPool.getDecompressor(this.codec);  
  71.       if ((this.codec instanceof SplittableCompressionCodec)) {  
  72.         SplitCompressionInputStream cIn = ((SplittableCompressionCodec)this.codec).createInputStream(this.fileIn, this.decompressor, this.start, this.end, SplittableCompressionCodec.READ_MODE.BYBLOCK);  
  73.   
  74.         this.in = new LineReader(cIn, job, recordDelimiter);  
  75.         this.start = cIn.getAdjustedStart();  
  76.         this.end = cIn.getAdjustedEnd();  
  77.         this.filePosition = cIn;  
  78.       } else {  
  79.         this.in = new LineReader(this.codec.createInputStream(this.fileIn, this.decompressor), job, recordDelimiter);  
  80.         this.filePosition = this.fileIn;  
  81.       }  
  82.     } else {  
  83.       this.fileIn.seek(this.start);  
  84.       this.in = new LineReader(this.fileIn, job, recordDelimiter);  
  85.       this.filePosition = this.fileIn;  
  86.     }  
  87.   
  88.     if (this.start != 0L) {  
  89.       this.start += this.in.readLine(new Text(), 0, maxBytesToConsume(this.start));  
  90.     }  
  91.     this.pos = this.start;  
  92.   }  
  93.   
  94.   public SQPRecordReader(InputStream in, long offset, long endOffset, int maxLineLength)  
  95.   {  
  96.     this(in, offset, endOffset, maxLineLength, null);  
  97.   }  
  98.   
  99.   public SQPRecordReader(InputStream in, long offset, long endOffset, int maxLineLength, byte[] recordDelimiter)  
  100.   {  
  101.     this.maxLineLength = maxLineLength;  
  102.     this.in = new LineReader(in, recordDelimiter);  
  103.     this.start = offset;  
  104.     this.pos = offset;  
  105.     this.end = endOffset;  
  106.     this.filePosition = null;  
  107.   }  
  108.   
  109.   public SQPRecordReader(InputStream in, long offset, long endOffset, Configuration job)  
  110.     throws IOException  
  111.   {  
  112.     this(in, offset, endOffset, job, null);  
  113.   }  
  114.   
  115.   public SQPRecordReader(InputStream in, long offset, long endOffset, Configuration job, byte[] recordDelimiter)  
  116.     throws IOException  
  117.   {  
  118.     this.maxLineLength = job.getInt("mapreduce.input.linerecordreader.line.maxlength"2147483647);  
  119.   
  120.     this.in = new LineReader(in, job, recordDelimiter);  
  121.     this.start = offset;  
  122.     this.pos = offset;  
  123.     this.end = endOffset;  
  124.     this.filePosition = null;  
  125.   }  
  126.   
  127.   public LongWritable createKey() {  
  128.     return new LongWritable();  
  129.   }  
  130.   
  131.   public Text createValue() {  
  132.     return new Text();  
  133.   }  
  134.   
  135.   private boolean isCompressedInput() {  
  136.     return this.codec != null;  
  137.   }  
  138.   
  139.   private int maxBytesToConsume(long pos) {  
  140.     return isCompressedInput() ? 2147483647 : (int)Math.min(2147483647L, this.end - pos);  
  141.   }  
  142.   
  143.   private long getFilePosition()  
  144.     throws IOException  
  145.   {  
  146.     long retVal;  
  147.     if ((isCompressedInput()) && (null != this.filePosition))  
  148.       retVal = this.filePosition.getPos();  
  149.     else {  
  150.       retVal = this.pos;  
  151.     }  
  152.     return retVal;  
  153.   }  
  154.   
  155.   public synchronized boolean next(LongWritable key, Text value)  
  156.     throws IOException  
  157.   {  
  158.     while (getFilePosition() <= this.end) {  
  159.       key.set(this.pos);  
  160.   
  161.       int newSize = this.in.readLine(value, this.maxLineLength, Math.max(maxBytesToConsume(this.pos), this.maxLineLength));  
  162.         
  163.       if (newSize == 0) {  
  164.         return false;  
  165.       }  
  166.         
  167.       if (encoding.compareTo(defaultEncoding) != 0) {  
  168.             String str = new String(value.getBytes(), 0, value.getLength(), encoding);  
  169.             value.set(str);  
  170.         }  
  171.         
  172.       if (FieldSep.compareTo(defaultFSep) != 0) {  
  173.             String replacedValue = value.toString().replace(FieldSep, defaultFSep);  
  174.             value.set(replacedValue);  
  175.         }  
  176.         
  177.       this.pos += newSize;  
  178.       if (newSize < this.maxLineLength) {  
  179.         return true;  
  180.       }  
  181.   
  182.       LOG.info("Skipped line of size " + newSize + " at pos " + (this.pos - newSize));  
  183.     }  
  184.   
  185.     return false;  
  186.   }  
  187.   
  188.   public synchronized float getProgress()  
  189.     throws IOException  
  190.   {  
  191.     if (this.start == this.end) {  
  192.       return 0.0F;  
  193.     }  
  194.     return Math.min(1.0F, (float)(getFilePosition() - this.start) / (float)(this.end - this.start));  
  195.   }  
  196.   
  197.   public synchronized long getPos() throws IOException  
  198.   {  
  199.     return this.pos;  
  200.   }  
  201.   
  202.   public synchronized void close() throws IOException {  
  203.     try {  
  204.       if (this.in != null)  
  205.         this.in.close();  
  206.     }  
  207.     finally {  
  208.       if (this.decompressor != null)  
  209.         CodecPool.returnDecompressor(this.decompressor);  
  210.     }  
  211.   }  
  212. }  

4 自定义InputFormat的使用

1.      将程序打成jar包,放在/usr/lib/hive/lib和各个节点的/usr/lib/hadoop-mapreduce目录下。

在hvie的CLI命令行界面可以设置如下参数,分别修改编码格式、自定义字段分隔符和自定义换行符。

[sql]  view plain  copy
  1. set textinputformat.record.encoding=UTF-8;  
  2. //"US-ASCII""ISO-8859-1""UTF-8""UTF-16BE""UTF-16LE""UTF-16"  
  3. set textinputformat.record.fieldsep=,;  
  4. set textinputformat.record.linesep=|+|;  


2.      建表,标示采用的Inputformat和OutputFormat,其中org.apach…noreKeyTextOutputFormat 是hive默认的OutputFormat分隔符。

[sql]  view plain  copy
  1. create table test  
  2. (  
  3. id string,  
  4. name string  
  5. )  
  6. stored as  
  7. INPUTFORMAT'com.learn.util.hadoop.SQPTextInputFormat'  
  8. OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'  

3.      Load 语句加载数据

实例

测试数据:


测试数据中有一个字段中含有换行符。字段分隔符和行分隔符分别为’,’和“|+|”。

分别设置字段分隔符和行分割符,并建表指定Inputformat和outputformat如下图所示,


Select  * 查询如下:


Select count(*)如下:


结果是3行,正确。

Select  id  from test1如下:


Select  name from test1:


Select  count(name) from test1:



结果正确。

Select  name,id from test1:


Select  id,name from test1;


Id和name两个字段单独查没问题,但是调用mapreduce一起查的时候带有‘\n’的字段显示上出了问题。

Select  id,name from test1 where id=13:


单独查询每个字段时候和查询总行数的时候都是没问题的,这说明改写的InputFormat起作用了,上面的出现的NULL问题应该是hive显示的问题。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值