Hive 特殊分隔符处理
HIVE特殊分隔符处理
Hive对文件中的分隔符默认情况下只支持单字节分隔符,,默认单字符是\001。当然你也可以在创建表格时指定数据的分割符号。但是如果数据文件中的分隔符是多字符的,如下图:
01||zhangsan
02||lisi
03||wangwu
补充:hive读取数据的机制
1、首先用inputformat的一个具体的实现类读取文件数据,返回一条条的记录(可以是行,或者是你逻辑中的“行”)
2、然后利用SerDe<默认:org.apache.hadoop.hive.serde2.LazySimpleSerDe>的一个具体的实现类,对上面返回的一条条记录进行字段切割
使用RegexSerDe通过正则表达式来抽取字段
1、建表
|
create table t_bi_reg(id string,name string) row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' with serdeproperties( 'input.regex'='(.*)\\|\\|(.*)', 'output.format.string'='%1$s%2$s' ) stored as textfile; |
2、加载数据
|
01||zhangsan 02||lisi 03||wangwu load data local inpath '/root/lianggang.txt' into table t_bi_reg; |
3、查询
|
hive> select * from t_bi_reg; OK 01 zhangsan 02 lisi 03 wangwu |
通过自定义inputformat解决特殊分隔符问题
其原理是在inputformat读取行的时候将数据中的“多字节分隔符”替换为hive默认的分隔符(ctrl+A 亦即 \001)或用于替代的单字符分隔符。以便hive在serde操作的时候按照默认的单字节分隔符进行字段抽取
|
package cn.gec.bigdata.hive.inputformat; import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.FileSplit; import org.apache.hadoop.mapred.InputSplit; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.LineRecordReader; import org.apache.hadoop.mapred.RecordReader; import org.apache.hadoop.mapred.Reporter; import org.apache.hadoop.mapred.TextInputFormat; public class BiDelimiterInputFormat extends TextInputFormat { @Override public RecordReader<LongWritable, Text> getRecordReader( InputSplit genericSplit, JobConf job, Reporter reporter) throws IOException { reporter.setStatus(genericSplit.toString()); MyDemoRecordReader reader = new MyDemoRecordReader(new LineRecordReader(job, (FileSplit) genericSplit)); // BiRecordReader reader = new BiRecordReader(job, (FileSplit)genericSplit); return reader; } public static class MyDemoRecordReader implements RecordReader<LongWritable, Text> { LineRecordReader reader; Text text; public MyDemoRecordReader(LineRecordReader reader) { this.reader = reader; text = reader.createValue(); } @Override public void close() throws IOException { reader.close(); } @Override public LongWritable createKey() { return reader.createKey(); } @Override public Text createValue() { return new Text(); } @Override public long getPos() throws IOException { return reader.getPos(); } @Override public float getProgress() throws IOException { return reader.getProgress(); } @Override public boolean next(LongWritable key, Text value) throws IOException { boolean next = reader.next(key, text); if(next){ String replaceText = text.toString().replaceAll("\\|\\|", "\\|"); value.set(replaceText); } return next; } } } |
1.打包成jar,放到$HIVE_HOME/lib下
2.建表指明自定义的inputformat
|
create table t_lianggang(id string,name string) row format delimited fields terminated by '|' stored as inputformat 'cn.gec.bigdata.hive.inputformat.BiDelimiterInputFormat' outputformat 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'; |
3.加载数据
|
01||zhangsan 02||lisi 03||wangwu load data local inpath '/root/lianggang.txt' into table t_lianggang; |
4.查询
|
hive> select * from t_lianggang; OK 01 zhangsan 02 lisi 03 wangwu |
Hive 特殊分隔符处理的更多相关文章
- Hive 默认分隔符
引言 Hive 中的默认分隔符是 ^A (\001) ,这是一种特殊的分隔符,使用的是 ASCII 编码的值,键盘是打不出来的 查看 Hive 默认分隔符文件 Linux 上的文件 以 \001 作为 ...
- hive 的分隔符、orderby sort by distribute by的优化
一.Hive 分号字符 分号是SQL语句结束标记,在HiveQL中也是,可是在HiveQL中,对分号的识别没有那么智慧,比如: select concat(cookie_id,concat(';',' ...
- Hive 多分隔符的使用 (转载)
方法一)通过org.apache.hadoop.hive.contrib.serde2.RegexSerDe格式的serde. 1) 建表语句 #指定以^|~作为分隔符 CREATE TABlE ta ...
- Hive 指定分隔符,将表导出到本地
hive表的数据源有四种: hbase hdfs 本地 其他hive表 而hive表本身有两种: 内部表和外部表. 而hbase的数据在hive中,可以建立对应的外部表(参看hive和hbase整合) ...
- hive默认分隔符引起的日志分割问题
Hive中的外部表 对于Hive中的外部表来说,因为表是外部的,Hive认为其并不拥有这份数据,删除该表并不会真正删除其中的数据,其中的表描述元信息会被删除掉. 对数据进行分区后,对于管理表,可以 ...
- hive中分隔符‘\001’到底是什么鬼
答:hive中的默认的是'\001'是一种特由的分隔符 使用的是ascii编码的值,键盘是打不出来的.
- hive默认分隔符
默认分隔符‘\001',对应ascii码SOH: 通过cat -A filename可以查看分隔符:
- hive
Hive Documentation https://cwiki.apache.org/confluence/display/Hive/Home 2016-12-22 14:52:41 ANTLR ...
- Sqoop_mysql,hive,hdfs导入导出操作
前言: 搭建环境,这里使用cdh版hadoop+hive+sqoop+mysql 下载 hadoop-2.5.0-cdh5.3.6.tar.gz hive-0.13.1-cdh5.3.6.tar.gz ...
随机推荐
- golang实现一个代理服务器(proxy)学习笔记
golang是google公司开发一门新的编程语言.对于老的程序员来说,学习一门语言最好的方式,不过是做一个小的项目. 网上看到这一篇使用golang开发proxy的例子,觉得挺有意思.希望通过实际模 ...
- ios读取plist文件:
@property (nonatomic,strong) NSArray *imageData;//定义一个数组 //懒加载数据 -(NSArray *)imageDate { if(_imageDa ...
- [转]How rival bots battled their way to poker supremacy
How rival bots battled their way to poker supremacy http://www.nature.com/news/how-rival-bots-battle ...
- Jquery的deferred对象,看这2篇牛人的文章,基本就够了。
http://blog.csdn.net/ligang2585116/article/details/51589073 http://www.ruanyifeng.com/blog/2011/08/a ...
- sharpkeys键盘按键重映射
/********************************************************************** * sharpkeys键盘按键重映射 * 说明: * 键 ...
- Samsung_tiny4412(驱动笔记05)----Makefile,open,read,write,lseek,poll,ioctl,fasync
/*********************************************************************************** * * Makefile,op ...
- 九度OJ1153-括号匹配-栈的应用
题目1153:括号匹配问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9697 解决:4213 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见 ...
- [LeetCode&Python] Problem 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- J - FatMouse's Speed
p的思路不一定要到最后去找到ans:也可以设置成在中间找到ans:比如J - FatMouse's Speed 这个题,如果要是让dp[n]成为最终答案的话,即到了i,最差的情况也是dp[i-1],就 ...
- MySQL Binlog--binlog_format参数
===================================================================================== binlog_format参 ...