use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfo in project hive by apache.
the class TypeInfoToSchema method createAvroArray.
private Schema createAvroArray(TypeInfo typeInfo) {
ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
Schema listSchema = createAvroSchema(listTypeInfo.getListElementTypeInfo());
return Schema.createArray(listSchema);
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfo in project hive by apache.
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(AvroSerdeUtils.getSchemaFor(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 org.apache.hadoop.hive.serde2.typeinfo.TypeInfo in project hive by apache.
the class TestAvroObjectInspectorGenerator method canHandleUnions.
@Test
public void canHandleUnions() throws SerDeException {
Schema s = AvroSerdeUtils.getSchemaFor(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 org.apache.hadoop.hive.serde2.typeinfo.TypeInfo in project hive by apache.
the class TestAvroSerializer method canSerializeStructs.
@Test
public void canSerializeStructs() throws SerDeException {
String field = "{ \"name\":\"struct1\", \"type\":{\"type\":\"record\", " + "\"name\":\"struct1_name\", \"fields\": [\n" + "{ \"name\":\"sInt\", \"type\":\"int\" }, { \"name\"" + ":\"sBoolean\", \"type\":\"boolean\" }, { \"name\":\"sString\", \"type\":\"string\" } ] } }";
Schema s = buildSchema(field);
GenericData.Record innerRecord = new GenericData.Record(s.getField("struct1").schema());
innerRecord.put("sInt", 77);
innerRecord.put("sBoolean", false);
innerRecord.put("sString", "tedious");
GenericData.Record r = new GenericData.Record(s);
r.put("struct1", innerRecord);
AvroSerializer as = new AvroSerializer();
AvroDeserializer ad = new AvroDeserializer();
AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
ObjectInspector oi = aoig.getObjectInspector();
List<String> columnNames = aoig.getColumnNames();
List<TypeInfo> columnTypes = aoig.getColumnTypes();
AvroGenericRecordWritable agrw = new AvroGenericRecordWritable(r);
agrw.setFileSchema(r.getSchema());
Object obj = ad.deserialize(columnNames, columnTypes, agrw, s);
Writable result = as.serialize(obj, oi, columnNames, columnTypes, s);
assertTrue(result instanceof AvroGenericRecordWritable);
GenericRecord r2 = ((AvroGenericRecordWritable) result).getRecord();
assertEquals(s, r2.getSchema());
GenericRecord r3 = (GenericRecord) r2.get("struct1");
assertEquals(77, r3.get("sInt"));
assertEquals(false, r3.get("sBoolean"));
assertEquals("tedious", r3.get("sString"));
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfo in project hive by apache.
the class TestTypeInfoToSchema method createAvroNestedStructSchema.
@Test
public void createAvroNestedStructSchema() throws IOException {
StructTypeInfo structTypeInfo = new StructTypeInfo();
ArrayList<String> names = new ArrayList<String>();
names.add("field1");
names.add("field2");
structTypeInfo.setAllStructFieldNames(names);
ArrayList<TypeInfo> typeInfos = new ArrayList<TypeInfo>();
typeInfos.add(STRING);
typeInfos.add(INT);
structTypeInfo.setAllStructFieldTypeInfos(typeInfos);
StructTypeInfo superStructTypeInfo = new StructTypeInfo();
ArrayList<String> superNames = new ArrayList<String>();
superNames.add("superfield1");
superNames.add("superfield2");
superStructTypeInfo.setAllStructFieldNames(superNames);
ArrayList<TypeInfo> superTypeInfos = new ArrayList<TypeInfo>();
superTypeInfos.add(STRING);
superTypeInfos.add(structTypeInfo);
superStructTypeInfo.setAllStructFieldTypeInfos(superTypeInfos);
final String specificSchema = IOUtils.toString(Resources.getResource("avro-nested-struct.avsc").openStream()).replace(lineSeparator, "");
String expectedSchema = genSchema(specificSchema);
Assert.assertEquals("Test for nested struct's avro schema failed", expectedSchema, getAvroSchemaString(superStructTypeInfo));
}
Aggregations