【问题标题】:Hive : reflect functionHive:反射功能
【发布时间】:2016-07-11 14:09:12
【问题描述】:

我正在尝试使用具有此签名的 Hive 的 Reflect 函数:

reflect(class, method[, arg1[, arg2..]])

我想检查一个值为hello world ! 的列c 是否包含world,所以我写了:

with a as
(select "hello world !" as c)
select reflect("java.lang.String",c ,"contains", "world") from a

但它没有工作,因为它不尊重签名,所以我尝试了这个

with a as
(select "hello world !" as c)
select reflect(reflect("java.lang.Object","toString",c) ,"contains", "world") from a

它也没有用!我想知道如何在给定的列上应用reflect 函数?

【问题讨论】:

  • Hive 手册中的所有示例都适用于 static 方法 (cwiki.apache.org/confluence/display/Hive/ReflectUDF) 并且源代码似乎创建了一个新的“类”实例通过调用 constructor-with-no-argument (grepcode.com/file/repo1.maven.org/maven2/org.apache.hive/…) 所以我认为你注定要失败。尝试自定义 UDF。
  • 顺便说一句,我猜你的用例涉及比一些简单的字符串原语更复杂的东西(参见case when C like '%world%' then True else False end
  • 谢谢@SamsonScharfrichter,但他们在文档中说:Reflect uses Java reflection to instantiate and call methods of objects; it can also call static functions。我的目标是掌握反射功能而不是like 示例。
  • @SamsonScharfrichter,Hive 手册中的isEmpty 方法呢?示例中使用的参数是什么?如何将其应用于列?
  • 不知道...看看源代码,如果你能理解的话:-/

标签: hadoop reflection hive hql


【解决方案1】:

reflect2 会有所帮助。见https://issues.apache.org/jira/browse/HIVE-20007

select reflect2("stackoverflow","length");  
+------+--+
| _c0  |
+------+--+
| 13   |
+------+--+

但是 hashCode() 不起作用。见https://github.com/apache/hive/blob/master/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFReflect2.java#L86

select reflect2("stackoverflow","hashCode");
Error: Error while compiling statement: FAILED: SemanticException [Error 10016]: Line 1:32 Argument type mismatch '"hashCode"': Use hash() UDF instead of this.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多