bla*_*ite 1 hadoop hive hiveql
考虑下表tab:
id fruits
1 orange, banana
2 orange
3 apple, banana, grape
Run Code Online (Sandbox Code Playgroud)
我想在fruits列上“爆炸”这个表:
select
id
individual_fruit
from tab
lateral view explode(split(fruits, ',')) the_fruits as individual_fruit
Run Code Online (Sandbox Code Playgroud)
这给了我这个:
id individual_fruit
1 orange
1 banana
2 orange
3 apple
3 banana
3 grape
Run Code Online (Sandbox Code Playgroud)
其中几行中的前导空格使得将这个新表与其他表连接起来很困难。如何去除新fruit列中的空白?我已经习惯了 Python,并且 Hive 的一些看起来像 Python 式的,所以类似的东西map(str.strip, individual_fruit))对我来说很有意义(但这显然在 Hive 中不起作用!)。
我有几种方法解决了我的问题。
你可以使用translate:
translate(individual_fruit, ' ', '')
Run Code Online (Sandbox Code Playgroud)
但这真的只在空格是字符串中唯一的空格时才有效。当包含其他空白类型时,这种方法会变得很麻烦。
或者,用于trim删除所有前导和尾随空格
trim(individual_fruit)
Run Code Online (Sandbox Code Playgroud)
我确信还有其他方法可以解决这个问题,也许使用regexp_replace,但我的问题是使用上述方法回答的。