赞
踩
- class SimpleUDFExample extends UDF {
-
- public Text evaluate(Text input) {
- return new Text("Hello " + input.toString());
- }
- }
- public class SimpleUDFExampleTest {
-
- @Test
- public void testUDF() {
- SimpleUDFExample example = new SimpleUDFExample();
- Assert.assertEquals("Hello world", example.evaluate(new Text("world")).toString());
- }
- }
%> hive
hive> ADD JAR target/hive-extensions-1.0-SNAPSHOT-jar-with-dependencies.jar;
hive> CREATE TEMPORARY FUNCTION helloworld as 'com.matthewrathbone.example.SimpleUDFExample';
hive> select helloworld(name) from people limit 1000;
- class SimpleUDFExample extends UDF {
-
- public Text evaluate(Text input) {
- if(input == null) return null;
- return new Text("Hello " + input.toString());
- }
- }
- @Test
- public void testUDFNullCheck() {
- SimpleUDFExample example = new SimpleUDFExample();
- Assert.assertNull(example.evaluate(null));
- }
- // 这个类似于简单API的evaluat方法,它可以读取输入数据和返回结果
- abstract Object evaluate(GenericUDF.DeferredObject[] arguments);
-
- // 该方法无关紧要,我们可以返回任何东西,但应当是描述该方法的字符串
- abstract String getDisplayString(String[] children);
-
- // 只调用一次,在任何evaluate()调用之前,你可以接收到一个可以表示函数输入参数类型的object inspectors数组
- // 这是你用来验证该函数是否接收正确的参数类型和参数个数的地方
- abstract ObjectInspector initialize(ObjectInspector[] arguments);
- containsString(List("a", "b", "c"), "b"); // true
-
- containsString(List("a", "b", "c"), "d"); // false
- class ComplexUDFExample extends GenericUDF {
-
- ListObjectInspector listOI;
- StringObjectInspector elementOI;
-
- @Override
- public String getDisplayString(String[] arg0) {
- return "arrayContainsExample()"; // this should probably be better
- }
-
- @Override
- public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
- if (arguments.length != 2) {
- throw new UDFArgumentLengthException("arrayContainsExample only takes 2 arguments: List<T>, T");
- }
- // 1. 检查是否接收到正确的参数类型
- ObjectInspector a = arguments[0];
- ObjectInspector b = arguments[1];
- if (!(a instanceof ListObjectInspector) || !(b instanceof StringObjectInspector)) {
- throw new UDFArgumentException("first argument must be a list / array, second argument must be a string");
- }
- this.listOI = (ListObjectInspector) a;
- this.elementOI = (StringObjectInspector) b;
-
- // 2. 检查list是否包含的元素都是string
- if(!(listOI.getListElementObjectInspector() instanceof StringObjectInspector)) {
- throw new UDFArgumentException("first argument must be a list of strings");
- }
-
- // 返回类型是boolean,所以我们提供了正确的object inspector
- return PrimitiveObjectInspectorFactory.javaBooleanObjectInspector;
- }
-
- @Override
- public Object evaluate(DeferredObject[] arguments) throws HiveException {
-
- // 利用object inspectors从传递的对象中得到list与string
- List<String> list = (List<String>) this.listOI.getList(arguments[0].get());
- String arg = elementOI.getPrimitiveJavaObject(arguments[1].get());
-
- // 检查空值
- if (list == null || arg == null) {
- return null;
- }
-
- // 判断是否list中包含目标值
- for(String s: list) {
- if (arg.equals(s)) return new Boolean(true);
- }
- return new Boolean(false);
- }
-
- }
- public class ComplexUDFExampleTest {
-
- @Test
- public void testComplexUDFReturnsCorrectValues() throws HiveException {
-
- // 建立需要的模型
- ComplexUDFExample example = new ComplexUDFExample();
- ObjectInspector stringOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
- ObjectInspector listOI = ObjectInspectorFactory.getStandardListObjectInspector(stringOI);
- JavaBooleanObjectInspector resultInspector = (JavaBooleanObjectInspector) example.initialize(new ObjectInspector[]{listOI, stringOI});
-
- // create the actual UDF arguments
- List<String> list = new ArrayList<String>();
- list.add("a");
- list.add("b");
- list.add("c");
-
- // 测试结果
-
- // 存在的值
- Object result = example.evaluate(new DeferredObject[]{new DeferredJavaObject(list), new DeferredJavaObject("a")});
- Assert.assertEquals(true, resultInspector.get(result));
-
- // 不存在的值
- Object result2 = example.evaluate(new DeferredObject[]{new DeferredJavaObject(list), new DeferredJavaObject("d")});
- Assert.assertEquals(false, resultInspector.get(result2));
-
- // 为null的参数
- Object result3 = example.evaluate(new DeferredObject[]{new DeferredJavaObject(null), new DeferredJavaObject(null)});
- Assert.assertNull(result3);
- }
- }
翻译来自于
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。