Using library sparklyr
, I try to create a date variable in the Spark dataframe this way (which works in R):
# Researching SPARK --------------------------------------------------------------------------
#library(data.table)
library(sparklyr)
library(dplyr)
setwd('C:/Users/aburnakov/Desktop')
#spark_install(version = "2.1.0")
r_spark_connection <- spark_connect(master = "local")
sample_dat <- data.frame(When = as.character(
c(
"2018-01-15 03:05:02.177"
, "2018-01-15 00:54:31.133"
, "2018-01-15 21:24:06.013"
, "2018-01-15 15:44:26.047"
, "2018-01-15 05:17:06.040"
, "2018-01-15 06:41:08.183"
, "2018-01-15 15:09:40.137"
, "2018-01-15 03:15:43.820"
, "2018-01-15 11:02:27.180"
, "2018-01-15 18:23:42.047"
)
)
)
write.csv(x = sample_dat, file = 'sample_dat.csv')
## write raw data from csv to spark env ------------------------------------------
sample_spark_df <- spark_read_csv(
sc = r_spark_connection
, name = 'sample_dat'
, path = 'sample_dat.csv'
, header = T
, infer_schema = F
, delimiter = ","
, quote = '"'
, escape = '`'
, charset = "UTF-8"
, null_value = NULL
, repartition = 10
, memory = F
, overwrite = T
)
## try either of two
sample_spark_df <- sample_spark_df %>%
mutate(
Date = as.Date(When, format = "%Y-%m-%d", tz = "")
)
sample_spark_df <- sample_spark_df %>%
mutate(
datetime_when = as.POSIXct(strptime(x = When, format = "%Y-%m-%d %H:%M:%OS", tz = ""))
)
## now observe the error
x <- collect(sample_spark_df)
Why is this? Can I still make the dates with indicating format and timezone?
similar troubles: Converting string/chr to date using sparklyr