【问题标题】:How to plot a graph with data gaps in Zeppelin?如何在 Zeppelin 中绘制带有数据间隙的图表?
【发布时间】:2022-01-18 15:17:08
【问题描述】:

Dataframe 被提取到一个临时表中以绘制每个时间单位(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

我得到的如下所示:

所以,我的期望是在这张图表中出现空白,因为有些日子根本没有数据。但相反,我看到它被密集地绘制,10 月的天数绘制在 8 月之后,没有显示 9 月的差距。

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

【问题讨论】:

    标签: scala apache-spark apache-spark-sql apache-zeppelin


    【解决方案1】:

    确实,按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    |
    +-----------+-----+
    

    仅用于说明目的:)

    【讨论】:

    • 这种方法有效,但是.. 它失败了 1) 输出的退出限制,默认情况下每个项目 1000。 2) 每个输出的整体内存限制。所以目前没有标准情节的选项只是考虑时间垃圾类型轴上的时间戳?在这里找到外部绘图仪的唯一方法是什么?
    • 是的,我不知道你是否可以让图形处理器插入缺失点,对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多