Home | About | Sematext search-lucene.com search-hadoop.com
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB
 Search Hadoop and all its subprojects:

Switch to Threaded View
Hive >> mail # user >> A GenericUDF Function to Extract a Field From an Array of Structs


Copy link to this message
-
Re: A GenericUDF Function to Extract a Field From an Array of Structs
try to change codes in evaluate method like,

for (int i = 0; i < numElements; i++) {
      Object element = listOI.getListElement(arguments[0].get(), i);
      Object product = structOI.getStructFieldData(element,
structOI.getStructFieldRef("productCategory"));
      ret.add(((PrimitiveObjectInspector)prodCatOI).getPrimitiveWritableObject(product));
}

2013/3/29 Peter Chu <[EMAIL PROTECTED]>:
> Sorry, the test should be following (changed extract_shas to
> extract_product_category):
>
> import org.apache.hadoop.hive.ql.metadata.HiveException;
> import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
> import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject;
> import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
> import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
> import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
> import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
> import
> org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
> import org.testng.annotations.Test;
>
> import java.util.ArrayList;
> import java.util.List;
>
> public class TestGenericUDFExtractProductCategory
> {
>     ArrayList<String> fieldNames = new ArrayList<String>();
>     ArrayList<ObjectInspector> fieldObjectInspectors = new
> ArrayList<ObjectInspector>();
>
>     @Test
>     public void simpleTest()
>         throws Exception
>     {
>         ListObjectInspector firstInspector = new MyListObjectInspector();
>
>         ArrayList test = new ArrayList();
>         test.add("test");
>
>         ArrayList test2 = new ArrayList();
>         test2.add(test);
>
>         StructObjectInspector soi > ObjectInspectorFactory.getStandardStructObjectInspector(test, test2);
>
>         fieldNames.add("productCategory");
>
> fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
>
>         GenericUDF.DeferredObject firstDeferredObject = new
> MyDeferredObject(test2);
>
>         GenericUDF extract_product_category = new
> GenericUDFExtractProductCategory();
>
>         extract_product_category.initialize(new
> ObjectInspector[]{firstInspector});
>
>         extract_product_category.evaluate(new
> DeferredObject[]{firstDeferredObject});
>     }
>
>     public class MyDeferredObject implements DeferredObject
>     {
>         private Object value;
>
>         public MyDeferredObject(Object value) {
>             this.value = value;
>         }
>
>         @Override
>         public Object get() throws HiveException
>         {
>             return value;
>         }
>     }
>
>     private class MyListObjectInspector implements ListObjectInspector
>     {
>         @Override
>         public ObjectInspector getListElementObjectInspector()
>         {
>             return
> ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,
> fieldObjectInspectors);
>         }
>
>         @Override
>         public Object getListElement(Object data, int index)
>         {
>             List myList = (List) data;
>             if (myList == null || index > myList.size()) {
>                 return null;
>             }
>             return myList.get(index);
>         }
>
>         @Override
>         public int getListLength(Object data)
>         {
>             if (data == null) {
>                 return -1;
>             }
>             return ((List) data).size();
>         }
>
>         @Override
>         public List<?> getList(Object data)
>         {
>             return (List) data;
>         }
>
>         @Override
>         public String getTypeName()
>         {
>             return null;  //To change body of implemented methods use File |
> Settings | File Templates.
>         }
>
>         @Override
>         public Category getCategory()
>         {
>             return Category.LIST;
>         }
>     }
> }
>
> ________________________________
> From: [EMAIL PROTECTED]
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB