如何在 Zeppelin 中绘制带有数据间隙的图表?

How to plot a graph with data gaps in Zeppelin?

数据帧被提取到一个温度 table 以绘制每个时间单位(1 天)的数据密度:

val dailySummariesDf =
      getDFFromJdbcSource(SparkSession.builder().appName("test").master("local").getOrCreate(), s"SELECT *  FROM values WHERE time > '2020-06-06' and devicename='Voltage' limit 100000000")
        .persist(StorageLevel.MEMORY_ONLY_SER)
    .groupBy($"digital_twin_id", window($"time", "1 day")).count().as("count")    
    .withColumn("windowstart", col("window.start"))
    .withColumn("windowstartlong", unix_timestamp(col("window.start")))
    .orderBy("windowstart")    


 dailySummariesDf. 
 registerTempTable("bank")

然后我用%sql处理器

绘制它
%sql
select   windowstart, count
from bank

%sql
select   windowstartlong, count
from bank

我得到的结果如下图:

因此,我的预期是此图表中存在间隙,因为有些日子根本没有数据。但相反,我看到它被密集地绘制,十月的天数在八月之后绘制,没有显示九月的差距。

如何强制这些图表显示间隙并考虑真实的 X 轴值?

实际上,按 window 列对数据集进行分组不会为这些间隔内不包含任何原始行的间隔生成任何行。

我能想到的一种处理方法是添加一堆假行(在原始数据集中“手动填充空白”),然后才应用 groupBy/window。对于您的情况,这可以通过创建一个简单的单列数据集来完成,该数据集包含您感兴趣的范围内的所有日期,然后将其加入您的原始数据集。

这是我的快速尝试:

import spark.implicits._
import org.apache.spark.sql.types._

// Define sample data
val df = Seq(("a","2021-12-01"),
  ("b","2021-12-01"),
  ("c","2021-12-01"),
  ("a","2021-12-02"),
  ("b","2021-12-17")
).toDF("c","d").withColumn("d",to_timestamp($"d"))

// Define a dummy dataframe for the range 12/01/2021 - 12/30/2021
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
val start = DateTime.parse("2021-12-01",DateTimeFormat.forPattern("yyyy-MM-dd")).getMillis/1000
val end = start + 30*24*60*60
val temp = spark.range(start,end,24*60*60).toDF().withColumn("tc",to_timestamp($"id".cast(TimestampType))).drop($"id")

// Fill the gaps in original dataframe
val nogaps = temp.join(df, temp.col("tc") === df.col("d"), "left")

// Aggregate counts by a tumbling 1-day window
val result = nogaps.groupBy(window($"tc","1 day","1 day","5 hours")).agg(sum(when($"c".isNotNull,1).otherwise(0)).as("count"))
result.withColumn("windowstart",to_date(col("window.start"))).select("windowstart","count").orderBy("windowstart").show(false)
+-----------+-----+                                                             
|windowstart|count|
+-----------+-----+
|2021-12-01 |3    |
|2021-12-02 |1    |
|2021-12-03 |0    |
|2021-12-04 |0    |
|2021-12-05 |0    |
|2021-12-06 |0    |
|2021-12-07 |0    |
|2021-12-08 |0    |
|2021-12-09 |0    |
|2021-12-10 |0    |
|2021-12-11 |0    |
|2021-12-12 |0    |
|2021-12-13 |0    |
|2021-12-14 |0    |
|2021-12-15 |0    |
|2021-12-16 |0    |
|2021-12-17 |1    |
|2021-12-18 |0    |
|2021-12-19 |0    |
|2021-12-20 |0    |
+-----------+-----+

仅供参考:)