如何使用时间戳创建Spark DataFrame?
如何使用Python在一个步骤中使用时间戳数据类型创建此SPARK DATAFRAME?这是我分两个步骤进行的。使用Spark 3.1.2
from pyspark.sql.functions import *
from pyspark.sql.types import *
schema_sdf = StructType([
StructField("ts", TimestampType(), True),
StructField("myColumn", LongType(), True),
])
sdf = spark.createDataFrame( ( [ ( to_timestamp(lit("2022-06-29 12:01:19.000")), 0 ) ] ), schema=schema_sdf )
How can I create this Spark dataframe with timestamp data type in one step using python? Here is how I do it in two steps. Using spark 3.1.2
from pyspark.sql.functions import *
from pyspark.sql.types import *
schema_sdf = StructType([
StructField("ts", TimestampType(), True),
StructField("myColumn", LongType(), True),
])
sdf = spark.createDataFrame( ( [ ( to_timestamp(lit("2022-06-29 12:01:19.000")), 0 ) ] ), schema=schema_sdf )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Pyspark不会自动解释字符串的时间戳值。我主要使用以下语法来创建DF,然后使用
cast
列类型到时间戳:自动推断长格式,但是对于时间戳,我们需要
cast> cast
。另一方面,即使没有铸造,您也可以使用需要时间戳作为输入的功能:
PySpark does not automatically interpret timestamp values from strings. I mostly use the following syntax to create the df and then to
cast
column type to timestamp:Long format was automatically inferred, but for timestamp we needed a
cast
.On the other hand, even without casting, you are able to use functions which need timestamp as input: