簡體   English   中英

由Apache Spark和MongoDB Hadoop-Connector創建的BSON結構

[英]BSON structure created by Apache Spark and MongoDB Hadoop-Connector

我正在嘗試使用MongoDB Hadoop-Connector將一些JSON從Spark(Scala)保存到MongoDB。 我遇到的問題是,此API似乎始終將您的數據保存為“{_id:...,value:{your JSON document}}”。

在下面的代碼示例中,我的文檔保存如下:

{
    "_id" : ObjectId("55e80cfea9fbee30aa703261"),
    "value" : {
        "_id" : "55e6c65da9fbee285f2f9175",
        "year" : 2014,
        "month" : 5,
        "day" : 6,
        "hour" : 18,
        "user_id" : 246
    }
}

有沒有辦法說服MongoDB Hadoop Connector在您指定的結構中編寫JSON / BSON,而不是將它嵌套在這些_id / value字段下?

這是我的Scala Spark代碼:

  val jsonstr = List("""{
        "_id" : "55e6c65da9fbee285f2f9175",
        "year" : 2014,
        "month" : 5,
        "day" : 6,
        "hour" : 18,
        "user_id" : 246}""")

  val conf = new SparkConf().setAppName("Mongo Dummy").setMaster("local[*]")
  val sc = new SparkContext(conf)

  // DB params
  val host = "127.0.0.1"
  val port = "27017"
  val database = "dummy"
  val collection = "fubar"

  // input is collection we want to read (not doing so here)
  val mongo_input = s"mongodb://$host/$database.$collection"
  // output is collection we want to write
  val mongo_output = s"mongodb://$host/$database.$collection"

  // Set up extra config for Hadoop connector
  val hadoopConfig = new Configuration()

  //hadoopConfig.set("mongo.input.uri", mongo_input)
  hadoopConfig.set("mongo.output.uri", mongo_output)

  // convert JSON to RDD
  val rdd = sc.parallelize(jsonstr)

  // write JSON data to DB
  val saveRDD = rdd.map { json =>
    (null, Document.parse(json))
  }

  saveRDD.saveAsNewAPIHadoopFile("file:///bogus",
    classOf[Object],
    classOf[BSONObject],
    classOf[MongoOutputFormat[Object, BSONObject]],
    hadoopConfig)
  // Finished  
  sc.stop

這是我的SBT:

name := "my-mongo-test"

version := "1.0"

scalaVersion := "2.10.4"

// Spark needs to appear in SBT BEFORE Mongodb connector!
libraryDependencies += "org.apache.spark" %% "spark-core" % "1.4.0"

// MongoDB-Hadoop connector
libraryDependencies += "org.mongodb.mongo-hadoop" % "mongo-hadoop-core" % "1.4.0"

老實說,我對從Spark中保存JSON - > BSON - > MongoDB似乎有多困難感到困惑。 因此,歡迎任何有關如何更靈活地保存我的JSON數據的建議。

好吧,我剛剛找到了解決方案。 事實證明,MongoRecordWriter其用於通過MongoOutputFormat插入在脫離BSONWritableMongoOutputBSONObject字段繼承的任何值。

因此,最簡單的解決方案是創建包含BSONObject作為值的RDD,而不是Document

我在Java中嘗試了這個解決方案,但我確信它也適用於Scala。 這是一個示例代碼:

JavaPairRDD<Object, BSONObject> bsons = values.mapToPair(lineValues -> {
    BSONObject doc = new BasicBSONObject();
    doc.put("field1", lineValues.get(0));
    doc.put("field2", lineValues.get(1));
    return new Tuple2<Object, BSONObject>(UUID.randomUUID().toString(), doc);
});

Configuration outputConfig = new Configuration();
outputConfig.set("mongo.output.uri",
                 "mongodb://localhost:27017/my_db.lines");
bsons.saveAsNewAPIHadoopFile("file:///this-is-completely-unused"
        , Object.class
        , BSONObject.class
        , MongoOutputFormat.class
        , outputConfig);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM