在spark-mongodb库中更新+推送支持?

时间:2016-04-26 14:31:25

标签: mongodb hadoop apache-spark apache-spark-sql spark-dataframe

我想使用spark在mongodb中的现有文档的嵌入字段中插入一个新条目。

示例

Blog {  
    id:"001"  
    title:"This is a test blog",  
    content:"...."  
    comments:[{title:"comment1",content:".."},{title:"comment2",content:"..."}]      
}

我想做什么

db.blogs.update({id:"001"}, {$push:{comments:{title:"commentX",content:".."}}});

目前在这个库中是否可能?如果没有,请你指出正确的方向。

先谢谢

1 个答案:

答案 0 :(得分:0)

我能够使用Casbah Library for Spark-mongoDb进行操作。

import java.sql.Timestamp
import java.util.Date

import com.mongodb.casbah.MongoClient
import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.casbah.query.Imports._

object TestCasbah {

  def main(args: Array[String]) {
    val mongoClient = MongoClient("172.18.96.45", 27017)
    val db = mongoClient("agentCallRecord")
    val coll = db("CallDetails")
    val query = MongoDBObject("agentId" -> "agent_1")
    val callRatingMongoObject = MongoDBObject("audioId" -> 12351,"startTime" -> new Timestamp(new Date().getTime).toString, "endTime" -> new Timestamp(new Date().getTime).toString, "totalScore" -> 1, "sentiment" -> "NEGATIVE")
    val update = $push("callRating" -> callRatingMongoObject)
    coll.update(query, update)
  }
}