问题描述
我有一个是(通过一个JSON文件)上传到SparkR从MySQL正式上课数据框对象,其中包含格式的字符串是这样的:
2012-07-02 20点14分○○秒
I have a Formal Class DataFrame object that was uploaded to SparkR from MySQL (via a json file), which contains formatted strings like this: "2012-07-02 20:14:00"
我需要把这些转换为SparkR日期时间类型,但这似乎并不能被支持。是否有一个未公开的函数或者用UDF这样的食谱? (注意,我并没有真正尝试过创建SparkR UDF,所以我抓救命稻草,在这里。)
I need to convert these to a datetime type in SparkR, but this does not seem to be supported yet. Is there an undocumented function or a recipe for doing this with a UDF? (Nb. I haven't actually tried creating a SparkR UDF before, so I'm grasping at straws, here.)
推荐答案
星火SQL不支持研究的UDF但在这种特殊情况下你可以简单地转换为时间戳
:
Spark SQL doesn't support R UDFs but in this particular case you can simply cast to timestamp
:
df <- createDataFrame(sqlContext,
data.frame(dts=c("2012-07-02 20:14:00", "2015-12-28 00:10:00")))
dfWithTimestamp <- withColumn(df, "ts", cast(df$dts, "timestamp"))
printSchema(dfWithTimestamp)
## root
## |-- dts: string (nullable = true)
## |-- ts: timestamp (nullable = true)
head(dfWithTimestamp)
## dts ts
## 1 2012-07-02 20:14:00 2012-07-02 20:14:00
## 2 2015-12-28 00:10:00 2015-12-28 00:10:00
这篇关于将字符串转换为datetime在SparkR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!