Hive多字符的分隔符

hive

默认是只支持单字符的分隔符,默认单字符是\001。当然你也可以在创建表格时指定数据的分割符号。如:

create table user(name string, password string) row format delimited fields terminated by ‘\t’。

通过这种方式,完成分隔符的指定。

如果你想要支持多字符的分隔符可以通过如下方式:

1、自定义一个 InputFormat ,重写 InputFormat 中 RecordReader 类中的 next 方法.。当然这是输入的时候调用的,输出的时候也是可以设定不同的分隔符的。方法和输入一样,自定义一个OutputFormat,不过这里要注意的是:自定义的OutputFormat必须要实现HiveOutputFormat接口,重写 OutputFormat 中 RecordWriter 中的 write 方法,这里可以参考HiveIgnoreKeyTextOutputFormat类。Hive的InputFormat/OutputFormat与Hadoop 的InputFormat/OutputFormat相当类似,InputFormat负责把输入的数据进行格式化或转换处理,然后提供给Hive,OutputFormat 负责把 Hive输出的数据重新格式化成目标格式再输出到文件,这种对格式进行定制的方式较为底层。重写完成后打包成jar,放入到Hive目录的lib文件夹下面。

public synchronized boolean next(LongWritable key, Text value)  
	            throws IOException {  
	 while (pos < end) {  
	       key.set(pos);  
	       int newSize = lineReader.readLine(value, maxLineLength,  
	           	Math.max((int) Math.min(Integer.MAX_VALUE, end - pos), maxLineLength));  
	       if (newSize == 0)  return false;  
	       String str = value.toString().toLowerCase().replaceAll("::", ":");  
	       value.set(str);  
	       pos += newSize;  
	       if (newSize < maxLineLength)  return true;  
	 }  
	 return false;  
}  
@Override
public void write(Writable r) throws IOException {
	if (r instanceof Text) {
		Text tr = (Text) r;
		String strReplace = tr.toString().toLowerCase().replace(":", "::");
		Text txtReplace = new Text();
		txtReplace.set(strReplace);
		outStream.write(txtReplace.getBytes(), 0, txtReplace.getLength());
		outStream.write(finalRowSeparator);
	} else {
		BytesWritable bw = (BytesWritable) r;
		outStream.write(bw.get(), 0, bw.getSize());
		outStream.write(finalRowSeparator);
	}
}

需要重新进入shell模式,在创建表的时候如下操作:

create table user(username string,password string)   
row format delimited  
fields terminated by ':'   
stored as
INPUTFORMAT 'org.platform.utils.bigdata.hive.CustomInputFormat'  
OUTPUTFORMAT 'org.platform.utils.bigdata.hive.CustomOutputFormat';  

2、通过 SerDe(serialize/deserialize) ,在数据序列化和反序列化时格式化数据。 这种方式比较复杂一点,对数据的控制能力也要弱一些,它使用正则表达式来匹配和处理数据,性能也会有所影响。但它的优点是可以自定义表属性信息SERDEPROPERTIES,在 SerDe 中通过这些属性信息可以有更多的定制行为。参考示例:

create table user(username string,password string,nickname string) 
row format serde 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe' 
with serdeproperties (
'input.regex'='([^:]*)::([^:]*)::([^:]*)',
'output.format.string'='%1$s %2$s %3$3') 
stored as textfile;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值