spark-avro icon indicating copy to clipboard operation
spark-avro copied to clipboard

How to convert Avro Schema Object to StructType?

Open kant111 opened this issue 7 years ago • 2 comments

How to convert Avro Schema Object to StructType?

kant111 avatar Dec 16 '17 11:12 kant111

Refer to: https://github.com/databricks/spark-avro/blob/b04cfe807752cde08d171cf6adf774f3795ab64d/src/main/scala/com/databricks/spark/avro/DefaultSource.scala#L94

gengliangwang avatar Dec 17 '17 08:12 gengliangwang

@gengliangwang Thanks! I code in Java so I had to do something like this. Please let me know if there is any easier way.

    import com.databricks.spark.avro.SchemaConverters;

   // @param schema: Avro Schema
    public static StructType convertSchemaToStructType(Schema schema) {
        StructType structType = new StructType();
        for (Schema.Field field : schema.getFields()) {
            structType.add(field.name(), SchemaConverters.toSqlType(field.schema()).dataType());
        }
        return structType;
    }

kant111 avatar Dec 18 '17 20:12 kant111