use of org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector in project hive by apache.
the class TestAvroDeserializer method canDeserializeVoidType.
@Test
public void canDeserializeVoidType() throws IOException, SerDeException {
String schemaString = "{\n" + " \"type\": \"record\", \n" + " \"name\": \"nullTest\",\n" + " \"fields\" : [\n" + " {\"name\": \"isANull\", \"type\": \"null\"}\n" + " ]\n" + "}";
Schema s = AvroSerdeUtils.getSchemaFor(schemaString);
GenericData.Record record = new GenericData.Record(s);
record.put("isANull", null);
assertTrue(GENERIC_DATA.validate(s, 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 theVoidObject = row.get(0);
assertNull(theVoidObject);
StandardStructObjectInspector oi = (StandardStructObjectInspector) aoig.getObjectInspector();
StructField fieldRef = oi.getStructFieldRef("isANull");
Object shouldBeNull = oi.getStructFieldData(row, fieldRef);
assertNull(shouldBeNull);
assertTrue(fieldRef.getFieldObjectInspector() instanceof VoidObjectInspector);
}
use of org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector in project hive by apache.
the class TestAvroDeserializer method canDeserializeRecordsInternal.
public void canDeserializeRecordsInternal(Schema s, Schema fileSchema) throws SerDeException, IOException {
GenericData.Record record = new GenericData.Record(s);
GenericData.Record innerRecord = new GenericData.Record(s.getField("aRecord").schema());
innerRecord.put("int1", 42);
innerRecord.put("boolean1", true);
innerRecord.put("long1", 42432234234l);
record.put("aRecord", innerRecord);
assertTrue(GENERIC_DATA.validate(s, record));
AvroGenericRecordWritable garw = Utils.serializeAndDeserializeRecord(record, fileSchema);
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 theRecordObject = row.get(0);
System.out.println("theRecordObject = " + theRecordObject.getClass().getCanonicalName());
// The original record was lost in the deserialization, so just go the
// correct way, through objectinspectors
StandardStructObjectInspector oi = (StandardStructObjectInspector) aoig.getObjectInspector();
List<? extends StructField> allStructFieldRefs = oi.getAllStructFieldRefs();
assertEquals(1, allStructFieldRefs.size());
StructField fieldRefForaRecord = allStructFieldRefs.get(0);
assertEquals("arecord", fieldRefForaRecord.getFieldName());
Object innerRecord2 = oi.getStructFieldData(row, fieldRefForaRecord);
// Extract innerRecord field refs
StandardStructObjectInspector innerRecord2OI = (StandardStructObjectInspector) fieldRefForaRecord.getFieldObjectInspector();
List<? extends StructField> allStructFieldRefs1 = innerRecord2OI.getAllStructFieldRefs();
assertEquals(3, allStructFieldRefs1.size());
assertEquals("int1", allStructFieldRefs1.get(0).getFieldName());
assertEquals("boolean1", allStructFieldRefs1.get(1).getFieldName());
assertEquals("long1", allStructFieldRefs1.get(2).getFieldName());
innerRecord2OI.getStructFieldsDataAsList(innerRecord2);
assertEquals(42, innerRecord2OI.getStructFieldData(innerRecord2, allStructFieldRefs1.get(0)));
assertEquals(true, innerRecord2OI.getStructFieldData(innerRecord2, allStructFieldRefs1.get(1)));
assertEquals(42432234234l, innerRecord2OI.getStructFieldData(innerRecord2, allStructFieldRefs1.get(2)));
}
use of org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector in project hive by apache.
the class TestAvroDeserializer method canDeserializeMapsWithPrimitiveKeys.
@Test
public void canDeserializeMapsWithPrimitiveKeys() throws SerDeException, IOException {
Schema s = AvroSerdeUtils.getSchemaFor(TestAvroObjectInspectorGenerator.MAP_WITH_PRIMITIVE_VALUE_TYPE);
GenericData.Record record = new GenericData.Record(s);
Map<String, Long> m = new Hashtable<String, Long>();
m.put("one", 1l);
m.put("two", 2l);
m.put("three", 3l);
record.put("aMap", m);
assertTrue(GENERIC_DATA.validate(s, record));
System.out.println("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 theMapObject = row.get(0);
assertTrue(theMapObject instanceof Map);
Map theMap = (Map) theMapObject;
// Verify the raw object that's been created
assertEquals(1l, theMap.get("one"));
assertEquals(2l, theMap.get("two"));
assertEquals(3l, theMap.get("three"));
// Verify that the provided object inspector can pull out these same values
StandardStructObjectInspector oi = (StandardStructObjectInspector) aoig.getObjectInspector();
List<Object> z = oi.getStructFieldsDataAsList(row);
assertEquals(1, z.size());
StructField fieldRef = oi.getStructFieldRef("amap");
Map theMap2 = (Map) oi.getStructFieldData(row, fieldRef);
assertEquals(1l, theMap2.get("one"));
assertEquals(2l, theMap2.get("two"));
assertEquals(3l, theMap2.get("three"));
}
use of org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector in project hive by apache.
the class TestAvroDeserializer method verifyNullableType.
private void verifyNullableType(GenericData.Record record, Schema s, String fieldName, String expected) throws SerDeException, IOException {
assertTrue(GENERIC_DATA.validate(s, 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 rowElement = row.get(0);
StandardStructObjectInspector oi = (StandardStructObjectInspector) aoig.getObjectInspector();
List<Object> fieldsDataAsList = oi.getStructFieldsDataAsList(row);
assertEquals(1, fieldsDataAsList.size());
StructField fieldRef = oi.getStructFieldRef(fieldName);
ObjectInspector fieldObjectInspector = fieldRef.getFieldObjectInspector();
StringObjectInspector soi = (StringObjectInspector) fieldObjectInspector;
if (expected == null) {
assertNull(soi.getPrimitiveJavaObject(rowElement));
} else {
assertEquals(expected, soi.getPrimitiveJavaObject(rowElement));
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector in project hive by apache.
the class TestAvroDeserializer method canDeserializeMapWithNullablePrimitiveValues.
@Test
public void canDeserializeMapWithNullablePrimitiveValues() throws SerDeException, IOException {
Schema s = AvroSerdeUtils.getSchemaFor(TestAvroObjectInspectorGenerator.MAP_WITH_NULLABLE_PRIMITIVE_VALUE_TYPE_SCHEMA);
GenericData.Record record = new GenericData.Record(s);
Map<String, Long> m = new HashMap<String, Long>();
m.put("one", 1l);
m.put("two", 2l);
m.put("three", 3l);
m.put("mu", null);
record.put("aMap", m);
assertTrue(GENERIC_DATA.validate(s, record));
System.out.println("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 theMapObject = row.get(0);
assertTrue(theMapObject instanceof Map);
Map theMap = (Map) theMapObject;
// Verify the raw object that's been created
assertEquals(1l, theMap.get("one"));
assertEquals(2l, theMap.get("two"));
assertEquals(3l, theMap.get("three"));
assertTrue(theMap.containsKey("mu"));
assertEquals(null, theMap.get("mu"));
// Verify that the provided object inspector can pull out these same values
StandardStructObjectInspector oi = (StandardStructObjectInspector) aoig.getObjectInspector();
List<Object> z = oi.getStructFieldsDataAsList(row);
assertEquals(1, z.size());
StructField fieldRef = oi.getStructFieldRef("amap");
Map theMap2 = (Map) oi.getStructFieldData(row, fieldRef);
assertEquals(1l, theMap2.get("one"));
assertEquals(2l, theMap2.get("two"));
assertEquals(3l, theMap2.get("three"));
assertTrue(theMap2.containsKey("mu"));
assertEquals(null, theMap2.get("mu"));
}
Aggregations