【发布时间】:2019-08-28 14:22:46
【问题描述】:
我正在尝试从按 where 子句过滤的 Hive 表中选择具有特殊字符的数据,特别是 Tab 和 NewLine。我试过了
我尝试过like '%\\n%'、like '%\\t%'、like '%hex(9)%' 等,但它们似乎不起作用。
还尝试创建一个虚拟表来插入此类数据,但这也不起作用。请帮忙。
【问题讨论】:
标签: hive special-characters etl hiveql
我正在尝试从按 where 子句过滤的 Hive 表中选择具有特殊字符的数据,特别是 Tab 和 NewLine。我试过了
我尝试过like '%\\n%'、like '%\\t%'、like '%hex(9)%' 等,但它们似乎不起作用。
还尝试创建一个虚拟表来插入此类数据,但这也不起作用。请帮忙。
【问题讨论】:
标签: hive special-characters etl hiveql
使用rlike '\\t' 表示制表符,rlike '\\n' 表示换行符(使用双反斜杠):
hive> select 'a\tb' rlike '\\t'; --tabs
OK
true
Time taken: 0.075 seconds, Fetched: 1 row(s)
对于换行符:
hive> select 'a\nb' rlike '\\n'; --newline
OK
true
Time taken: 0.454 seconds, Fetched: 1 row(s)
使用换行符和制表符插入值的示例:
create table test_special_chars as
select 'a\nb' as a union all select 'a\tb';
换行很棘手。问题是表格默认是文本文件,换行符通常被解释为换行符,这就是为什么当被选中时,它会返回一个额外的行:
select * from test_special_chars;
OK
a
b
a b
实际上,插入 \n 在文本文件中创建了额外的行。这就是发生的事情。
但是如果你创建 ORC 表:
create table test_special_chars stored as ORC as select 'a\nb' as a union all select 'a\tb';
效果很好,因为 ORC 不是文本格式,可以存储换行符:
select count(*) from test_special_chars where a rlike '\\n';
返回:
OK
1
Time taken: 40.564 seconds, Fetched: 1 row(s)
当你select a from test_special_chars where a rlike '\\n'时,在屏幕上它也会显示为两行,它在选择时被解释,但ORC和文本文件之间的区别在于ORC中换行符可以存储在值中而无需创建额外的行文件。这就是为什么rlike '\\n' 与 ORC 一起使用而不能与 textfile 一起使用(不返回任何行),在插入 textfile 后 \n 在文件中创建两个单独的行,而在 ORC 中却没有。
这是用其他东西替换换行符的方法:
select regexp_replace(a,'\\n',' newline ') from test_special_chars where a rlike '\\n';
结果:
OK
a newline b
Time taken: 1.502 seconds, Fetched: 1 row(s)
【讨论】: