简体   繁体   English

无法使用 Spark 结构化流向 MongoDB 发送数据

[英]unable to send data to MongoDB using spark strucutred streaming

I followed This Unable to send data to MongoDB using Kafka-Spark Structured Streaming to send data from spark structured streaming to mongoDB and I implemented it successfully but there is one issue.我按照此无法使用 Kafka-Spark Structured Streaming将数据从 spark 结构化流发送到 mongoDB 并成功实现它,但存在一个问题。 Like when function就像当 function

override def process(record: Row): Unit = {

    val doc: Document = Document(record.prettyJson.trim)
    // lazy opening of MongoDB connection


    ensureMongoDBConnection()
    val result = collection.insertOne(doc)
    if (messageCountAccum != null)
      messageCountAccum.add(1)
  }

code is executing without any problem but no data is being send to MongoDB代码执行没有任何问题,但没有数据发送到 MongoDB

But if i add a print statement like this但是如果我添加这样的打印语句

override def process(record: Row): Unit = {
    val doc: Document = Document(record.prettyJson.trim)

    // lazy opening of MongoDB connection


    ensureMongoDBConnection()
    val result = collection.insertOne(doc)
    result.foreach(println) //print statement
    if (messageCountAccum != null)
      messageCountAccum.add(1)
  }

Data is getting inserted in MongoDB数据正在插入 MongoDB

I don't know why????不知道为什么????

foreach initializes the writer sink. foreach初始化写入器接收器。 Without the foreach your dataframe is never calculated.如果没有 foreach,则永远不会计算您的 dataframe。

Try this :

val df = // your df here
df.map(r => process(r))
df.count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM