Hive UDTF返回ArrayList列

时间:2014-12-18 05:42:16

标签: java hadoop arraylist hive

我是Hive UDTF的新手。我有一个要求,我必须在UDTF中将字符串值作为Paratmeter传递,并且返回的列应该是ArrayList。

我写了以下代码:

public StructObjectInspector initialize(ObjectInspector[] arg0)
        throws UDFArgumentException {
        ArrayList<String> fieldNames = new ArrayList<String>();
        ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
        fieldNames.add("col1");
        stringOI = (PrimitiveObjectInspector) arg0[0];
       listOi=(ListObjectInspector) arg0[0];
        fieldOIs.add(listOi.getListElementObjectInspector());
        return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}

@Override
public void process(Object[] record) throws HiveException {
    // TODO Auto-generated method stub
     String document = (String) stringOI.getPrimitiveJavaObject(record[0]);
     if (document == null) {
          return;
        }
    firstColumn=(String) stringOI.getPrimitiveJavaObject(record[0]);
    secondColumn=(String) stringOI.getPrimitiveJavaObject(record[1]);
    if(outputMapper.containsKey(firstColumn))
    {

        ArrayList<String> tempList=new ArrayList<String>();
        tempList=outputMapper.get(firstColumn);
        tempList.add(secondColumn);
        outputMapper.put(firstColumn,tempList);
    }
    else
    {
            childVendorList=new ArrayList<String>();
            childVendorList.add(secondColumn);
            outputMapper.put(firstColumn,childVendorList);
    }
    forward(outputMapper.get(firstColumn));

}

}

我得到以下例外:

java.lang.ClassCastException:org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyStringObjectInspector无法强制转换为org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector

任何人都可以帮忙???

1 个答案:

答案 0 :(得分:2)

listOi=(ListObjectInspector) arg0[0];
fieldOIs.add(listOi.getListElementObjectInspector());
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);

这个arg0 [0]是一个原始对象检查器。使用listOi.getListElementObjectInspector(),只需获取类似的PrimitiveObjectInspector(如String,Integer不是List)。它应该

fieldOIs.add(ObjectInspectorFactory.getStandardListObjectInspector(stringOI ))

这将使用stringOI类型的List特定输出列。