指定 pyspark 写入的时间戳的格式
Specify format of Timestamp written by pyspark
背景是我在 1.6 上使用 databricks csv read/writer 开发的一个简单的 pyspark 程序,一切都很愉快。我的数据框有一个时间戳列,以标准 YYYY-MM-DD HH24:MI:SS
格式写出。
foo,bar,2016-10-14 14:30:31.985
现在我 运行 在 EMR 上使用 Spark 2,时间戳列被写为 以微秒为单位的纪元 。这会导致问题,因为目标 (Redshift) 本身无法处理(仅秒或毫秒)。
foo,bar,1476455559456000
查看the docs,似乎我应该能够指定与timestampFormat
一起使用的格式,但我只是得到一个错误:
TypeError: csv() got an unexpected keyword argument 'timestampFormat'
我是说错了,还是这个选项不存在?以 不是 微秒的格式干净地获取我的时间戳数据的任何其他方法(毫秒就可以,或者任何其他标准时间格式)
简单的重现代码:
df = sqlContext.createDataFrame([('foo','bar')]).withColumn('foo',pyspark.sql.functions.current_timestamp())
df.printSchema()
df.show()
# Use the new Spark 2 native method
df.write.csv(path='/tmp/foo',mode='overwrite')
# Use the databricks CSV method, pre Spark 2
df.write.save(path='/tmp/foo2',format='com.databricks.spark.csv',mode='overwrite')
原来我看的文档是针对 2.0.1 的,而我 运行 是针对 2.0.0 的——而 timestampFormat
是 2.0.1 中的新文档。
背景是我在 1.6 上使用 databricks csv read/writer 开发的一个简单的 pyspark 程序,一切都很愉快。我的数据框有一个时间戳列,以标准 YYYY-MM-DD HH24:MI:SS
格式写出。
foo,bar,2016-10-14 14:30:31.985
现在我 运行 在 EMR 上使用 Spark 2,时间戳列被写为 以微秒为单位的纪元 。这会导致问题,因为目标 (Redshift) 本身无法处理(仅秒或毫秒)。
foo,bar,1476455559456000
查看the docs,似乎我应该能够指定与timestampFormat
一起使用的格式,但我只是得到一个错误:
TypeError: csv() got an unexpected keyword argument 'timestampFormat'
我是说错了,还是这个选项不存在?以 不是 微秒的格式干净地获取我的时间戳数据的任何其他方法(毫秒就可以,或者任何其他标准时间格式)
简单的重现代码:
df = sqlContext.createDataFrame([('foo','bar')]).withColumn('foo',pyspark.sql.functions.current_timestamp())
df.printSchema()
df.show()
# Use the new Spark 2 native method
df.write.csv(path='/tmp/foo',mode='overwrite')
# Use the databricks CSV method, pre Spark 2
df.write.save(path='/tmp/foo2',format='com.databricks.spark.csv',mode='overwrite')
原来我看的文档是针对 2.0.1 的,而我 运行 是针对 2.0.0 的——而 timestampFormat
是 2.0.1 中的新文档。