【解决方案】pyspark 初次连接mongo 时报错Class not found exception:com.mongodb.spark.sql.DefaultSource

部分报错如下:

Traceback (most recent call last):
      File "/home/cisco/spark-mongo-test.py", line 7, in 
        df = spark.read.format("com.mongodb.spark.sql.DefaultSource").load()
      File "/home/cisco/spark-2.4.1-bin-hadoop2.7/python/lib/pyspark.zip/pyspark/sql/readwriter.py", line 165, in load
      File "/home/cisco/spark-2.4.1-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
      File "/home/cisco/spark-2.4.1-bin-hadoop2.7/python/lib/pyspark.zip/pyspark/sql/utils.py", line 63, in deco
      File "/home/cisco/spark-2.4.1-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value
    py4j.protocol.Py4JJavaError: An error occurred while calling o39.load.
    : java.lang.ClassNotFoundException: Failed to find data source: com.mongodb.spark.sql.DefaultSource. Please find packages at http://spark.apache.org/third-party-projects.html
            at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:549)
            at org.apache.spark.sql.execution.datasources.DataSource.providingClass$lzycompute(DataSource.scala:86)
            at org.apache.spark.sql.execution.datasources.DataSource.providingClass(DataSource.scala:86)
            at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:301)
            at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:178)
            at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:146)
            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:244)
            at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
            at py4j.Gateway.invoke(Gateway.java:280)
            at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
            at py4j.commands.CallCommand.execute(CallCommand.java:79)
            at py4j.GatewayConnection.run(GatewayConnection.java:214)
            at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.ClassNotFoundException: com.mongodb.spark.sql.DefaultSource.DefaultSource
            at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
            at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21$$anonfun$apply$12.apply(DataSource.scala:533)
            at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21$$anonfun$apply$12.apply(DataSource.scala:533)
            at scala.util.Try$.apply(Try.scala:192)
            at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21.apply(DataSource.scala:533)
            at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21.apply(DataSource.scala:533)
            at scala.util.Try.orElse(Try.scala:84)
            at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:53

1 、由于spark 中缺少如下相关jar包:

mongo-java-driver-3.10.2.jar          mongo-spark-connector_2.11-2.4.1.jar    

将如下jar包添加到$SPARK_HOME/jars/  

再次执行既能正常运行。

2 、在国外论坛上看到类似问题说是在$SPARK_HOME/conf/spark-defaults.conf添加如下配置:

spark.jars.packages     org.mongodb.spark:mongo-spark-connector_2.11-2.4.1

我只操作第一步就能正常运行了,第二步供参考。

你可能感兴趣的:(Spark,spark)