如何在udf中为一种类型的数组编写测试[struct [a:string,b:map [string,string]]]?输入包括一个字符串和一个数组[struct [a:string,b: map [string,string]]]类型。我的代码是fllows:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
public class UDFClkNum extends GenericUDF {
private ListObjectInspector listOI;
private StructObjectInspector structOI;
private MapObjectInspector mapOI;
private StringObjectInspector typeOI;
@Override
public Object evaluate(DeferredObject[] arg0) throws HiveException {
// TODO Auto-generated method stub
int count = 0;
List list = listOI.getList(arg0[1].get());
String type = typeOI.getPrimitiveJavaObject(arg0[0]);
Pattern p1 = Pattern.compile(".*_" + type + "$");
if (null == list)
return 0;
for (int i = 0; i < list.size(); ++i) {
Map map = (Map) structOI.getStructFieldData(listOI.getListElement(arg0[1], i), structOI.getStructFieldRef("action_attribute"));
if (null == map)
return 0;
else {
Matcher m1 = p1.matcher((String)map.get("action_attribute"));
if (m1.find())
++count;
}
}
return count;
}
@Override
public String getDisplayString(String[] arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public ObjectInspector initialize(ObjectInspector[] arg0)
throws UDFArgumentException {
if (2 != arg0.length) {
throw new UDFArgumentException("arg0 only takes 2 argument");
}
ObjectInspector a = arg0[0];
ObjectInspector b = arg0[1];
if (!(a instanceof StringObjectInspector)) {
throw new UDFArgumentException("the argument must be a String");
}
if (!(b instanceof ListObjectInspector)) {
throw new UDFArgumentException("the argument must be an array / list");
}
this.typeOI = (StringObjectInspector) a;
this.listOI = (ListObjectInspector) b;
if(!(listOI.getListElementObjectInspector() instanceof StructObjectInspector)) {
throw new UDFArgumentException("the argument must be a list of struct");
}
this.structOI = (StructObjectInspector) listOI.getListElementObjectInspector();
MapObjectInspector mapOI = (MapObjectInspector) structOI.getStructFieldRef("action_attribute");
return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
}
public static void main(String[] args) throws HiveException {
UDFClkNum clk = new UDFClkNum();
ObjectInspector stringOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
List<String> structFieldNames = new ArrayList<String>();
List<ObjectInspector> structFieldObjectInspectors = new ArrayList<ObjectInspector>();
structFieldNames.add("action_attribute");
structFieldObjectInspectors.add(ObjectInspectorFactory.getStandardMapObjectInspector(stringOI, stringOI));
ObjectInspector structOI = ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames, structFieldObjectInspectors);
ObjectInspector listOI = ObjectInspectorFactory.getStandardListObjectInspector(structOI);
DeferredObject[] objs = new DeferredObject[2];
objs[0] = new DeferredJavaObject("k1");
//objs[1] = new DeferredJavaObject();
Object result = clk.evaluate(objs);
System.out.println(result);
}
}
我无法处理objs[1] = new DeferredJavaObject();
。我不知道如何初始化objs [1]。