use of com.linkedin.haivvreo.AvroObjectInspectorGenerator in project haivvreo by jghoman.
the class TestAvroObjectInspectorGenerator method canHandleUnions.
@Test
public void canHandleUnions() throws SerDeException {
Schema s = Schema.parse(UNION_SCHEMA);
AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
// Column names
assertEquals(1, aoig.getColumnNames().size());
assertEquals("aUnion", aoig.getColumnNames().get(0));
// Column types
assertEquals(1, aoig.getColumnTypes().size());
TypeInfo typeInfo = aoig.getColumnTypes().get(0);
assertTrue(typeInfo instanceof UnionTypeInfo);
UnionTypeInfo uti = (UnionTypeInfo) typeInfo;
// Check that the union has come out unscathed. No scathing of unions allowed.
List<TypeInfo> typeInfos = uti.getAllUnionObjectTypeInfos();
assertEquals(2, typeInfos.size());
assertEquals(INT, typeInfos.get(0));
assertEquals(STRING, typeInfos.get(1));
assertEquals("uniontype<int,string>", uti.getTypeName());
}
use of com.linkedin.haivvreo.AvroObjectInspectorGenerator in project haivvreo by jghoman.
the class TestAvroObjectInspectorGenerator method canHandleRecords.
@Test
public void canHandleRecords() throws SerDeException {
Schema s = Schema.parse(RECORD_SCHEMA);
AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
// Column names
assertEquals(1, aoig.getColumnNames().size());
assertEquals("aRecord", aoig.getColumnNames().get(0));
// Column types
assertEquals(1, aoig.getColumnTypes().size());
TypeInfo typeInfo = aoig.getColumnTypes().get(0);
assertEquals(ObjectInspector.Category.STRUCT, typeInfo.getCategory());
assertTrue(typeInfo instanceof StructTypeInfo);
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
// Check individual elements of subrecord
ArrayList<String> allStructFieldNames = structTypeInfo.getAllStructFieldNames();
ArrayList<TypeInfo> allStructFieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
assertEquals(allStructFieldNames.size(), 3);
String[] names = new String[] { "int1", "boolean1", "long1" };
String[] typeInfoStrings = new String[] { "int", "boolean", "bigint" };
for (int i = 0; i < allStructFieldNames.size(); i++) {
assertEquals("Fieldname " + allStructFieldNames.get(i) + " doesn't match expected " + names[i], names[i], allStructFieldNames.get(i));
assertEquals("Typeinfo " + allStructFieldTypeInfos.get(i) + " doesn't match expected " + typeInfoStrings[i], typeInfoStrings[i], allStructFieldTypeInfos.get(i).getTypeName());
}
}
use of com.linkedin.haivvreo.AvroObjectInspectorGenerator in project haivvreo by jghoman.
the class TestAvroObjectInspectorGenerator method canHandleEnums.
// Enums are one of two Avro types that Hive doesn't have any native support for.
@Test
public void canHandleEnums() throws SerDeException {
Schema s = Schema.parse(ENUM_SCHEMA);
AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
// Column names - we lose the enumness of this schema
assertEquals(1, aoig.getColumnNames().size());
assertEquals("baddies", aoig.getColumnNames().get(0));
// Column types
assertEquals(1, aoig.getColumnTypes().size());
assertEquals(STRING, aoig.getColumnTypes().get(0));
}
use of com.linkedin.haivvreo.AvroObjectInspectorGenerator in project haivvreo by jghoman.
the class TestAvroObjectInspectorGenerator method primitiveTypesWorkCorrectly.
@Test
public void primitiveTypesWorkCorrectly() throws SerDeException {
final String bunchOfPrimitives = "{\n" + " \"namespace\": \"testing\",\n" + " \"name\": \"PrimitiveTypes\",\n" + " \"type\": \"record\",\n" + " \"fields\": [\n" + " {\n" + " \"name\":\"aString\",\n" + " \"type\":\"string\"\n" + " },\n" + " {\n" + " \"name\":\"anInt\",\n" + " \"type\":\"int\"\n" + " },\n" + " {\n" + " \"name\":\"aBoolean\",\n" + " \"type\":\"boolean\"\n" + " },\n" + " {\n" + " \"name\":\"aLong\",\n" + " \"type\":\"long\"\n" + " },\n" + " {\n" + " \"name\":\"aFloat\",\n" + " \"type\":\"float\"\n" + " },\n" + " {\n" + " \"name\":\"aDouble\",\n" + " \"type\":\"double\"\n" + " },\n" + " {\n" + " \"name\":\"aNull\",\n" + " \"type\":\"null\"\n" + " }\n" + " ]\n" + "}";
AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(Schema.parse(bunchOfPrimitives));
String[] expectedColumnNames = { "aString", "anInt", "aBoolean", "aLong", "aFloat", "aDouble", "aNull" };
verifyColumnNames(expectedColumnNames, aoig.getColumnNames());
TypeInfo[] expectedColumnTypes = { STRING, INT, BOOLEAN, LONG, FLOAT, DOUBLE, VOID };
verifyColumnTypes(expectedColumnTypes, aoig.getColumnTypes());
// Rip apart the object inspector, making sure we got what we expect.
final ObjectInspector oi = aoig.getObjectInspector();
assertTrue(oi instanceof StandardStructObjectInspector);
final StandardStructObjectInspector ssoi = (StandardStructObjectInspector) oi;
List<? extends StructField> structFields = ssoi.getAllStructFieldRefs();
assertEquals(expectedColumnNames.length, structFields.size());
for (int i = 0; i < expectedColumnNames.length; i++) {
assertEquals("Column names don't match", expectedColumnNames[i].toLowerCase(), structFields.get(i).getFieldName());
assertEquals("Column types don't match", expectedColumnTypes[i].getTypeName(), structFields.get(i).getFieldObjectInspector().getTypeName());
}
}
use of com.linkedin.haivvreo.AvroObjectInspectorGenerator in project haivvreo by jghoman.
the class TestAvroObjectInspectorGenerator method failOnNonRecords.
// that we can only process records
@Test
public void failOnNonRecords() throws Exception {
String nonRecordSchema = "{ \"type\": \"enum\",\n" + " \"name\": \"Suit\",\n" + " \"symbols\" : [\"SPADES\", \"HEARTS\", \"DIAMONDS\", \"CLUBS\"]\n" + "}";
Schema s = Schema.parse(nonRecordSchema);
try {
new AvroObjectInspectorGenerator(s);
fail("Should not be able to handle non-record Avro types");
} catch (SerDeException sde) {
assertTrue(sde.getMessage().startsWith("Schema for table must be of type RECORD"));
}
}
Aggregations