计算和按日期汇总数据/时间

问题描述 投票:0回答:1

我有这样一个数据帧的工作:

Id     | TimeStamp         | Event     |  DeviceId
1      | 5.2.2019 8:00:00  | connect   |  1
2      | 5.2.2019 8:00:05  | disconnect|  1

我使用databricks和pyspark做ETL过程。我该如何计算和建立这样一个数据帧的显示在底部?使用UDF我已经尝试过,但我无法找到一种方法,使其工作。我也试图通过遍历整个数据帧做,但这是极其缓慢。

我想这汇总数据帧得到一个新的数据帧,它告诉我的时候,每个设备有多久连接和断开:

Id     | StartDateTime   | EndDateTime   | EventDuration  |State    |  DeviceId
1      | 5.2.19 8:00:00  | 5.2.19 8:00:05| 0.00:00:05     |connected|  1
pyspark etl data-warehouse databricks
1个回答
1
投票

我想,你可以用一个window功能,并与withColumn一些进一步的柱创作这部作品。

我做的代码应该为设备的映射,并创建一个表,每个状态的持续时间。唯一的要求是,连接和断开交替出现。

然后你可以使用下面的代码:

from pyspark.sql.types import *
from pyspark.sql.functions import *
from pyspark.sql.window import Window
import datetime
test_df = sqlContext.createDataFrame([(1,datetime.datetime(2019,2,5,8),"connect",1),
(2,datetime.datetime(2019,2,5,8,0,5),"disconnect",1),
(3,datetime.datetime(2019,2,5,8,10),"connect",1),
(4,datetime.datetime(2019,2,5,8,20),"disconnect",1),], 
["Id","TimeStamp","Event","DeviceId"])    
#creation of dataframe with 4 events for 1 device
test_df.show()

输出:

+---+-------------------+----------+--------+
| Id|          TimeStamp|     Event|DeviceId|
+---+-------------------+----------+--------+
|  1|2019-02-05 08:00:00|   connect|       1|
|  2|2019-02-05 08:00:05|disconnect|       1|
|  3|2019-02-05 08:10:00|   connect|       1|
|  4|2019-02-05 08:20:00|disconnect|       1|
+---+-------------------+----------+--------+

然后,您可以创建辅助功能和窗口:

my_window = Window.partitionBy("DeviceId").orderBy(col("TimeStamp").desc()) #create window
get_prev_time = lag(col("Timestamp"),1).over(my_window)                     #get previous timestamp
time_diff = get_prev_time.cast("long") - col("TimeStamp").cast("long")      #compute duration

test_df.withColumn("EventDuration",time_diff)\
.withColumn("EndDateTime",get_prev_time)\           #apply the helper functions
.withColumnRenamed("TimeStamp","StartDateTime")\    #rename according to your schema
.withColumn("State",when(col("Event")=="connect", "connected").otherwise("disconnected"))\ #create the state column 
.filter(col("EventDuration").isNotNull()).select("Id","StartDateTime","EndDateTime","EventDuration","State","DeviceId").show()
#finally some filtering for the last events, which do not have a previous time

输出:

+---+-------------------+-------------------+-------------+------------+--------+
| Id|      StartDateTime|        EndDateTime|EventDuration|       State|DeviceId|
+---+-------------------+-------------------+-------------+------------+--------+
|  3|2019-02-05 08:10:00|2019-02-05 08:20:00|          600|   connected|       1|
|  2|2019-02-05 08:00:05|2019-02-05 08:10:00|          595|disconnected|       1|
|  1|2019-02-05 08:00:00|2019-02-05 08:00:05|            5|   connected|       1|
+---+-------------------+-------------------+-------------+------------+--------+
© www.soinside.com 2019 - 2024. All rights reserved.