2017-05-26 82 views
0

我在spark-2.1.0-bin-hadoop2.7,Scala 2.11.8 & python3.5。pyspark mongoDB連接器問題

對於火花蒙戈連接器我正在佈置一個罐子中../spark-2.1.0-bin-後使用「蒙戈-火花connector_2.11-2.0.0.jar」從http://repo1.maven.org/maven2/org/mongodb/spark/mongo-spark-connector_2.11/2.0.0/

現在hadoop2.7 /罐路徑,我開始我的./pyspark如下官方蒙戈商務部網站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 

一切都很好,我得到我的sparksession對象火花。

現在,當我試圖收集加載到數據幀

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

我getitng此錯誤:

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 

我只是想不通:我在做什麼錯了,我很想念

待辦事宜我添加任何依賴的jar

-or這個連接到構造函數僅用於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() 

請幫我整理了這一點是相同的。提前致謝。

回答

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