Pyspark Dataframe writing to AVRO error


#1

Hi Friends,

I am trying to write my dataframe to AVRO format but am getting an error.

I tried these 2 versions of packages:

pyspark --master yarn --conf spark.ui.port=44335 --packages com.databricks:spark-avro_2.10:2.0.1

and

pyspark --master yarn --conf spark.ui.port=44335 --packages com.databricks:spark-avro_2.11:4.0.0

The error I get is this:

orders_DF.write.format(“avro”).mode(“Overwrite”)
… .option(“header”, “true”).save("/user/macksv17/practice/files/orders_avro")
Traceback (most recent call last):
File “”, line 2, in
File “/usr/hdp/current/spark-client/python/pyspark/sql/readwriter.py”, line 397, in save
self._jwrite.save(path)
File “/usr/hdp/current/spark-client/python/lib/py4j-0.9-src.zip/py4j/java_gateway.py”, line 813, in call
File “/usr/hdp/current/spark-client/python/pyspark/sql/utils.py”, line 45, in deco
return f(*a, **kw)
File “/usr/hdp/current/spark-client/python/lib/py4j-0.9-src.zip/py4j/protocol.py”, line 308, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o54.save.
: java.lang.ClassNotFoundException: Failed to find data source: avro. Please use Spark package http://spark-packages.org/package/databricks/spark-avro
at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.lookupDataSource(ResolvedDataSource.scala:72)
at org.apache.spark.sql.execution.datasources.ResolvedDataSource$.apply(ResolvedDataSource.scala:219)
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:148)
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:139)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:381)
at py4j.Gateway.invoke(Gateway.java:259)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:209)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: avro.DefaultSource

Please help.

Thanks.

Michael


Learn Spark 1.6.x or Spark 2.x on our state of the art big data labs

  • Click here for access to state of the art 13 node Hadoop and Spark Cluster


#2

After launching pyspark with below command

pyspark --packages com.databricks:spark-avro_2.10:2.0.1

you can save avro file format below way,

dataframe.write.format("com.databricks.spark.avro").save("output path dir")
or
dataframe.save("output path dir", "com.databricks.spark.avro”)


#3

Thanks Sunil. It works now.


#4