【问题标题】:Write a Hive UDF function that takes variable number of args and outpts a JSON blob编写一个 Hive UDF 函数,该函数采用可变数量的 args 并输出 JSON blob
【发布时间】:2016-01-27 21:08:07
【问题描述】:

我想编写一个 Hive UDF,它采用可变数量的参数(不同类型)并将其输出为 JSON blob(列名到列值映射)。

Select userId, myudf(col2, col3) from TABLE 2; // the output of udf should be {"col2":50, "col3":"Y" }

Select userId, myudf(col2, col3, col4) from TABLE 1; // the output of udf should be {"col2":"s", "col3":5, "col4":"Y"}

Select userId, myudf(col2, col3, col4, col6, col7) from TABLE 3; //the output of udf should be {"col2":"M", "col3":"A", "col4":2.5, "col6":"D", "col7":99 }

每个表都有不同类型的不同列(userId 在所有列中都是通用的)。如果有帮助,我可以单独传递列名:myudf("col2", col2, "col3", col3)。任何想法将不胜感激。

【问题讨论】:

  • Hive UDF 的参数不只是一个元组吗?如果是这样,您可以为每个位置参数执行tuple.get(n)
  • 您可以尝试将连接的行作为参数传递并在 UDF 中拆分以创建 JSON
  • cricket_007@,是的,它只是一个元组。问题是参数的类型信息(如果我假设我传递了列名,因为 UDF 不会提供该信息)。要转换为 JSON,我需要知道参数的类型,因为所有内容都将作为通用对象传递给 UDF。我也可以传递一些类型信息(每列多一个参数),但我正在寻找更好的解决方案。

标签: json hadoop hive udf


【解决方案1】:

您应该使用 GenericUDF 对象(按 UDF 对象的顺序)。

Mark Grover 写了一篇关于 http://mark.thegrovers.ca/tech-blog/how-to-write-a-hive-udf 的精彩博客文章

这里是相关的源代码:https://github.com/markgrover/hive-translate/blob/master/src/main/java/org/mgrover/hive/translate/GenericUDFTranslate.java

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2018-07-04
    • 1970-01-01
    • 2012-06-08
    相关资源
    最近更新 更多