简体   繁体   中英

Return ArrayList<String> in HIVE UDF

I've created a UDF function which takes two text arguments and return ArrayList. But when I call the UDF function in Hive, it gives me an error.

Here is a snippet of my UDF code:

public class MyTestUDF extends UDF {
    public ArrayList<String> evaluate(Text i, Text s) {

        if(s == null) return null;

        String id = i.toString();
        String value = s.toString();

        <parse string value to v1, v2, and v3, and apply logic>

        ArrayList<String> result = new ArrayList<String>();
        result.add(id);
        result.add(v1);
        result.add(v2);
        result.add(v3);

        return result;
    }       
}

And here is how I ran in Hive:

hive> SELECT multi[0] AS id,
             multi[1] AS value1, 
             multi[2] AS value2,
             multi[3] AS value3
      FROM (SELECT my_udf_function(id, data) AS multi FROM testDB) bar;

FAILED: SemanticException [Error 10033]: Line 1:7 [] not valid on 
        non-collection types '0': struct<elementdata:struct<>,size:int>

The data is a giant string value that I parse and apply logic and return three values as a format of ArrayList.

I referred this link Returning & Using Multiple Values from a HIVE UDF but it doesn't work for me.

Can anyone please help?

Thanks!

Change ArrayList<String> to ArrayList<Text> , because Hive requires serializable types such as FloatWritable , IntWritable , or Text . For more information, I suggest Returning & Using Multiple Values from a HIVE UDF , easy to read and explaining what to do after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM