AvroUtils { public static Schema schemaFor(StructType structType) { return schemaFor(structType, null, null); }  static Schema schemaFor(StructType structType); static Schema schemaFor(StructType structType, String record, String namespace); static Schema typeFor(DataType dataType); static Schema typeFor(DataType dataType, boolean isOptional); static StructType structTypeFor(Schema schema); static DataType dataTypeFor(Schema schemaType);  }
@Test public void toSchemaNullable() throws Exception { StructType input = DataTypes.createStructType(Lists.newArrayList( DataTypes.createStructField("field1", DataTypes.BooleanType, false), DataTypes.createStructField("field2", DataTypes.StringType, true), DataTypes.createStructField("field3", DataTypes.DateType, false), DataTypes.createStructField("field4", DataTypes.TimestampType, false) )); Schema schema = AvroUtils.schemaFor(input); assertEquals("Invalid field count", 4, schema.getFields().size()); assertEquals("Invalid field name", "field1", schema.getFields().get(0).name()); assertEquals("Invalid field type", Schema.Type.BOOLEAN, schema.getFields().get(0).schema().getType()); assertEquals("Invalid field default", null, schema.getFields().get(0).defaultVal()); assertEquals("Invalid nullable (union) type", Schema.Type.UNION, schema.getFields().get(1).schema().getType()); assertEquals("Invalid nullable (union) type count", 2, schema.getFields().get(1).schema().getTypes().size()); assertEquals("Invalid field type", Schema.Type.STRING, schema.getFields().get(1).schema().getTypes().get(0).getType()); assertEquals("Invalid union default", null, schema.getFields().get(1).defaultVal()); }
@Test public void toSchemaArraysNested() throws Exception { StructType input = DataTypes.createStructType(Lists.newArrayList( DataTypes.createStructField("Outer", DataTypes.createArrayType( DataTypes.createArrayType(DataTypes.IntegerType, false), false), false) )); Schema schema = AvroUtils.schemaFor(input); assertEquals("Invalid field count", 1, schema.getFields().size()); assertEquals("Invalid field name", "Outer", schema.getFields().get(0).name()); assertEquals("Invalid field type", Schema.Type.ARRAY, schema.getFields().get(0).schema().getType()); assertEquals("Invalid outer element type, i.e the inner type", Schema.Type.ARRAY, schema.getFields().get(0).schema().getElementType().getType()); assertEquals("Invalid inner element type", Schema.Type.INT, schema.getFields().get(0).schema().getElementType().getElementType().getType()); }
@Test public void toSchemaMapsNested() throws Exception { StructType input = DataTypes.createStructType(Lists.newArrayList( DataTypes.createStructField("Outer", DataTypes.createMapType(DataTypes.StringType, DataTypes.createMapType(DataTypes.StringType, DataTypes.IntegerType, false), false), false) )); Schema schema = AvroUtils.schemaFor(input); assertEquals("Invalid field count", 1, schema.getFields().size()); assertEquals("Invalid field name", "Outer", schema.getFields().get(0).name()); assertEquals("Invalid field type", Schema.Type.MAP, schema.getFields().get(0).schema().getType()); assertEquals("Invalid outer value type, i.e the inner type", Schema.Type.MAP, schema.getFields().get(0).schema().getValueType().getType()); assertEquals("Invalid inner value type", Schema.Type.INT, schema.getFields().get(0).schema().getValueType().getValueType().getType()); }
@Test public void toSchemaStructTypesNested() throws Exception { StructType input = DataTypes.createStructType(Lists.newArrayList( DataTypes.createStructField("Outer", DataTypes.createStructType( Lists.newArrayList( DataTypes.createStructField("Inner", DataTypes.createStructType(Lists.newArrayList( DataTypes.createStructField("field1", DataTypes.IntegerType, false) )), false) )), false) ) ); Schema schema = AvroUtils.schemaFor(input); assertEquals("Invalid outer record name", "record0", schema.getName()); assertEquals("Invalid outer field count", 1, schema.getFields().size()); assertEquals("Invalid outer field name", "Outer", schema.getFields().get(0).name()); assertEquals("Invalid outer field type", Schema.Type.RECORD, schema.getFields().get(0).schema().getType()); assertEquals("Invalid inner record name", "record1", schema.getFields().get(0).schema().getName()); assertEquals("Invalid inner field count", 1, schema.getFields().get(0).schema().getFields().size()); assertEquals("Invalid inner field name", "Inner", schema.getFields().get(0).schema().getFields().get(0).name()); assertEquals("Invalid inner field type", Schema.Type.RECORD, schema.getFields().get(0).schema().getFields().get(0).schema().getType()); assertEquals("Invalid inner record name", "record2", schema.getFields().get(0).schema().getFields().get(0).schema().getName()); assertEquals("Invalid nested field count", 1, schema.getFields().get(0).schema().getFields().get(0).schema().getFields().size()); assertEquals("Invalid nested field name", "field1", schema.getFields().get(0).schema().getFields().get(0).schema().getFields().get(0).name()); assertEquals("Invalid nested field type", Schema.Type.INT, schema.getFields().get(0).schema().getFields().get(0).schema().getFields().get(0).schema().getType()); }