Last active
April 1, 2020 13:04
-
-
Save vector4wang/4b6fa4a30f9dd18470e385032ef2fd2a to your computer and use it in GitHub Desktop.
[hive udf map] 'a','b','c' -> {"key1":"a","key2":"b","key3":"c"}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MapUdfSample extends GenericUDF { | |
private final Map<Text, Text> resultMap = new LinkedHashMap<Text, Text>(); | |
private final Map<String, String> config_map = new LinkedHashMap<String, String>(); | |
private transient ObjectInspector[] argumentOIs; | |
public void initConfigMap(){ | |
config_map.put("a","100"); | |
config_map.put("b","200"); | |
config_map.put("c","300"); | |
config_map.put("e","400"); | |
} | |
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { | |
if (arguments.length == 0) { | |
throw new UDFArgumentException("UrlParamsToMap param must be more argus."); | |
} | |
initConfigMap(); | |
argumentOIs = arguments; | |
return ObjectInspectorFactory | |
.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector, | |
PrimitiveObjectInspectorFactory.writableStringObjectInspector); | |
} | |
public Object evaluate(DeferredObject[] arguments) throws HiveException { | |
Object key1 = arguments[0].get(); | |
Object key2 = arguments[1].get(); | |
Object key3 = arguments[2].get(); | |
StringObjectInspector soi0 = (StringObjectInspector)argumentOIs[0]; | |
StringObjectInspector soi1 = (StringObjectInspector)argumentOIs[1]; | |
StringObjectInspector soi2 = (StringObjectInspector)argumentOIs[2]; | |
String key1Str = soi0.getPrimitiveJavaObject(key1); | |
String key2Str = soi1.getPrimitiveJavaObject(key2); | |
String key3Str = soi2.getPrimitiveJavaObject(key3); | |
resultMap.put(new Text("key1"), new Text(key1Str)); | |
resultMap.put(new Text("key2"), new Text(key2Str)); | |
resultMap.put(new Text("key3"), new Text(config_map.get(key3Str))); | |
return resultMap; | |
} | |
public String getDisplayString(String[] children) { | |
return "map(" + children[0] + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment