4

I'm using the mongo-spark connector to connect between spark and MongoDB. and I could not insert data to MongoDB because of spark default save mode is to error if the "table" (collection) exists and the I try this.

MongoSpark.write(centenarians).option("collection", "hundredClub")*.option("mode","append")*.save();

but this is not an effect. how can I solve this problem please help me thanks!

This is error message:

Exception in thread "main" java.lang.UnsupportedOperationException: MongoCollection already exists

2 Answers 2

4

I solved this problem by myself... I share this solution (I'm using spark language by Scala):

centenarians
  .write
  .format("com.mongodb.spark.sql.DefaultSource")
  .option("collection", "hundredClub")
  .mode("append")
  .save()
Sign up to request clarification or add additional context in comments.

Comments

1

I'm usign mongo-spark-connector_2.11. If you have your spark data in form of Dataset or dataframe , you can save it using different modes of save method :

For example , (Java Code)

MongoSpark.write(yourDataSet).mode("append").option("replaceDocument","false").save();

It will append new data in your existing document matching it with _id on mongo collection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.