- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
是否可以为 rangeBetween 或 rowsBetween 创建一个可以在 orderBy 中具有多个条件的窗口函数。假设我有一个如下所示的数据框。
user_id timestamp date event
0040b5f0 2018-01-22 13:04:32 2018-01-22 1
0040b5f0 2018-01-22 13:04:35 2018-01-22 0
0040b5f0 2018-01-25 18:55:08 2018-01-25 1
0040b5f0 2018-01-25 18:56:17 2018-01-25 1
0040b5f0 2018-01-25 20:51:43 2018-01-25 1
0040b5f0 2018-01-31 07:48:43 2018-01-31 1
0040b5f0 2018-01-31 07:48:48 2018-01-31 0
0040b5f0 2018-02-02 09:40:58 2018-02-02 1
0040b5f0 2018-02-02 09:41:01 2018-02-02 0
0040b5f0 2018-02-05 14:03:27 2018-02-05 1
对于每一行,我需要日期不超过 3 天的事件 列值的总和。但是我不能对同一天晚些时候发生的事件求和。我可以创建一个窗口函数,例如:
days = lambda i: i * 86400
my_window = Window\
.partitionBy(["user_id"])\
.orderBy(F.col("date").cast("timestamp").cast("long"))\
.rangeBetween(-days(3), 0)
但这将包括同一天晚些时候发生的事件。我需要创建一个窗口函数,它的行为类似于(对于带有 * 的行):
user_id timestamp date event
0040b5f0 2018-01-22 13:04:32 2018-01-22 1----|==============|
0040b5f0 2018-01-22 13:04:35 2018-01-22 0 sum here all events
0040b5f0 2018-01-25 18:55:08 2018-01-25 1 only within 3 days
* 0040b5f0 2018-01-25 18:56:17 2018-01-25 1----| |
0040b5f0 2018-01-25 20:51:43 2018-01-25 1===================|
0040b5f0 2018-01-31 07:48:43 2018-01-31 1
0040b5f0 2018-01-31 07:48:48 2018-01-31 0
0040b5f0 2018-02-02 09:40:58 2018-02-02 1
0040b5f0 2018-02-02 09:41:01 2018-02-02 0
0040b5f0 2018-02-05 14:03:27 2018-02-05 1
我尝试创建类似这样的东西:
days = lambda i: i * 86400
my_window = Window\
.partitionBy(["user_id"])\
.orderBy(F.col("date").cast("timestamp").cast("long"))\
.rangeBetween(-days(3), Window.currentRow)\
.orderBy(F.col("t_stamp"))\
.rowsBetween(Window.unboundedPreceding, Window.currentRow)
但它只反射(reflect)了最后一个orderBy。
结果表应该是这样的:
user_id timestamp date event event_last_3d
0040b5f0 2018-01-22 13:04:32 2018-01-22 1 1
0040b5f0 2018-01-22 13:04:35 2018-01-22 0 1
0040b5f0 2018-01-25 18:55:08 2018-01-25 1 2
0040b5f0 2018-01-25 18:56:17 2018-01-25 1 3
0040b5f0 2018-01-25 20:51:43 2018-01-25 1 4
0040b5f0 2018-01-31 07:48:43 2018-01-31 1 1
0040b5f0 2018-01-31 07:48:48 2018-01-31 0 1
0040b5f0 2018-02-02 09:40:58 2018-02-02 1 2
0040b5f0 2018-02-02 09:41:01 2018-02-02 0 2
0040b5f0 2018-02-05 14:03:27 2018-02-05 1 2
我一直坚持这个问题有一段时间了,如果有任何关于如何处理它的建议,我将不胜感激。
最佳答案
我已经用 scala 编写了满足您要求的等价物。我认为转换为 python 应该不难:
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions._
val DAY_SECS = 24*60*60 //Seconds in a day
//Given a timestamp in seconds, returns the seconds equivalent of 00:00:00 of that date
val trimToDateBoundary = (d: Long) => (d / 86400) * 86400
//Using 4 for range here - since your requirement is to cover 3 days prev, which date wise inclusive is 4 days
//So e.g. given any TS of 25 Jan, the range will cover (25 Jan 00:00:00 - 4 times day_secs = 22 Jan 00:00:00) to current TS
val wSpec = Window.partitionBy("user_id").
orderBy(col("timestamp").cast("long")).
rangeBetween(trimToDateBoundary(Window.currentRow)-(4*DAY_SECS), Window.currentRow)
df.withColumn("sum", sum('event) over wSpec).show()
以下是应用于您的数据时的输出:
+--------+--------------------+--------------------+-----+---+
| user_id| timestamp| date|event|sum|
+--------+--------------------+--------------------+-----+---+
|0040b5f0|2018-01-22 13:04:...|2018-01-22 00:00:...| 1.0|1.0|
|0040b5f0|2018-01-22 13:04:...|2018-01-22 00:00:...| 0.0|1.0|
|0040b5f0|2018-01-25 18:55:...|2018-01-25 00:00:...| 1.0|2.0|
|0040b5f0|2018-01-25 18:56:...|2018-01-25 00:00:...| 1.0|3.0|
|0040b5f0|2018-01-25 20:51:...|2018-01-25 00:00:...| 1.0|4.0|
|0040b5f0|2018-01-31 07:48:...|2018-01-31 00:00:...| 1.0|1.0|
|0040b5f0|2018-01-31 07:48:...|2018-01-31 00:00:...| 0.0|1.0|
|0040b5f0|2018-02-02 09:40:...|2018-02-02 00:00:...| 1.0|2.0|
|0040b5f0|2018-02-02 09:41:...|2018-02-02 00:00:...| 0.0|2.0|
|0040b5f0|2018-02-05 14:03:...|2018-02-05 00:00:...| 1.0|2.0|
+--------+--------------------+--------------------+-----+---+
我没有使用“日期”列。不确定我们如何在考虑的情况下实现您的要求。因此,如果 TS 的日期可能与日期列不同,则此解决方案不涵盖它。
注意:接受Column
参数的rangeBetween
已在Spark 2.3.0 中引入接受日期/时间戳类型的列。所以,这个解决方案可能更优雅。
关于python - PySpark 窗口函数 : multiple conditions in orderBy on rangeBetween/rowsBetween,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48688780/
现在我已经创建了一个额外的跨度来容纳一个条件。 568 || subKey == 0" ng-repeat="links in linksWrap.links">
一些 excel IF 语句可能会变得相当长,我正在寻找一种更简单的方法来编写它们。例如,如果我要写: If($B$4+13=7,$B$4+13,FALSE) 我认为它会更容易说: If($B$4+1
我有一个包含 FromDate 、 ToDate 、 VendorName 和 GoodsName 的表单,一旦一切为真,我需要显示结果 示例: FromDate="11/20/2019"、ToDat
我经常看到使用 !!condition 而不仅仅是常规条件的代码。即: if(!!value){ doSomething(); } 对比: if(value){ doSomething
这个问题有点模棱两可,这两个在汇编代码/性能方面是否等效: public void example{ do{ //some statements; if(condition)
在我看到的使用 Any 方法的 Linq 查询示例中,大约有一半是通过将其应用于 Where() 调用的结果来实现的,另一半则直接将其应用于集合。这两种样式是否总是等效的,或者在某些情况下它们可能会返
这个问题在这里已经有了答案: What does !!(x) mean in C (esp. the Linux kernel)? (3 个答案) 关闭 9 年前。 我见过人们使用带有两个 '!'
我对部署在生产环境中的应用程序进行了线程转储,该应用程序使用 logback。我不是分析线程转储的专家,但是,我必须这样做。正在学习,网上也看了一些文章。 下面是真正的线程转储: "logback-8
在 SQL 中(特别是 Postgres): 子句 where not foo='bar' in case foo is null 评估为某种 null,导致该行不是包含在结果中。 另一方面,子句 w
是不是类似于has and condition with join和where condition after join? 例如 对于以下两个查询,它会给我相同的结果吗 1) SELECT COUNT
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
如果您调用某个函数,并且该函数在发生错误时返回 NULL(例如,想想 malloc() 或 fopen()),两个更好: FILE *fp = fopen(argv[0], "r"); if (fp
我正在使用 Azure 数据工厂 V2,我需要在父检查验证中实现两级检查。例如:如果条件一为真,那么我需要检查条件 2。并且,如果条件 2 为真,则检查条件 3。 这是一种分层检查。当我在父 IF 条
使用 Linq to Entities 有以下区别吗? db.EntityName.Where(a => a.Id == id).FirstOrDefault(); db.EntityName.Fir
我有一种情况,我已经用两种不同的方式解决了,但想知道人们对这些选项的看法,以及他们是否有其他选择...... 系统正在处理数据的“间隔”。 所有数据都分配到一个“区间” 该间隔由事实表 中的“inte
我有包含字段 Amount, Condition1, Condition2 的表格。 例子: Amount Condition1 Condition2 ---------------------
我正在尝试在 Netbeans 中制作一个简单的 MySQL、Java JDBC Web 应用程序。我希望根据当前 session 中的状态变量显示不同的内容。我尝试了以下方法: 首先,我在 .jsp
我想为 postnuke cms 设计一个主题。 并希望在模板文件中使用 css 条件。 postnuke 使用类似 smarty 的标签 .... 所以当我使用 .... 它给出了一些关于标签的错误
我想问一下asyncio.Condition .我对这个概念并不熟悉,但我从学生时代就知道并了解锁、信号量和队列。 我找不到很好的解释或典型的用例,只是 this example .我看了看来源。核心
我想知道如何在不在语句中重做相同查询两次的情况下处理 SQL 比较。这是我要找的: SELECT columnName10, IF( SELECT columnName20 FROM Othe
我是一名优秀的程序员,十分优秀!