Spark reading Avro file

covfefe picture covfefe · Dec 11, 2018 · Viewed 9.8k times · Source

I'm using com.databricks.spark.avro. When I run it from spark-shell like so: spark-shell --jar spark-avro_2.11-4.0.0.jar, I am able to read the file by doing this:

import org.apache.spark.sql.SQLContext
val sqlContext = new SQLContext(sc)
val avroInput = sqlContext.read.format("com.databricks.spark.avro").load(inputPath)
avroInput.write.format("com.databricks.spark.avro").save(outputPath)

But if I try to do the same thing from my project using sbt clean run, I get:

java.lang.ClassNotFoundException: Failed to find data source: org.apache.spark.sql.avro.AvroFileFormat. Please find packages at http://spark.apache.org/third-party-projects.html
[info]   at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:657)
[info]   at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:194)
[info]   at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:178)
[info]   at com.databricks.spark.avro.package$AvroDataFrameReader$$anonfun$avro$2.apply(package.scala:34)

"com.databricks" %% "spark-avro" % "4.0.0" is listed in my Dependencies and it's in my external libraries. Is there another dependency I'm missing?

Answer

covfefe picture covfefe · Dec 21, 2018

Turns out I didn't have to use the databricks jar. I added apache spark avro to my dependencies:

"org.apache.spark"             %% "spark-avro"           % "2.4.0"

And I was able to read my avro file into a DataFrame:

val avroInput = sparkSession.read
  .format("avro")
  .load("/pathtoFile/avroFile.avro")