我想创建一个UDF,它将一个未确定的结构作为输入,并将相同的结构作为输出。
我想在两个不同的结构上使用相同的函数:
select multiple_prop(struct_1) as new_struct_1, multiple_prop(struct_2) as new_struct_2
from
(
select
named_struct('hello',2,'bye') as struct_1
,named_struct(23,444) as struct_2
from table1
)a
这是我到目前为止所做的事情:
public class multiple_prop extends GenericUDF {
private StructObjectInspector moi;
private Object[] result;
@Override
public ObjectInspector initialize(ObjectInspector[] arguments)
throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentException("Usage : multiple_prop(struct) ");
}
if (!arguments[0].getCategory().equals(Category.STRUCT)) {
throw new UDFArgumentException("Usage : multiple_prop(struct) ");
}
moi = (StructObjectInspector) arguments[0];
ArrayList structFieldNames = new ArrayList();
ArrayList structFieldObjectInspectors = new ArrayList();
for(int i=0;i< moi.getAllStructFieldRefs().size();i++ )
{
structFieldNames.add(moi.getAllStructFieldRefs().get(i).getFieldName());
structFieldObjectInspectors.add(moi.getAllStructFieldRefs().get(i).getFieldObjectInspector());
}
StructObjectInspector si;
return si = ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames, structFieldObjectInspectors);
}
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
result = new Object[moi.getAllStructFieldRefs().size()];
int n_elems = moi.getAllStructFieldRefs().size();
for (int i=0;i<n_elems;i++)
{
result[i]=moi.getStructFieldRef(moi.getAllStructFieldRefs().get(i).getFieldName());
}
return result;
}
@Override
public String getDisplayString(String[] children) {
return "fin";
}
}
这会带来此错误:
org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector$MyField cannot be cast to org.apache.hadoop.io.IntWritable
我希望代码本身不起作用,因为我附带的所有教程都有一组预定义的 structFieldNames 和 structFieldObjectInspectors 成员(意味着它们只适用于已定义的类型)结构)。
所以,我很确定问题来自这些代码:
for(int i=0;i< moi.getAllStructFieldRefs().size();i++ )
{
structFieldNames.add(moi.getAllStructFieldRefs().get(i).getFieldName());
structFieldObjectInspectors.add(moi.getAllStructFieldRefs().get(i).getFieldObjectInspector());
}
和
result[i]=moi.getStructFieldRef(moi.getAllStructFieldRefs().get(i).getFieldName());
希望你能伸出援助之手。