pyspark mongoDB连接器问题

时间:2017-05-26 10:32:47

标签: python mongodb apache-spark

我在Spark-2.1.0-bin-hadoop2.7,Scala 2.11.8& python3.5。

对于spark-mongo连接器,我正在使用http://repo1.maven.org/maven2/org/mongodb/spark/mongo-spark-connector_2.11/2.0.0/中的“mongo-spark-connector_2.11-2.0.0.jar”

现在将该jar放入../spark-2.1.0-bin-hadoop2.7/jars路径后,我正在启动我的./pyspark,如下官方mongo doc网站https://docs.mongodb.com/spark-connector/master/python-api/#tutorials

./bin/pyspark --conf "spark.mongodb.input.uri=mongodb://127.0.0.1/test.myCollection?readPreference=primaryPreferred" 
          --conf "spark.mongodb.output.uri=mongodb://127.0.0.1/test.myCollection" \
          --packages org.mongodb.spark:mongo-spark-connector_2.11:2.0.0

一切都很好,我把我的火花对象当作火花。

现在,当我尝试将集合加载到数据框

df = spark.read.format("com.mongodb.spark.sql.DefaultSource").option("uri","mongodb://127.0.0.1/test.myCollection").load()

我得到这个错误:

py4j.protocol.Py4JJavaError: An error occurred while calling o36.load.
: java.lang.NoClassDefFoundError: com/mongodb/ConnectionString
    at com.mongodb.spark.config.MongoCompanionConfig$$anonfun$4.apply(MongoCompanionConfig.scala:278)
    at com.mongodb.spark.config.MongoCompanionConfig$$anonfun$4.apply(MongoCompanionConfig.scala:278)
    at scala.util.Try$.apply(Try.scala:192)
    at com.mongodb.spark.config.MongoCompanionConfig$class.connectionString(MongoCompanionConfig.scala:278)
    at com.mongodb.spark.config.ReadConfig$.connectionString(ReadConfig.scala:39)
    at com.mongodb.spark.config.ReadConfig$.apply(ReadConfig.scala:51)
    at com.mongodb.spark.config.ReadConfig$.apply(ReadConfig.scala:39)
    at com.mongodb.spark.config.MongoCompanionConfig$class.apply(MongoCompanionConfig.scala:124)
    at com.mongodb.spark.config.ReadConfig$.apply(ReadConfig.scala:39)
    at com.mongodb.spark.config.MongoCompanionConfig$class.apply(MongoCompanionConfig.scala:113)
    at com.mongodb.spark.config.ReadConfig$.apply(ReadConfig.scala:39)
    at com.mongodb.spark.sql.DefaultSource.createRelation(DefaultSource.scala:67)
    at com.mongodb.spark.sql.DefaultSource.createRelation(DefaultSource.scala:50)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:330)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:152)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:125)
    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:745)
Caused by: java.lang.ClassNotFoundException: com.mongodb.ConnectionString
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 27 more

我无法弄清楚:我做错了什么,我错过了什么

-do我必须添加任何依赖jar

- 或者此连接器仅适用于2.0.x而不适用于spark2.1.0(从未在spark2.0.x上试过)

  • 如果mongoDB上有授权,那么我的uri将会是什么样子

- 它显示了connectionString,但我已经重新检查了100次(在pymongo连接url中也是如此)。

- 也尝试使用会话构建器对象但没有结果

from pyspark.sql import SparkSession

my_spark = SparkSession \
.builder \
.appName("myApp") \
.config("spark.mongodb.input.uri", "mongodb://127.0.0.1/test.coll") \
.config("spark.mongodb.output.uri", "mongodb://127.0.0.1/test.coll") \
.getOrCreate()

请帮我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

它适用于Spark 2.1.x,您需要在类路径中包含MongoDB Java驱动程序以及连接器。编辑spark-defaults.conf,这样就包含了必要的jar文件:

spark.driver.extraClassPath /opt/mongo-spark-connector_2.11-2.0.0.jar,/opt/mongo-java-driver-3.2.2.jar
spark.executor.extraClassPath /opt/mongo-spark-connector_2.11-2.0.0.jar,/opt/mongo-java-driver-3.2.2.jar
spark.driver.extraLibraryPath /opt/mongo-spark-connector_2.11-2.0.0.jar,/opt/mongo-java-driver-3.2.2.jar
spark.executor.extraLibraryPath /opt/mongo-spark-connector_2.11-2.0.0.jar,/opt/mongo-java-driver-3.2.2.jar

答案 1 :(得分:1)

我遇到了同样的问题。

似乎mongo-spark-connector仅适用于Python 2.x.一旦我从3.5改为2.7,mongodb数据就没有问题了。