【发布时间】:2021-05-13 18:39:26
【问题描述】:
我正在使用 spark-2.4.1v。我的项目中有一个用例,对于每个日期(process_date),我需要将当天记录与前一天记录一起考虑,并对该数据集执行某些其他操作。 那么如何为此准备数据集呢?我尝试了滞后功能,但没有取得太大的成功。
对于上述用例,给定数据如下:
+----------+----------+----+-------+------------+-----------+
|company_id| gen_date|year|quarter|total_assets|create_date|
+----------+----------+----+-------+------------+-----------+
| 989856662|2019-01-02|2019| 1| 3900.435058| 2019-09-11|
| 989856665|2019-01-02|2019| 1| 4836.435058| 2019-09-11|
| 989856667|2019-01-02|2019| 1| 5836.435058| 2019-09-11|
| 989856662|2019-01-01|2019| 1| 3800.435058| 2019-09-11|
| 989856665|2019-01-01|2019| 1| 3834.435058| 2019-09-11|
| 989856667|2019-01-01|2019| 1| 5834.435058| 2019-09-11|
| 989856662|2018-12-31|2018| 4| 3700.435058| 2019-09-11|
| 989856665|2018-12-31|2018| 4| 3900.435058| 2019-09-11|
| 989856667|2018-12-31|2018| 4| 5833.435058| 2019-09-11|
| 989856662|2018-12-30|2018| 4| 3832.435058| 2019-09-11|
| 989856665|2018-12-30|2018| 4| 3700.435058| 2019-09-11|
| 989856667|2018-12-30|2018| 4| 5832.435058| 2019-09-11|
+----------+----------+----+-------+------------+-----------+
这里 gen_date 是关键列,对于每个 gen_date ,我需要取 它以前可用的 gen_date 记录。这些将被处理 一起设置,即 process_date 2019-01-02 - 它应该有 2019-01-02 和 2019-01-01 的记录同样适用于 process_date 记录 gen_date 2018-12-30 及其之前的 gen_date 即 2018-12-29,但是 此处 2018-12-29 gen_date 记录不可用,因此应该是 考虑 gen_date 2018-12-30 记录。
在给定的集合中
对于 process_date 2019-01-02 => (gen_date 2019-01-02) 的记录 + (gen_date 2019-01-01) 的记录 对于 process_date 2019-01-01 => (gen_date 2019-01-01) 的记录 + (gen_date 2018-12-31) 的记录 对于 process_date 2018-12-31 => (gen_date 2018-12-31) 的记录 + (gen_date 2018-12-30) 的记录 对于 process_date 2018-12-30 => (gen_date 2018-12-30) 的记录 + 没有以前的 gen_date 记录。
输出应该如下:
+----------+------------+----------+----+-------+------------+-----------+
|company_id|process_date| gen_date|year|quarter|total_assets|create_date|
+----------+------------+----------+----+-------+------------+-----------+
| 989856662| 2019-01-02|2019-01-02|2019| 1| 3900.435058| 2019-09-11|
| 989856662| 2019-01-02|2019-01-01|2019| 1| 3800.435058| 2019-09-11|
| 989856665| 2019-01-02|2019-01-02|2019| 1| 4836.435058| 2019-09-11|
| 989856665| 2019-01-02|2019-01-01|2019| 1| 3834.435058| 2019-09-11|
| 989856667| 2019-01-02|2019-01-02|2019| 1| 5836.435058| 2019-09-11|
| 989856667| 2019-01-02|2019-01-01|2019| 1| 5834.435058| 2019-09-11|
| 989856662| 2019-01-01|2019-01-01|2019| 1| 3800.435058| 2019-09-11|
| 989856662| 2019-01-01|2018-12-31|2018| 4| 3700.435058| 2019-09-11|
| 989856665| 2019-01-01|2019-01-01|2019| 1| 3834.435058| 2019-09-11|
| 989856665| 2019-01-01|2018-12-31|2018| 4| 3900.435058| 2019-09-11|
| 989856667| 2019-01-01|2019-01-01|2019| 1| 5834.435058| 2019-09-11|
| 989856667| 2019-01-01|2018-12-31|2018| 4| 5833.435058| 2019-09-11|
| 989856662| 2018-12-31|2018-12-31|2018| 4| 3700.435058| 2019-09-11|
| 989856662| 2018-12-31|2018-12-30|2018| 4| 3832.435058| 2019-09-11|
| 989856665| 2018-12-31|2018-12-31|2018| 4| 3900.435058| 2019-09-11|
| 989856665| 2018-12-31|2018-12-30|2018| 4| 3700.435058| 2019-09-11|
| 989856667| 2018-12-31|2018-12-31|2018| 4| 5833.435058| 2019-09-11|
| 989856667| 2018-12-31|2018-12-30|2018| 4| 5832.435058| 2019-09-11|
| 989856662| 2018-12-30|2018-12-30|2018| 4| 3832.435058| 2019-09-11|
| 989856665| 2018-12-30|2018-12-30|2018| 4| 3700.435058| 2019-09-11|
| 989856667| 2018-12-30|2018-12-30|2018| 4| 5832.435058| 2019-09-11|
+----------+------------+----------+----+-------+------------+-----------+
how to achieve above output ?
以下是所附笔记本网址。
【问题讨论】:
标签: dataframe apache-spark apache-spark-sql