【问题标题】:Split strings in an array by last comma within each string通过每个字符串中的最后一个逗号拆分数组中的字符串
【发布时间】:2015-10-08 14:19:02
【问题描述】:

说,我在 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"

有没有办法在 hive 中实现这一点?

【问题讨论】:

    标签: java arrays hadoop split hive


    【解决方案1】:

    使用单线器

    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)。

    所以是的,您将能够使用来自Apache CommonsString.splitStringUtils,而不会出现任何问题。

    【讨论】:

    • 你确定我可以在蜂巢中使用它吗?
    • 我不太确定是否可以直接在 hive 中使用它。但也许这可以用来创建一个 UDF ......我对 java-world 很陌生 :-)
    • @DatamineR 正在查看 hive 文档,是的,您可以使用我的解决方案。
    【解决方案2】:

    还要注意 google guava 库。其实Splitter就是这么强大 https://github.com/google/guava/wiki/StringsExplained#splitter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2015-10-04
      • 2022-11-27
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多