将 unix_timestamp 转换为 spark 中的正常时间戳(带秒)

convert a unix_timestamp to normal timestamp(with seconds) in spark

df.schema

操作:

StructField(time,LongType,true)

代码:

df.registerTempTable("dfTbl")
dfNew= spark.sql("""
                 SELECT *, cast(time as Timestamp) as newTIMESTMP 
                 FROM dfTbl d
              """)

我得到了输出,但值很奇怪。

例如:

50505-01-01

对可能出现的错误有什么建议吗?

根据问题,OP 有纪元的时间字段。因此,在执行查询之前应将其转换为秒数。

df.registerTempTable("dfTbl")
dfNew= spark.sql("""
             SELECT *, cast(time/1000 as Timestamp) as newTIMESTMP 
             FROM dfTbl d
          """)

将您的 unix 时间戳传递给以下函数

def unixtodate(utime):
  import datetime
  current=datetime.datetime(1970,01,01,00,00,00)+datetime.timedelta(seconds=utime)
  return current