Apache spark 在PySpark结构化流媒体中,对多个输出流使用单个流数据帧

Apache spark 在PySpark结构化流媒体中,对多个输出流使用单个流数据帧,apache-spark,pyspark,spark-streaming,spark-structured-streaming,Apache Spark,Pyspark,Spark Streaming,Spark Structured Streaming,有一个连续的数据流,在所有转换之后,它有下一个模式: root |-- message_id: string (nullable = true) |-- device_id: string (nullable = true) |-- metric_id: long (nullable = true) |-- value: long (nullable = true) |-- timestamp: string (nullable = true) 还有一套规则,即: if metric

有一个连续的数据流,在所有转换之后,它有下一个模式:

root
 |-- message_id: string (nullable = true)
 |-- device_id: string (nullable = true)
 |-- metric_id: long (nullable = true)
 |-- value: long (nullable = true)
 |-- timestamp: string (nullable = true)
还有一套规则,即:

if metric_id = 4077 and value > 10 and value < 25
如果度量_id=4077且值>10且值<25
这意味着,如果流中的任何行满足该条件,则必须将该行推送到另一个流中


如何识别满足警报条件(有多条)的消息,并在将它们推送到不同的流中?

Spark结构化流应用程序允许您使用相同的输入流拥有多个输出流

这意味着,如果例如
df
是您的输入流数据帧,您只需定义一个数据帧过滤器,并将得到的过滤数据帧用于另一个输出流,如下所示:

df=readStream.format(…).options(…).load().select(…)
#创建仅包含更改的新数据帧
alertsDf=df.filter((df.metric_id==“4077”)&(df.value>10)和(df.value<45))
#对输出流使用两个数据帧
df.writeStream.format(…).options(…).start()
alertsDf.writeStream.format(…).options(…).start()
spark.streams.determination()

对于容错,建议分别为每个输出流设置选项
checkpointLocation

按照文档,最好的方法是使用ForEachBatch,这正是所描述的用例

因此,重复使用上述示例,您将执行以下操作:

df = readStream.format(...).options(...).load().select(...)

# use both DataFrames for output streams in ForEachBatch
# And Applying transformations inside the ForEachBatch scope, but this can 
  happen outside

df.writeStream.foreachBatch((batch:df) =>
     df.write.format(...).options(location1),
     df.filter( (df.metric_id == "4077") & (df.value > 10)  & (df.value < 45) 
     ).write.format(...).options(location2)

)
.start()
.awaitTermination()
df=readStream.format(…).options(…).load().select(…)
#对ForEachBatch中的输出流使用两个数据帧
#并在ForEachBatch范围内应用转换,但这可以
发生在外面
df.writeStream.foreachBatch((批次:df)=>
df.write.format(…).options(位置1),
df.filter((df.metric_id==“4077”)&(df.value>10)和(df.value<45)
).write.format(…).options(位置2)
)
.start()
.终止

我希望我帮了忙,谢谢

最好的?不是真的。有可能。注意容错。部分写入会发生什么情况?