How to fix ClassNotFoundException when connecting pyspark with Neo4j?

Viewed 755

I want to feed data using pyspark to neo4j. I have tried the following code:

from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local[1]") \
    .appName("SparkByExamples.com") \
    .getOrCreate()

df = spark.read.csv("countries.csv")

df.write \
    .format("org.neo4j.spark.DataSource") \
    .mode("ErrorIfExists") \
    .option("url", "bolt://localhost:7687") \
    .option("labels", ":Countries") \
    .save()

But it gives me an error like this:

2020-11-11 17:49:19 WARN  Utils:66 - Set SPARK_LOCAL_IP if you need to bind to another address
2020-11-11 17:49:20 WARN  NativeCodeLoader:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Traceback (most recent call last):
  File "xxx", line 13, in <module>
    .option("labels", ":Countries") \
  File "/opt/spark/spark-2.4.0-bin-hadoop2.7/python/pyspark/sql/readwriter.py", line 734, in save
    self._jwrite.save()
  File "/opt/spark/spark-2.4.0-bin-hadoop2.7/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
  File "/opt/spark/spark-2.4.0-bin-hadoop2.7/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/opt/spark/spark-2.4.0-bin-hadoop2.7/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o40.save.
: java.lang.ClassNotFoundException: Failed to find data source: org.neo4j.spark.DataSource. Please find packages at http://spark.apache.org/third-party-projects.html
    at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:657)
2 Answers

Problem is from your format. You need to include the package into Spark Application

Try

$SPARK_HOME/bin/spark-shell --packages neo4j-contrib:neo4j-spark-connector:2.4.5-M2
Related