Search in sources :

Example 6 with StandardListObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector in project cdap by caskdata.

the class StandardObjectInspectorsTest method testCollectionObjectInspector.

@Test
public void testCollectionObjectInspector() throws Throwable {
    // Test with sets
    ObjectInspector oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<Set<String>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    StandardListObjectInspector loi = (StandardListObjectInspector) oi;
    Set<String> set = Sets.newHashSet("foo", "bar", "foobar");
    List<?> inspectedSet = loi.getList(set);
    Assert.assertTrue(inspectedSet.contains("foo"));
    Assert.assertTrue(inspectedSet.contains("bar"));
    Assert.assertTrue(inspectedSet.contains("foobar"));
    // Test with queues
    oi = ObjectInspectorFactory.getReflectionObjectInspector(new TypeToken<Queue<String>>() {
    }.getType());
    Assert.assertTrue(oi instanceof StandardListObjectInspector);
    loi = (StandardListObjectInspector) oi;
    Queue<String> queue = new LinkedList<>();
    queue.add("foo");
    queue.add("bar");
    List<?> inspectedQueue = loi.getList(set);
    Assert.assertEquals("bar", inspectedQueue.get(0));
    Assert.assertEquals("foo", inspectedQueue.get(1));
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) TypeToken(com.google.common.reflect.TypeToken) Queue(java.util.Queue) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 7 with StandardListObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector in project hive by apache.

the class TestGenericUDFMapValues method testNullMap.

@Test
public void testNullMap() throws HiveException, IOException {
    ObjectInspector[] inputOIs = { ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector) };
    Map<String, String> input = null;
    DeferredObject[] args = { new DeferredJavaObject(input) };
    GenericUDFMapValues udf = new GenericUDFMapValues();
    StandardListObjectInspector oi = (StandardListObjectInspector) udf.initialize(inputOIs);
    Object res = udf.evaluate(args);
    Assert.assertTrue(oi.getList(res).isEmpty());
    udf.close();
}
Also used : StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) Test(org.junit.Test)

Example 8 with StandardListObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector in project hive by apache.

the class MatchPath method createSelectListRR.

/*
   * add array<struct> to the list of columns
   */
protected static RowResolver createSelectListRR(MatchPath evaluator, PTFInputDef inpDef) throws SemanticException {
    RowResolver rr = new RowResolver();
    RowResolver inputRR = inpDef.getOutputShape().getRr();
    evaluator.inputColumnNamesMap = new HashMap<String, String>();
    ArrayList<String> inputColumnNames = new ArrayList<String>();
    ArrayList<ObjectInspector> inpColOIs = new ArrayList<ObjectInspector>();
    for (ColumnInfo inpCInfo : inputRR.getColumnInfos()) {
        ColumnInfo cInfo = new ColumnInfo(inpCInfo);
        String colAlias = cInfo.getAlias();
        String[] tabColAlias = inputRR.reverseLookup(inpCInfo.getInternalName());
        if (tabColAlias != null) {
            colAlias = tabColAlias[1];
        }
        ASTNode inExpr = null;
        inExpr = PTFTranslator.getASTNode(inpCInfo, inputRR);
        if (inExpr != null) {
            rr.putExpression(inExpr, cInfo);
            colAlias = inExpr.toStringTree().toLowerCase();
        } else {
            colAlias = colAlias == null ? cInfo.getInternalName() : colAlias;
            rr.put(cInfo.getTabAlias(), colAlias, cInfo);
        }
        evaluator.inputColumnNamesMap.put(cInfo.getInternalName(), colAlias);
        inputColumnNames.add(colAlias);
        inpColOIs.add(cInfo.getObjectInspector());
    }
    StandardListObjectInspector pathAttrOI = ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardStructObjectInspector(inputColumnNames, inpColOIs));
    ColumnInfo pathColumn = new ColumnInfo(PATHATTR_NAME, TypeInfoUtils.getTypeInfoFromObjectInspector(pathAttrOI), null, false, false);
    rr.put(null, PATHATTR_NAME, pathColumn);
    return rr;
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ArrayList(java.util.ArrayList) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) ColumnInfo(org.apache.hadoop.hive.ql.exec.ColumnInfo) StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector) RowResolver(org.apache.hadoop.hive.ql.parse.RowResolver)

Example 9 with StandardListObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector in project SQLWindowing by hbutani.

the class NPathUtils method createSelectListInputOI.

/*
	 * add array<struct> to the list of columns
	 */
public static ObjectInspector createSelectListInputOI(StructObjectInspector inputOI) {
    List<? extends StructField> fields = inputOI.getAllStructFieldRefs();
    ArrayList<ObjectInspector> selectListFieldOIs = new ArrayList<ObjectInspector>();
    ArrayList<String> selectListFieldNames = new ArrayList<String>();
    StandardListObjectInspector pathAttrOI = ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorUtils.getStandardObjectInspector(inputOI));
    for (StructField f : fields) {
        selectListFieldOIs.add(ObjectInspectorUtils.getStandardObjectInspector(f.getFieldObjectInspector()));
        selectListFieldNames.add(f.getFieldName());
    }
    selectListFieldOIs.add(pathAttrOI);
    selectListFieldNames.add(PATHATTR_NAME);
    return ObjectInspectorFactory.getStandardStructObjectInspector(selectListFieldNames, selectListFieldOIs);
}
Also used : StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) ArrayList(java.util.ArrayList) StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector)

Example 10 with StandardListObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector in project haivvreo by jghoman.

the class TestAvroDeserializer method canDeserializeArrays.

@Test
public void canDeserializeArrays() throws SerDeException, IOException {
    Schema s = Schema.parse(TestAvroObjectInspectorGenerator.ARRAY_WITH_PRIMITIVE_ELEMENT_TYPE);
    GenericData.Record record = new GenericData.Record(s);
    List<String> list = new ArrayList<String>();
    list.add("Eccleston");
    list.add("Tennant");
    list.add("Smith");
    record.put("anArray", list);
    assertTrue(GENERIC_DATA.validate(s, record));
    System.out.println("Array-backed record = " + record);
    AvroGenericRecordWritable garw = Utils.serializeAndDeserializeRecord(record);
    AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
    AvroDeserializer de = new AvroDeserializer();
    ArrayList<Object> row = (ArrayList<Object>) de.deserialize(aoig.getColumnNames(), aoig.getColumnTypes(), garw, s);
    assertEquals(1, row.size());
    Object theArrayObject = row.get(0);
    assertTrue(theArrayObject instanceof List);
    List theList = (List) theArrayObject;
    // Verify the raw object that's been created
    assertEquals("Eccleston", theList.get(0));
    assertEquals("Tennant", theList.get(1));
    assertEquals("Smith", theList.get(2));
    // Now go the correct way, through objectinspectors
    StandardStructObjectInspector oi = (StandardStructObjectInspector) aoig.getObjectInspector();
    StructField fieldRefToArray = oi.getStructFieldRef("anArray");
    Object anArrayData = oi.getStructFieldData(row, fieldRefToArray);
    StandardListObjectInspector anArrayOI = (StandardListObjectInspector) fieldRefToArray.getFieldObjectInspector();
    assertEquals(3, anArrayOI.getListLength(anArrayData));
    JavaStringObjectInspector elementOI = (JavaStringObjectInspector) anArrayOI.getListElementObjectInspector();
    Object firstElement = anArrayOI.getListElement(anArrayData, 0);
    assertEquals("Eccleston", elementOI.getPrimitiveJavaObject(firstElement));
    assertTrue(firstElement instanceof String);
    Object secondElement = anArrayOI.getListElement(anArrayData, 1);
    assertEquals("Tennant", elementOI.getPrimitiveJavaObject(secondElement));
    assertTrue(secondElement instanceof String);
    Object thirdElement = anArrayOI.getListElement(anArrayData, 2);
    assertEquals("Smith", elementOI.getPrimitiveJavaObject(thirdElement));
    assertTrue(thirdElement instanceof String);
}
Also used : JavaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaStringObjectInspector) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)9 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)9 Test (org.junit.Test)8 StandardListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector)6 List (java.util.List)5 LinkedList (java.util.LinkedList)4 StructField (org.apache.hadoop.hive.serde2.objectinspector.StructField)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 TypeToken (com.google.common.reflect.TypeToken)3 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)3 Schema (org.apache.avro.Schema)2 GenericData (org.apache.avro.generic.GenericData)2 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)2 JavaStringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaStringObjectInspector)2 ArrayDeque (java.util.ArrayDeque)1 HashSet (java.util.HashSet)1 Queue (java.util.Queue)1 ColumnInfo (org.apache.hadoop.hive.ql.exec.ColumnInfo)1 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)1 RowResolver (org.apache.hadoop.hive.ql.parse.RowResolver)1