您的位置:首页 > 移动开发 > Objective-C

udf (MapObjectInspector的使用)

2014-07-24 21:37 344 查看
public class DimensionConvertor extends GenericUDF {
MapObjectInspector valueIO;
StringObjectInspector typeIO;

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
Map<Text, Text> value = (Map<Text, Text>) this.valueIO.getMap(arguments[0].get());
//		Map<String, String> value = (Map<String, String>) this.valueIO.getMap(arguments[0].get());
String type = this.typeIO.getPrimitiveJavaObject(arguments[1].get());

Text abc = value.get(new Text("app_id"));
//		for (Map.Entry<Text, Text> entry : value.entrySet()) {
//			abc = entry.getKey();
//		}

return abc;
}

@Override
public String getDisplayString(String[] arg0) {
// TODO Auto-generated method stub
return "language id";
}

@Override
public ObjectInspector initialize(ObjectInspector[] arguments)
throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentLengthException("should be two argumentus");
}
ObjectInspector value = arguments[0];
ObjectInspector type = arguments[1];
if (!(value instanceof MapObjectInspector)) {
throw new UDFArgumentLengthException("argument is not right type");
}
if (!(type instanceof StringObjectInspector)) {
throw new UDFArgumentLengthException("argument is not right type");
}
this.valueIO = (MapObjectInspector) value;
this.typeIO = (StringObjectInspector) type;
return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
}

}

hql: select trans_dimension_id(map('app_id',uid), 'app') from test_cid_uid_language;

MapObjectInspector中的键值对都是Text的类型,这样才能进行正反序列化,所以在写UDF时,需要写出正确的泛型,才能从参数中取到键和值,否则只能取到null值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: