Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

How to convert Avro Schema Object to StructType? #261

Open
kant111 opened this issue Dec 16, 2017 · 2 comments
Open

How to convert Avro Schema Object to StructType? #261

kant111 opened this issue Dec 16, 2017 · 2 comments

Comments

@kant111
Copy link

kant111 commented Dec 16, 2017

How to convert Avro Schema Object to StructType?

@gengliangwang
Copy link
Contributor

Refer to:

SchemaConverters.toSqlType(avroSchema).dataType match {

@kant111
Copy link
Author

kant111 commented Dec 18, 2017

@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;
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants