Use 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 table:
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';
Returns:
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,不适用于文本文件(不返回任何行),插入文本文件后 \ 并在文件中创建两个单独的行,但在 ORC 中则不会。
这是用其他内容替换换行符的方法:
select regexp_replace(a,'\\n',' newline ') from test_special_chars where a rlike '\\n';
Result:
OK
a newline b
Time taken: 1.502 seconds, Fetched: 1 row(s)