问题描述
我正在使用 sparklyr
包转换我的原始 R 代码以使其适用于 Spark。我使用 lubridate
包来计算两个日期之间的天数。在 R 中,这会生成 duration
数据类型,然后可以将其转换为数值数据类型,如下例所示。
# Load packages
library(sparklyr)
library(dplyr)
library(lubridate)
# Create dataframe with start and end date
df <- tibble(start = ymd("20210101"),end = ymd("20210105"))
df
---
# A tibble: 1 x 2
start end
<date> <date>
1 2021-01-01 2021-01-05
---
# Calculate duration and convert to numeric using R dataframe
df %>%
mutate(dur = end - start,dur_num = as.numeric(dur))
---
# A tibble: 1 x 4
start end dur dur_num
<date> <date> <drtn> <dbl>
1 2021-01-01 2021-01-05 4 days 4
---
使用 sparklyr
对 Spark 数据帧执行完全相同的转换会产生错误,因为持续时间数据类型会自动转换为字符串数据类型。下面的示例显示了代码和错误。请忽略本地R转Spark时因时区差异导致的日期变化。
## Connect to local Spark cluster
sc <- spark_connect(master = "local",version = "3.0")
# copy dataframe to Spark
df_spark <- copy_to(sc,df)
# Calculate duration using Spark dataframe
df_spark %>%
mutate(dur = end - start)
---
# Source: spark<?> [?? x 3]
start end dur
<date> <date> <chr>
1 2020-12-31 2021-01-04 4 days
---
# Calculate duration and convert to numeric using Spark dataframe
df_spark %>%
mutate(dur = end - start,dur_num = as.numeric(dur))
---
Error: org.apache.spark.sql.AnalysisException: cannot resolve 'CAST(q01.`dur` AS DOUBLE)' due to data type
mismatch: cannot cast interval to double; line 1 pos 30;
'Project [start#58,end#59,dur#280,cast(dur#280 as double) AS dur_num#281]
+- SubqueryAlias q01
+- Project [start#58,subtractdates(end#59,start#58) AS dur#280]
+- SubqueryAlias df
+- LogicalRDD [start#58,end#59],false
---
是否可以使用 lubridate::duration
在 Spark 中使用 sparklyr
数据类型?如果没有,有没有办法绕过转换为字符串的天数作为结果的两倍?感谢所有帮助。
解决方法
您可以使用内置 hive 函数来执行此操作:
df_spark %>%
mutate(dur = datediff(end,start))
# Source: spark<?> [?? x 3]
start end dur
<date> <date> <int>
1 2021-01-01 2021-01-05 4
在做 diff 之前是否是一个 datetime 对象将 datetime 对象转换为数字,例如
df <- tibble(start = ymd_hms("20210101 00:00:00"),end = ymd_hms("20210105 00:00:00"))
df_spark <- copy_to(sc,df)
df_spark %>%
mutate(dur = (as.numeric(end) - as.numeric(start))/(3600*24))
# Source: spark<?> [?? x 3]
start end dur
<dttm> <dttm> <dbl>
1 2021-01-01 00:00:00 2021-01-05 00:00:00 4