hive 查询特定联合类型的记录

标签 hive unions

我有一个示例配置单元表创建为

CREATE TABLE union_test(foo UNIONTYPE<int, double, array<string>, struct<a:int,b:string>>);

数据可以看作
SELECT foo FROM union_test;

输出是
{0:1}
{1:2.0}
{2:["three","four"]}
{3:{"a":5,"b":"five"}}
{2:["six","seven"]}
{3:{"a":8,"b":"eight"}}
{0:9}
{1:10.0}

第一个字段(标签)表示联合的类型(0 表示 int,1 表示 double ,2 表示数组等)。

我的问题是,如果我发现只选择联合类型为 2(数组)的那些记录,我应该如何构建我的查询?

最佳答案

Hive 中没有从 UnionType 读取数据的函数。所以我写了2个UDF。一个是获取 Union 标签(你尝试做的),第二个是从 union 获取 struct 作为示例。

get_union_tag() 函数:

 package HiveUDF;
 import org.apache.hadoop.hive.ql.exec.Description;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;


 @Description(name = "get_union_tag", value = "_FUNC_(unionObject)"
    + " - Returns union object Tag", extended = "Example:\n" + "  > SELECT _FUNC_(unionObject) FROM src LIMIT 1;\n one")
 public class GetUnionTag extends GenericUDF {  

// Global variables that inspect the input.
// These are set up during the initialize() call, and are then used during the
// calls to evaluate()
private transient UnionObjectInspector uoi;

@Override
// This is what we do in the initialize() method:
// Verify that the input is of the type expected
// Set up the ObjectInspectors for the input in global variables
// Return the ObjectInspector for the output
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {        

    // Verify the input is of the required type.
    // Set the global variables (the various ObjectInspectors) while we're doing this

    // Exactly one input argument
    if( arguments.length != 1 ){
        throw new UDFArgumentLengthException("_FUNC_(unionObject) accepts exactly one argument.");
    }
    // Is the input an array<>
    if( arguments[0].getCategory() != ObjectInspector.Category.UNION ){
        throw new UDFArgumentTypeException(0,"The single argument to AddExternalIdToPurchaseDetails should be "
                + "Union<>"
                + " but " + arguments[0].getTypeName() + " is found");

    }

    // Store the ObjectInspectors for use later in the evaluate() method
    uoi = ((UnionObjectInspector)arguments[0]);

    // Set up the object inspector for the output, and return it
    return PrimitiveObjectInspectorFactory.javaByteObjectInspector;
}

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {

    byte tag =  uoi.getTag(arguments[0].get());
    return tag;
}

@Override
public String getDisplayString(String[] children) {

    StringBuilder sb = new StringBuilder();
    sb.append("get_union_tag(");
    for (int i = 0; i < children.length; i++) {
        if (i > 0) {
            sb.append(',');
        }
        sb.append(children[i]);
    }
    sb.append(')');
    return sb.toString();
}

}

函数 get_struct_from_union() UDF :
package HiveUDF;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector;

@Description(name = "get_union_struct", value = "_FUNC_(unionObject)"
    + " - Returns struct ", extended = "Example:\n" + "  > _FUNC_(unionObject).value \n 90.0121")
public class GetUnionStruct extends GenericUDF {

// Global variables that inspect the input.
// These are set up during the initialize() call, and are then used during the
// calls to evaluate()
//
// ObjectInspector for the list (input array<>)
// ObjectInspector for the struct<>
// ObjectInspectors for the elements of the struct<>, target, quantity and price
private UnionObjectInspector unionObjectInspector;
private StructObjectInspector structObjectInspector;

@Override
// This is what we do in the initialize() method:
// Verify that the input is of the type expected
// Set up the ObjectInspectors for the input in global variables
// Return the ObjectInspector for the output
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {        

    // Verify the input is of the required type.
    // Set the global variables (the various ObjectInspectors) while we're doing this

    // Exactly one input argument
    if( arguments.length != 1 ){
        throw new UDFArgumentLengthException("_FUNC_(unionObject) accepts exactly one argument.");
    }
    // Is the input an array<>
    if( arguments[0].getCategory() != ObjectInspector.Category.UNION ){
        throw new UDFArgumentTypeException(0,"The single argument to AddExternalIdToPurchaseDetails should be "
                + "Union<Struct>"
                + " but " + arguments[0].getTypeName() + " is found");

    }        

    // Set up the object inspector for the output, and return it
    return structObjectInspector;
}

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {

    return ((UnionObjectInspector) unionObjectInspector).getField(arguments[0].get());
}

@Override
public String getDisplayString(String[] children) {

    StringBuilder sb = new StringBuilder();
    sb.append("get_union_vqtstruct(");
    for (int i = 0; i < children.length; i++) {
        if (i > 0) {
            sb.append(',');
        }
        sb.append(children[i]);
    }
    sb.append(')');
    return sb.toString();
}

}

使用这些 UDF 编译并创建 jar 文件。比上传到配置单元(在我的情况下是 HDInsight)。不仅仅是使用
add jar wasb:///hive/HiveGUDF.jar;
CREATE TEMPORARY FUNCTION get_union_struct AS 'HiveUDF.GetUnionStruct';

在你运行之前,例如
SELECT get_union_tag(exposed) FROM test;

关于hive 查询特定联合类型的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362110/

相关文章:

C# Union vs Contains 用于连续数据列表

python - 无法将python变量传递给hive -e

hadoop - 无法使用配置单元聚合功能获得预期的输出

hadoop - Hive 暂停和恢复任务

c - 如何在 C 中的结构内、 union 内使用结构?

c++ - c 中的 union 一次存储一个数据

c++ - Union hack 用于字节序测试和字节交换

hadoop - 没有管理员权限的用户可以管理配置单元中的对象访问权限吗?

scala - 实例化 'org.apache.spark.sql.hive.HiveSessionState'时出错:在Linux服务器上

c - C 中未命名的结构/union 有什么好处?