如何使用mongodb-spark连接器将数据插入mongodb中的现有集合

时间:2017-08-25 07:34:56

标签: mongodb apache-spark collections connector

我使用mongo-spark连接器连接spark和MongoDB。 并且我无法向MongoDB插入数据,因为如果"表"表示火花默认保存模式是错误的。 (集合)存在 而我试试这个。

extension UIAlertController {

    public convenience init?(test: String) {
        let title = NSLocalizedString("ALERT_CHOOSE_AVATAR_TITLE", comment: "")
        let message = NSLocalizedString("ALERT_CHOOSE_AVATAR_MESSAGE", comment: "")
        self.init(title: title, message: message, preferredStyle: .actionSheet)

        let selectPremadeAvatarAction = UIAlertAction(title: NSLocalizedString("SELECT_PREMADE_AVATAR", comment: ""), style: .default) { Void in

        }
        addAction(selectPremadeAvatarAction)

        let chooseFromGalleryAction = UIAlertAction(title: NSLocalizedString("CHOOSE_FROM_GALLERY", comment: ""), style: .default) { Void in

        }
        addAction(chooseFromGalleryAction)

        let takeNewPictureAction = UIAlertAction(title: NSLocalizedString("TAKE_NEW_PICTURE", comment: ""), style: .default) { Void in

        }
        addAction(takeNewPictureAction)

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { Void in
        }
        addAction(cancelAction)
    }

}

但这不是影响。 我怎么解决这个问题 请帮帮我谢谢!

这是错误消息:

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

2 个答案:

答案 0 :(得分:2)

我自己解决了这个问题...... 我分享了这个解决方案(我使用Scala的spark语言):

centenarians
  .write
  .format("com.mongodb.spark.sql.DefaultSource")
  .option("collection", "hundredClub")
  .mode("append")
  .save()

答案 1 :(得分:1)

我是monign-spark-connector_2.11。 如果您以数据集或数据框的形式保存了火花数据,则可以使用不同的保存方法模式进行保存:

例如,(Java代码)

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

它将在您现有的文档中附加新数据,并将其与mongo集合上的_id相匹配。

相关问题