use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project hive by apache.
the class AvroSerializer method serializeStruct.
private Object serializeStruct(StructTypeInfo typeInfo, StructObjectInspector ssoi, Object o, Schema schema) throws AvroSerdeException {
int size = schema.getFields().size();
List<? extends StructField> allStructFieldRefs = ssoi.getAllStructFieldRefs();
List<Object> structFieldsDataAsList = ssoi.getStructFieldsDataAsList(o);
GenericData.Record record = new GenericData.Record(schema);
ArrayList<TypeInfo> allStructFieldTypeInfos = typeInfo.getAllStructFieldTypeInfos();
for (int i = 0; i < size; i++) {
Field field = schema.getFields().get(i);
TypeInfo colTypeInfo = allStructFieldTypeInfos.get(i);
StructField structFieldRef = allStructFieldRefs.get(i);
Object structFieldData = structFieldsDataAsList.get(i);
ObjectInspector fieldOI = structFieldRef.getFieldObjectInspector();
Object val = serialize(colTypeInfo, fieldOI, structFieldData, field.schema());
record.put(field.name(), val);
}
return record;
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project SQLWindowing by hbutani.
the class TranslateUtils method getTypeMap.
public static ArrayList<? extends Object>[] getTypeMap(StructObjectInspector oi) {
StructTypeInfo t = (StructTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(oi);
ArrayList<String> fnames = t.getAllStructFieldNames();
ArrayList<TypeInfo> fields = t.getAllStructFieldTypeInfos();
return new ArrayList<?>[] { fnames, fields };
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project phoenix by apache.
the class PhoenixObjectInspectorFactory method createStructObjectInspector.
public static LazySimpleStructObjectInspector createStructObjectInspector(TypeInfo type, LazySerDeParameters serdeParams) {
StructTypeInfo structTypeInfo = (StructTypeInfo) type;
List<String> fieldNames = structTypeInfo.getAllStructFieldNames();
List<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
List<ObjectInspector> fieldObjectInspectors = new ArrayList<ObjectInspector>(fieldTypeInfos.size());
for (int i = 0; i < fieldTypeInfos.size(); i++) {
fieldObjectInspectors.add(createObjectInspector(fieldTypeInfos.get(i), serdeParams));
}
return LazyObjectInspectorFactory.getLazySimpleStructObjectInspector(fieldNames, fieldObjectInspectors, null, serdeParams.getSeparators()[1], serdeParams, ObjectInspectorOptions.JAVA);
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project cdap by caskdata.
the class ObjectSerializer method fromLazyObject.
private Object fromLazyObject(TypeInfo type, Object data) {
if (data == null) {
return null;
}
switch(type.getCategory()) {
case PRIMITIVE:
Writable writable = ((LazyPrimitive) data).getWritableObject();
return fromWritable(writable);
case LIST:
ListTypeInfo listType = (ListTypeInfo) type;
TypeInfo listElementType = listType.getListElementTypeInfo();
List<Object> list = ((LazyArray) data).getList();
if (list.isEmpty()) {
return ImmutableList.of();
}
Object[] arrayContent = new Object[list.size()];
for (int i = 0; i < arrayContent.length; i++) {
arrayContent[i] = fromLazyObject(listElementType, list.get(i));
}
return arrayContent;
case MAP:
MapTypeInfo mapType = (MapTypeInfo) type;
Map<Object, Object> mapContent = Maps.newConcurrentMap();
Map<Object, Object> map = ((LazyMap) data).getMap();
for (Map.Entry<Object, Object> entry : map.entrySet()) {
mapContent.put(fromLazyObject(mapType.getMapKeyTypeInfo(), entry.getKey()), fromLazyObject(mapType.getMapValueTypeInfo(), entry.getValue()));
}
return mapContent;
case STRUCT:
StructTypeInfo structType = (StructTypeInfo) type;
List<TypeInfo> info = structType.getAllStructFieldTypeInfos();
List<String> names = structType.getAllStructFieldNames();
Map<String, Object> structMap = Maps.newConcurrentMap();
List<Object> struct = ((LazyStruct) data).getFieldsAsList();
for (int structIndex = 0; structIndex < info.size(); structIndex++) {
structMap.put(names.get(structIndex), fromLazyObject(info.get(structIndex), struct.get(structIndex)));
}
return structMap;
case UNION:
throw new UnsupportedOperationException("union not yet supported");
default:
return data.toString();
}
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project cdap by caskdata.
the class ObjectSerializer method serialize.
public Writable serialize(Object o, ObjectInspector objectInspector) {
//overwrite field names (as they get lost by Hive)
StructTypeInfo structTypeInfo = (StructTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(objectInspector);
structTypeInfo.setAllStructFieldNames(columnNames);
List<TypeInfo> info = structTypeInfo.getAllStructFieldTypeInfos();
List<String> names = structTypeInfo.getAllStructFieldNames();
Map<String, Object> recordMap = new HashMap<>();
List<Object> recordObjects = ((StructObjectInspector) objectInspector).getStructFieldsDataAsList(o);
for (int structIndex = 0; structIndex < info.size(); structIndex++) {
Object obj = recordObjects.get(structIndex);
TypeInfo objType = info.get(structIndex);
if (obj instanceof LazyNonPrimitive || obj instanceof LazyPrimitive) {
// In case the SerDe that deserialized the object is the one of a native table
recordMap.put(names.get(structIndex), fromLazyObject(objType, obj));
} else if (obj instanceof Writable) {
// Native tables sometimes introduce primitive Writable objects at this point
recordMap.put(names.get(structIndex), fromWritable((Writable) obj));
} else {
// In case the deserializer is the DatasetSerDe
recordMap.put(names.get(structIndex), serialize(obj, objType));
}
}
// TODO Improve serialization logic - CDAP-11
return new Text(GSON.toJson(recordMap));
}
Aggregations