Convert Double to Date using Spark in R

依然范特西╮ 提交于 2019-11-29 17:02:26

This looks like a sparklyr bug. The simplest workaround is to cast dates to character, before calling copy_to:

df <- tibble::tibble(Date=as.Date(c("2018-02-05", "2018-02-06")))
sdf <- df %>% mutate(Date = as.character(Date)) %>% copy_to(sc, .)

sdf
# Source:   table<sparklyr_11ae23aa677e> [?? x 1]
# Database: spark_connection
  Date      
  <chr>     
1 2018-02-05
2 2018-02-06

and casting it later:

sdf %>% mutate(Date = to_date(Date))
# Source:   lazy query [?? x 1]
# Database: spark_connection
  Date      
  <date>    
1 2018-02-05
2 2018-02-06

You can also try using the numeric value as an offset since beginning of the Unix epoch:

sdf <- df  %>% copy_to(sc, .)

sdf
# Source:   table<sparklyr_13ab19ec6f53> [?? x 1]
# Database: spark_connection
   Date
  <dbl>
1 17567
2 17568
sdf %>% mutate(Date = date_add(to_date("1970-01-01"), Date))
# Source:   lazy query [?? x 1]
# Database: spark_connection
  Date      
 <date>    
1 2018-02-05
2 2018-02-06

Alternatively, you can skip copy_to completely (it has very limited applications anyway, and is seldom useful in production) and use one of built-in input formats (spark_read_*).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!