You need to enable JavaScript to run this app. 如何用Spark分析函数回溯前序失败记录统计尝试次数
优惠活动
大模型
产品
解决方案
定价
更多

如何用Spark分析函数回溯前序失败记录统计尝试次数

Spark 事件回溯统计实现方案

现有测试数据

arrayData = [
  ('abc','PN1','SN1','2021-02-03 10:20:11','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:20:15','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:20:19','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:21:11','2021-02-03 10:21:19','Success'),
  ('abc','PN1','SN1','2021-02-03 10:22:19','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:22:29','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:22:39','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:22:49','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:22:59','','Fail'),
  ('abc','PN1','SN1','2021-02-03 10:31:11','2021-02-03 10:31:19','Success'),
  ('abc','PN1','SN1','2021-02-03 10:31:21','2021-02-03 10:32:19','Success'),
  ('abc','PN1','SN1','2021-02-03 11:32:49','','Fail'),
  ('abc','PN1','SN1','2021-02-03 11:34:59','','Fail'),
  ('abc','PN1','SN2','2021-02-03 10:22:49','','Fail'),
  ('abc','PN1','SN2','2021-02-03 10:22:59','','Fail')
]

原始数据Schema:

root
 |-- event: string (nullable = true)
 |-- PN: string (nullable = true)
 |-- SN: string (nullable = true)
 |-- Claim_Start: string (nullable = true)
 |-- Claim_End: string (nullable = true)
 |-- Status: string (nullable = true)

原始数据预览:

+-----+---+---+-------------------+-------------------+-------+
|event| PN| SN|        Claim_Start|          Claim_End| Status|
+-----+---+---+-------------------+-------------------+-------+
|  abc|PN1|SN1|2021-02-03 10:20:11|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:20:15|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:20:19|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:21:11|2021-02-03 10:21:19|Success|
|  abc|PN1|SN1|2021-02-03 10:22:19|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:22:29|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:22:39|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:22:49|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:22:59|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 10:31:11|2021-02-03 10:31:19|Success|
|  abc|PN1|SN1|2021-02-03 10:31:21|2021-02-03 10:32:19|Success|
|  abc|PN1|SN1|2021-02-03 11:32:49|                   |   Fail|
|  abc|PN1|SN1|2021-02-03 11:34:59|                   |   Fail|
|  abc|PN1|SN2|2021-02-03 10:22:49|                   |   Fail|
|  abc|PN1|SN2|2021-02-03 10:22:59|                   |   Fail|
+-----+---+---+-------------------+-------------------+-------+

已完成的转换处理

from pyspark.sql import functions as f
df2 = df.withColumn("event_start_time",f.to_timestamp(df.Claim_Start,'yyyy-MM-dd HH:mm:ss'))\
        .withColumn("event_end_time",f.to_timestamp(df.Claim_End,'yyyy-MM-dd HH:mm:ss'))\
        .drop("Claim_Start","Claim_End")

转换后数据预览:

+-----+---+---+-------+-------------------+-------------------+
|event| PN| SN| Status|   event_start_time|     event_end_time|
+-----+---+---+-------+-------------------+-------------------+
|  abc|PN1|SN1|   Fail|2021-02-03 10:20:11|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 10:20:15|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 10:20:19|               null|
|  abc|PN1|SN1|Success|2021-02-03 10:21:11|2021-02-03 10:21:19|
|  abc|PN1|SN1|   Fail|2021-02-03 10:22:19|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 10:22:29|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 10:22:39|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 10:22:49|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 10:22:59|               null|
|  abc|PN1|SN1|Success|2021-02-03 10:31:11|2021-02-03 10:31:19|
|  abc|PN1|SN1|Success|2021-02-03 10:31:21|2021-02-03 10:32:19|
|  abc|PN1|SN1|   Fail|2021-02-03 11:32:49|               null|
|  abc|PN1|SN1|   Fail|2021-02-03 11:34:59|               null|
|  abc|PN1|SN2|   Fail|2021-02-03 10:22:49|               null|
|  abc|PN1|SN2|   Fail|2021-02-03 10:22:59|               null|
+-----+---+---+-------+-------------------+-------------------+

预期输出

+---+---+-----+-------+-------------------+-------------------+-------------------+------------+
| PN| SN|event| status|   event_start_time|     event_end_time|          first_try|num_attempts|
+---+---+-----+-------+-------------------+-------------------+-------------------+------------+
|PN1|SN1|  abc|   Fail|2021-02-03 11:32:49|                   |2021-02-03 11:32:49|           2|
|PN1|SN1|  abc|Success|2021-02-03 10:21:11|2021-02-03 10:21:19|2021-02-03 10:20:11|           4|
|PN1|SN1|  abc|Success|2021-02-03 10:31:11|2021-02-03 10:31:19|2021-02-03 10:22:29|           6|
|PN1|SN1|  abc|Success|2021-02-03 10:31:21|2021-02-03 10:32:19|               null|           1|
|PN1|SN2|  abc|   Fail|2021-02-03 10:22:49|                   |2021-02-03 10:22:49|           2|
+---+---+-----+-------+-------------------+-------------------+-------------------+------------+

输出逻辑说明:按event、PN、SN分组并按event_start_time升序排序后,以SN1分组的记录为例,第4条为Success事件,本次成功是第4次尝试,首次尝试时间为2021-02-03 10:20:11;若连续出现Success记录,则后一条Success无前序失败记录,first_try为null,尝试次数为1;若分组末尾为连续Fail记录,则作为未结束组统计,event_end_time保持为空,统计对应首次尝试时间和尝试次数。

实现方案

可以通过Spark窗口函数实现,核心思路是给每个事件组分配组ID,把上一个Success之后到当前Success/分组末尾的所有记录划到同一个组,再按组聚合统计。

实现代码

from pyspark.sql import Window
# 定义分区排序窗口
win = Window.partitionBy("event","PN","SN").orderBy("event_start_time")
# 步骤1:给Success打标记,计算组ID:组ID为当前记录之前的Success数量
df3 = df2.withColumn("success_flag", f.when(f.col("Status") == "Success", 1).otherwise(0))\
         .withColumn("group_id", f.sum("success_flag").over(win.rowsBetween(Window.unboundedPreceding, -1)))
# 步骤2:按分组聚合计算指标
df_res = df3.groupBy("event","PN","SN","group_id").agg(
    f.max(f.when(f.col("Status") == "Success", f.col("event_start_time"))).alias("event_start_time"),
    f.max(f.when(f.col("Status") == "Success", f.col("event_end_time"))).alias("event_end_time"),
    f.min("event_start_time").alias("first_try"),
    f.count("*").alias("num_attempts"),
    f.max("Status").alias("status")
).select("PN","SN","event","status","event_start_time","event_end_time",
        # 连续Success的情况:组内第一条就是Success,first_try设为null
        f.when(f.col("status") == "Success", 
               f.when(f.first("Status").over(Window.partitionBy("event","PN","SN","group_id").orderBy("event_start_time")) == "Success", None)
               .otherwise(f.col("first_try"))
              ).otherwise(f.col("first_try")).alias("first_try"),
        "num_attempts"
).orderBy("PN","SN","event_start_time")
# 输出结果
df_res.show(truncate=False)

代码说明

  • 分组逻辑:通过group_id将每一段「上一个Success之后的所有记录+当前Success」划分为同一组,末尾未结束的Fail记录也会被划到单独的组
  • 特殊场景处理:如果组内第一条记录就是Success,说明是连续Success场景,直接将first_try设为null,尝试次数为1
  • 末尾Fail组处理:组内没有Success记录,所以event_end_time保持为null,直接统计组内记录数和首次尝试时间即可

内容的提问来源于stack exchange,提问作者Kumar P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 00:45:05