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值
相关文章推荐
- MapX系列-- 使用LayerInfo object 在map中添加已有的矢量图层
- MapX系列-- 使用LayerInfo object 在map中添加栅格图层
- 2 TileMapObject的使用
- *Delphi控件之---UpDown以及其与TEdit的配合使用(比如限制TEdit只能输入数字,还有Object Inspector之组件属性的介绍)
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- MapObject控件的使用之加入图层
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- 使用JSONObject(json-lib)格式化bean,map,list!!!
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- java 使用反射转换对象 Object to Map
- MapObject控件的使用之图层操作
- MapX系列-- 使用LayerInfo object 在map中添加栅格图层
- 使用Map把sql返回的list<Object>转成需要的实体对象
- 2 TileMapObject的使用
- java Map使用Object 做为Key的问题
- MapObject控件的使用之加入图层
- MapObject控件的使用之改变图层颜色
- MapX系列-- 使用LayerInfo object 在map中添加栅格图层
- [C++/object c]_[初级]_[std::map容器的使用总结和NSDictionary词典使用总结]
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值