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));
}
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;
}
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;
}
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;
}
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);
}
Aggregations