可以使用Pyspark的filter函数和between函数来解决此问题。将列中的时间戳转换为日期和时间格式,并使用between函数筛选两个日期时间戳之间的记录。
以下是一个示例代码:
from pyspark.sql.functions import col, to_timestamp
df = spark.createDataFrame([
("2021-01-01 12:00:00", 1),
("2021-01-01 12:15:00", 2),
("2021-01-01 12:30:00", 3),
("2021-01-02 12:00:00", 4),
("2021-01-02 12:15:00", 5),
("2021-01-02 12:30:00", 6)],
["timestamp", "value"])
df = df.withColumn("timestamp", to_timestamp(col("timestamp"), "yyyy-MM-dd HH:mm:ss"))
start_time = "2021-01-01 12:15:00"
end_time = "2021-01-02 12:15:00"
filtered_df = df.filter((col("timestamp").between(start_time, end_time)))
display(filtered_df)
在此示例中,我们使用to_timestamp函数将字符串时间戳转换为Pyspark的时间戳格式。然后,我们使用filter和between函数来筛选在2021年1月1日12:15:00和2021年1月2日12:15:00之间的记录。