插入具有特殊字符的 Hive table 内容 -Tab space 和换行符

Inserting Hive table content with special characters -Tab space and New Line

我正在尝试 select 具有特殊字符的数据,特别是 Tab 和 NewLine,来自 Hive tables 对 where 子句的过滤。我试过了

我试过 like '%\n%'like '%\t%'like '%hex(9)%' 等,但它们似乎不起作用。

还尝试创建一个虚拟 table 来插入此类数据,但这也不起作用。请大家帮忙

制表符使用 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';

换行很棘手。问题是 table 默认情况下是文本文件,换行符通常被解释为换行符,这就是为什么在 selected 时,它 returns 多了一行:

 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'时,在屏幕上也会显示为两行,在select上被解释,但ORC和文本文件的区别在于ORC中可以换行存储在值中而不在文件中创建额外的行。这就是为什么 rlike '\n' 适用于 ORC 而不适用于文本文件(不返回任何行)的原因,在文本文件中插入后 \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)