Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Avro Schema object into StructType in spark

I have an RDD of type Row i.e, RDD[Row] and avro schema object .I need to create a dataframe with this info.

I need toconvert avro schema object into StructType for creating DataFrame.

Can you please help .

like image 675
Dushyant Singh Avatar asked Nov 24 '16 14:11

Dushyant Singh


People also ask

What is spark StructType?

StructType – Defines the structure of the Dataframe Spark provides spark. sql. types. StructType class to define the structure of the DataFrame and It is a collection or list on StructField objects. By calling Spark DataFrame printSchema() print the schema on console where StructType columns are represented as struct .


1 Answers

com.databricks.spark.avro has a class to help you with this

 StructType requiredType = (StructType) SchemaConverters.toSqlType(AvroClass.getClassSchema()).dataType();

Please go through this specific example : http://bytepadding.com/big-data/spark/read-write-parquet-files-using-spark/

like image 66
KrazyGautam Avatar answered Sep 24 '22 12:09

KrazyGautam