当在源头更新基础数据时,结构化流中使用的 Spark DataFrame 会发生什么情况?

What happens to a Spark DataFrame used in Structured Streaming when its underlying data is updated at the source?

我有一个用例,我要加入带有静态 DataFrame 的流媒体 DataFrame。静态 DataFrame 是从镶木地板 table (包含镶木地板文件的目录)中读取的。 此镶木地板数据每天由另一个进程更新一次。

我的问题是我的静态 DataFrame 会怎样?

我没有任何代码可以分享,因为我还没有写任何代码,我只是在探索可能性。我正在使用 Spark 2.3.2

一大(一组)问题。

我自己(还)没有实施所有方面,但这是我的理解和一组来自同事的信息,他们执行了我认为令人信服且合乎逻辑的方面。我注意到关于此主题的信息不足。

因此,如果您有 JOIN(流式 --> 静态),则:

  • 如果按照 Databricks 应用标准编码实践并应用 .cache,SparkStructuredStreamingProgram 将只读取一次静态源,并且在后续处理周期中看不到任何变化,也没有程序失败。

  • 如果应用了 Databricks 的标准编码实践并且未使用缓存,则 SparkStructuredStreamingProgram 将在每个循环中读取静态源,并且所有更改都将在随后的处理周期中看到。

  • 但是,加入大型静态源不是一个好主意。如果大数据集明显,则使用 Hbase 或其他一些键值存储,如果易失性或非易失性则使用 mapPartitions。不过这个比较难。设计师告诉我,这是由我工作的一家航空公司完成的,数据工程师绝非易事。确实,没那么容易。

  • So, we can say that updates to static source will not cause any crash.
  • “...是否有可能以任何方式强制 DataFrame 每天更新一次...”我没有在文档或此处的 SO 上看到任何类似的方法。您可以使用 var 使静态源成为数据帧,并在驱动程序上使用计数器。由于每次都会评估和生成微批物理计划,因此我认为广播连接方面或优化没有问题。这是否是最优雅的,值得商榷 - 并且不是我的偏好。
  • If your data is small enough, the alternative is to read using a JOIN and thus perform the look up, via the use of the primary key augmented with some max value in a technical column that is added to the key to make the primary key a compound primary key - and that the data is updated in the background with a new set of data, thus not overwritten. Easiest in my view if you know the data is volatile and the data is small. Versioning means others may still read older data. That is why I state this, it may be a shared resource.

  • The final say for me is that I would NOT want to JOIN with the latest info if the static source is large - e.g. some Chinese companies have 100M customers! In this case I would use a KV store as LKP using mapPartitions as opposed to JOIN. See https://medium.com/@anchitsharma1994/hbase-lookup-in-spark-streaming-acafe28cb0dc that provides some insights. Also, this is old but still applicable source of information: https://blog.codecentric.de/en/2017/07/lookup-additional-data-in-spark-streaming/. Both are good reads. But requires some experience and to see the forest for the trees.