繁体   English   中英

如何在spark executor处收集所有记录并将其作为批处理进行处理

[英]How to collect all records at spark executor and process it as batch

在我的 spark kinesis 流应用程序中,我使用 foreachBatch 来获取流数据并需要将其发送到 drools 规则引擎以进行进一步处理。

我的要求是,我需要在列表/规则会话中累积所有 json 数据,并将其发送给规则引擎,以便在执行程序端作为批处理进行处理。

//Scala Code Example:
    val dataFrame = sparkSession.readStream
          .format("kinesis")
          .option("streamName", streamName)
          .option("region", region)
          .option("endpointUrl",endpointUrl)
          .option("initialPosition", "TRIM_HORIZON")
          .load()


    val query = dataFrame
        .selectExpr("CAST(data as STRING) as krecord")
        .writeStream
        .foreachBatch(function)
        .start()

    query.awaitTermination()


     val function = (batchDF: DataFrame, batchId: Long) => {
       val ruleSession = kBase.newKieSession() //Drools Rule Session, this is getting created at driver side

         batchDF.foreach(row => { // This piece of code is being run in executor.
               val jsonData: JSONData = jsonHandler.convertStringToJSONType(row.mkString)
               ruleSession.insert(jsonData) // Getting a null pointer exception here as the ruleSession is not available in executor.
             }
           )

       ruleHandler.processRule(ruleSession) // Again this is in the driver scope.
      }

在上面的代码中,我面临的问题是:foreachBatch 中使用的函数在驱动程序端执行,batchDF.foreach 中的代码在工作程序/执行程序端执行,因此无法获取规则会话。

有没有办法在每个执行器端运行整个函数?

或者

有没有更好的方法在转换后在批处理 DataFrame 中累积所有数据并将其从执行器/工作器内部发送到下一个进程?

我认为这可能有效......而不是运行 foreach,你可以使用 foreachBatch 或 foreachPartition(或者,如果你想要返回信息,或者像 mapPartition 这样的地图版本)。 在这部分中,打开与drools 系统的连接。 从那时起,遍历每个分区(或批处理)中的数据集,将每个分区发送到 drools 系统(或者您可以将整个块发送到 drools)。 在 foreachPartition / foreachBatch 部分的最后,关闭连接(如果适用)。

@codeaperature,这就是我实现批处理的方式,受您的回答启发,将其作为答案发布,因为这超出了评论中的字数限制。

  • 在数据帧上使用 foreach 并传入 ForeachWriter。
  • 在 ForeachWriter 的 open 方法中初始化规则会话。
  • 将每个输入 JSON 添加到 process 方法中的规则会话。
  • 使用加载了批量数据的规则会话在 close 方法中执行规则。

//斯卡拉代码:

val dataFrame = sparkSession.readStream
          .format("kinesis")
          .option("streamName", streamName)
          .option("region", region)
          .option("endpointUrl",endpointUrl)
          .option("initialPosition", "TRIM_HORIZON")
          .load()

val query = dataFrame
  .selectExpr("CAST(data as STRING) as krecord")
  .writeStream
  .foreach(dataConsumer)
  .start()

val dataConsumer = new ForeachWriter[Row] {

  var ruleSession: KieSession = null;

  def open(partitionId: Long, version: Long): Boolean = { // first open is called once for every batch
    ruleSession = kBase.newKieSession()
    true
  }

  def process(row: Row) = { // the process method will be called for a batch of records
    val jsonData: JSONData = jsonHandler.convertStringToJSONType(row.mkString)
    ruleSession.insert(jsonData) // Add all input json to rule session.
  }

  def close(errorOrNull: Throwable): Unit = { // after calling process for all records in bathc close is called
    val factCount = ruleSession.getFactCount
    if (factCount > 0) {
      ruleHandler.processRule(ruleSession) //batch processing of rule
    }
  }
}

暂无
暂无

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

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