每小时每小时的广告点击次数

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

我是火花和学习的新手。我碰到一堵墙,我想找出每小时的点击数。给定此表:adclicks_schema

到目前为止,我已将时间戳转换为:

timestamp_only = adclicks.selectExpr(["to_timestamp(timestamp) as timestamp"])

click_count_by_hour = adclicks.select("timestamp")

click_count_by_hours.show(24)

而且我被困住了,接下来我该怎么办?我可以使用任何Spark SQL函数吗?

apache-spark pyspark apache-spark-sql pyspark-sql
1个回答
0
投票

您可以使用内置函数中的小时从时间戳中提取hour

  • 根据hourcount的记录数分组。

Example:

#sample data
df.show()
#+-------------------+
#|          timestamp|
#+-------------------+
#|2019-10-01 12:22:34|
#|2019-10-01 13:22:34|
#+-------------------+

from pyspark.sql.functions import *

df.withColumn("hour",hour(col("timestamp"))).\
groupBy("hour").\
agg(count("*").alias("count")).\
show()
#+----+-----+
#|hour|count|
#+----+-----+
#|  12|    1|
#|  13|    1|
#+----+-----+
© www.soinside.com 2019 - 2024. All rights reserved.