Search in sources :

Example 26 with MapObjectInspector

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

the class TestOrcStruct method testInspectorFromTypeInfo.

@Test
public void testInspectorFromTypeInfo() throws Exception {
    TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString("struct<c1:boolean,c2:tinyint" + ",c3:smallint,c4:int,c5:bigint,c6:float,c7:double,c8:binary," + "c9:string,c10:struct<c1:int>,c11:map<int,int>,c12:uniontype<int>" + ",c13:array<timestamp>>");
    StructObjectInspector inspector = (StructObjectInspector) OrcStruct.createObjectInspector(typeInfo);
    assertEquals("struct<c1:boolean,c2:tinyint,c3:smallint,c4:int,c5:" + "bigint,c6:float,c7:double,c8:binary,c9:string,c10:struct<" + "c1:int>,c11:map<int,int>,c12:uniontype<int>,c13:array<timestamp>>", inspector.getTypeName());
    assertEquals(null, inspector.getAllStructFieldRefs().get(0).getFieldComment());
    assertEquals(null, inspector.getStructFieldRef("UNKNOWN"));
    OrcStruct s1 = new OrcStruct(13);
    for (int i = 0; i < 13; ++i) {
        s1.setFieldValue(i, i);
    }
    List<Object> list = new ArrayList<Object>();
    list.addAll(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
    assertEquals(list, inspector.getStructFieldsDataAsList(s1));
    ListObjectInspector listOI = (ListObjectInspector) inspector.getAllStructFieldRefs().get(12).getFieldObjectInspector();
    assertEquals(ObjectInspector.Category.LIST, listOI.getCategory());
    assertEquals(10, listOI.getListElement(list, 10));
    assertEquals(null, listOI.getListElement(list, -1));
    assertEquals(null, listOI.getListElement(list, 13));
    assertEquals(13, listOI.getListLength(list));
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(1, 2);
    map.put(2, 4);
    map.put(3, 6);
    MapObjectInspector mapOI = (MapObjectInspector) inspector.getAllStructFieldRefs().get(10).getFieldObjectInspector();
    assertEquals(3, mapOI.getMapSize(map));
    assertEquals(4, mapOI.getMapValueElement(map, 2));
}
Also used : HashMap(java.util.HashMap) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) ArrayList(java.util.ArrayList) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) Test(org.junit.Test)

Example 27 with MapObjectInspector

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

the class ExprNodeDescExprFactory method toExpr.

/**
 * {@inheritDoc}
 */
@Override
protected ExprNodeDesc toExpr(ColumnInfo colInfo, RowResolver rowResolver, int offset) throws SemanticException {
    ObjectInspector inspector = colInfo.getObjectInspector();
    if (inspector instanceof ConstantObjectInspector && inspector instanceof PrimitiveObjectInspector) {
        return toPrimitiveConstDesc(colInfo, inspector);
    }
    if (inspector instanceof ConstantObjectInspector && inspector instanceof ListObjectInspector) {
        ObjectInspector listElementOI = ((ListObjectInspector) inspector).getListElementObjectInspector();
        if (listElementOI instanceof PrimitiveObjectInspector) {
            return toListConstDesc(colInfo, inspector, listElementOI);
        }
    }
    if (inspector instanceof ConstantObjectInspector && inspector instanceof MapObjectInspector) {
        ObjectInspector keyOI = ((MapObjectInspector) inspector).getMapKeyObjectInspector();
        ObjectInspector valueOI = ((MapObjectInspector) inspector).getMapValueObjectInspector();
        if (keyOI instanceof PrimitiveObjectInspector && valueOI instanceof PrimitiveObjectInspector) {
            return toMapConstDesc(colInfo, inspector, keyOI, valueOI);
        }
    }
    if (inspector instanceof ConstantObjectInspector && inspector instanceof StructObjectInspector) {
        boolean allPrimitive = true;
        List<? extends StructField> fields = ((StructObjectInspector) inspector).getAllStructFieldRefs();
        for (StructField field : fields) {
            allPrimitive &= field.getFieldObjectInspector() instanceof PrimitiveObjectInspector;
        }
        if (allPrimitive) {
            return toStructConstDesc(colInfo, inspector, fields);
        }
    }
    // non-constant or non-primitive constants
    ExprNodeColumnDesc column = new ExprNodeColumnDesc(colInfo);
    column.setSkewedCol(colInfo.isSkewedCol());
    return column;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 28 with MapObjectInspector

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

the class HiveJsonWriter method visitMap.

/**
 * Visit a vertex in the graph that is a Java Map.
 *
 * @param oi The map's ObjectInspector
 * @param o The Map object
 * @return A JsonNode representation of the Map
 * @throws SerDeException The Map cannot be parsed
 */
private ObjectNode visitMap(final ObjectInspector oi, final Object o) throws SerDeException {
    final MapObjectInspector moi = (MapObjectInspector) oi;
    final ObjectInspector mapKeyInspector = moi.getMapKeyObjectInspector();
    final ObjectInspector mapValueInspector = moi.getMapValueObjectInspector();
    final Map<?, ?> map = moi.getMap(o);
    final ObjectNode objectNode = this.nodeFactory.objectNode();
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        final JsonNode keyNode = primitiveToNode(mapKeyInspector, entry.getKey());
        final JsonNode valueNode = walkObjectGraph(mapValueInspector, entry.getValue(), Collections.emptyList());
        objectNode.set(keyNode.asText(), valueNode);
    }
    return objectNode;
}
Also used : ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) HiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map)

Example 29 with MapObjectInspector

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

the class HiveJsonReader method visitMapNode.

/**
 * Visit a node if it is expected to be a Map (a.k.a. JSON Object)
 *
 * @param rootNode The node pointing at the JSON object
 * @param oi The ObjectInspector to parse the Map (must be a
 *          MapObjectInspector)
 * @return A Java Map containing the contents of the JSON map
 * @throws SerDeException The SerDe is not configured correctly
 */
private Map<Object, Object> visitMapNode(final JsonNode rootNode, final ObjectInspector oi) throws SerDeException {
    Preconditions.checkArgument(JsonNodeType.OBJECT == rootNode.getNodeType());
    final Map<Object, Object> ret = new LinkedHashMap<>();
    final ObjectInspector mapKeyInspector = ((MapObjectInspector) oi).getMapKeyObjectInspector();
    final ObjectInspector mapValueInspector = ((MapObjectInspector) oi).getMapValueObjectInspector();
    if (!(mapKeyInspector instanceof PrimitiveObjectInspector)) {
        throw new SerDeException("Map key must be a primitive type");
    }
    final Iterator<Entry<String, JsonNode>> it = rootNode.fields();
    while (it.hasNext()) {
        final Entry<String, JsonNode> field = it.next();
        final Object key = visitNode(new TextNode(field.getKey()), mapKeyInspector);
        final Object val = visitNode(field.getValue(), mapValueInspector);
        ret.put(key, val);
    }
    return ret;
}
Also used : ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 30 with MapObjectInspector

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

the class LazySimpleStructObjectInspector method getStructFieldData.

// With Data
@Override
public Object getStructFieldData(Object data, StructField fieldRef) {
    if (data == null) {
        return null;
    }
    StructObject struct = (StructObject) data;
    MyField f = (MyField) fieldRef;
    int fieldID = f.getFieldID();
    assert (fieldID >= 0 && fieldID < fields.size());
    ObjectInspector oi = f.getFieldObjectInspector();
    if (oi instanceof AvroLazyObjectInspector) {
        return ((AvroLazyObjectInspector) oi).getStructFieldData(data, fieldRef);
    }
    if (oi instanceof MapObjectInspector) {
        ObjectInspector valueOI = ((MapObjectInspector) oi).getMapValueObjectInspector();
        if (valueOI instanceof AvroLazyObjectInspector) {
            return ((AvroLazyObjectInspector) valueOI).getStructFieldData(data, fieldRef);
        }
    }
    return struct.getField(fieldID);
}
Also used : AvroLazyObjectInspector(org.apache.hadoop.hive.serde2.avro.AvroLazyObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) BaseStructObjectInspector(org.apache.hadoop.hive.serde2.BaseStructObjectInspector) AvroLazyObjectInspector(org.apache.hadoop.hive.serde2.avro.AvroLazyObjectInspector) StructObject(org.apache.hadoop.hive.serde2.StructObject) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)

Aggregations

MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)55 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)47 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)46 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)45 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)34 Map (java.util.Map)28 ArrayList (java.util.ArrayList)24 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)23 BinaryObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector)21 ByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector)21 DoubleObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector)21 FloatObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)21 IntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector)21 ShortObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector)21 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)21 BooleanObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector)20 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)20 DateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector)19 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)19 StructField (org.apache.hadoop.hive.serde2.objectinspector.StructField)17