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
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With