Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 我们如何从结构化流媒体中获得小批量时间_Scala_Spark Streaming_Spark Structured Streaming - Fatal编程技术网

Scala 我们如何从结构化流媒体中获得小批量时间

Scala 我们如何从结构化流媒体中获得小批量时间,scala,spark-streaming,spark-structured-streaming,Scala,Spark Streaming,Spark Structured Streaming,在Spark streaming中,有一个带有时间参数的forEachRDD,可以利用该时间并将其用于不同的目的—元数据、在rdd中创建额外的时间列 val stream = KafkaUtils.createDirectStream(...) stream.foreachRDD { (rdd, time) => // update metadata with time // convert rdd to df and add time column // write df

在Spark streaming中,有一个带有时间参数的forEachRDD,可以利用该时间并将其用于不同的目的—元数据、在rdd中创建额外的时间列

val stream = KafkaUtils.createDirectStream(...)
stream.foreachRDD { (rdd, time) => 
  // update metadata with time 
  // convert rdd to df and add time column
  // write df
 }    
在结构化流媒体API中

val df: Dataset[Row] = spark
  .readStream
  .format("kafka")
  .load()

df.writeStream.trigger(...)
  .outputMode(...)
  .start()

如何能够为结构化流媒体获取类似的时间(小批量时间)数据,以便能够以相同的方式使用它

我搜索了一个函数,它提供了获取batchTime的可能性,但它似乎还不存在于Spark结构化流式API中

下面是一个解决方法,我使用
foreachBatch
获取批处理时间(假设批处理间隔为2000毫秒),它允许我们获取批处理ID:

val now = java.time.Instant.now
val batchInterval = 2000
df.writeStream.trigger(Trigger.ProcessingTime(batchInterval))
  .foreachBatch({ (batchDF: DataFrame, batchId: Long) =>
     println(now.plusMillis(batchId * batchInterval.milliseconds))
  })
  .outputMode(...)
  .start()
以下是输出:


2019-07-29T17:13:19.880Z
2019-07-29T17:13:21.880Z
2019-07-29T17:13:23.880Z
2019-07-29T17:13:25.880Z
2019-07-29T17:13:27.880Z
2019-07-29T17:13:29.880Z
2019-07-29T17:13:31.880Z
2019-07-29T17:13:33.880Z
2019-07-29T17:13:35.880Z


我希望有帮助

太好了,非常感谢!