使用每个字符串

时间:2015-10-08 14:19:02

标签: java arrays hadoop split hive

说,我在hive中有一个字符串数组,例如:

hive> select array from my_table;
["\"string1\"","\"string2_component_a\",\"string2_component_b\"","\"string3_component_a\",\"string3_component_b\",\"string3_component_c\""]

如您所见,有以下三个字符串:

"string1"
"string2_component_a","string2_component_b"
"string3_component_a","string3_component_b","string3_component_c"

我想要的是在每个字符串中用 last 逗号分割每个元素。因此,生成的数组应该包含以下5个组件:

"string1"
"string2_component_a"
"string2_component_b"
"string3_component_a","string3_component_b"
"string3_component_c"

有没有办法在蜂巢中实现这一目标?

2 个答案:

答案 0 :(得分:2)

Oneliner使用

yourString.split(",", StringUtils.countMatches(yourString, ".") - 1);

更新:

  

你确定我可以使用它吗?

根据Hive documentation

  

要求   Java 1.7   注意:Hive版本1.2以后需要Java 1.7或更高版本。 Hive版本0.14到1.1也适用于Java 1.6。强烈建议用户开始使用Java 1.8(参见HIVE-8607)。

是的,您可以使用String.split中的StringUtilsApache Commons,而不会有任何问题。

答案 1 :(得分:0)

还要注意谷歌番石榴库。事实上,Splitter是如此强大 https://github.com/google/guava/wiki/StringsExplained#splitter