flinksql下的时间日期格式转换

时间:2025-04-09 07:16:10

flinksql里面最常用的事情就是时间格式转换,比如各种时间格式转换成TIMESTAMP(3).
now() bigint-- CAST(TO_TIMESTAMP(log_time) as TIMESTAMP(3)) ,log_time=now()
localtimestamp timestamp(3)

timestamp – 不带括号数字表示timestamp(6)
now() 1403006911000 bigint – 毫秒时间戳数值 1528257600000
localtimestamp 1636272032500 timestamp(3) – 毫秒时间戳
timestamp(3) 1636272032500 – 毫秒时间戳
timestamp(9)
timestamp(6)

TIMESTAMP(9) TO_TIMESTAMP(BIGINT time)
TIMESTAMP(9) TO_TIMESTAMP(STRING time)
TIMESTAMP(9) TO_TIMESTAMP(STRING time, STRING format)
BIGINT TIMESTAMP_TO_MS(TIMTSTAMP time)
BIGINT TIMESTAMP_TO_MS(STRING time, STRING format)

TO_DATE(CAST(LOCALTIMESTAMP AS VARCHAR))

FROM_UNIXTIME(TIMESTAMP_TO_MS(localtimestamp)/1000, ‘yyyy-MM-dd HH:mm:ss’)

event_time 6点到6点
time_pt as cast(to_timestamp(eventTime - 6 * 3600 * 1000) as TIMESTAMP(3)) – 偏移6小时

date_format(to_timestamp(eventTime-636001000), ‘yyyy-MM-dd’) = date_format(to_timestamp((UNIX_TIMESTAMP(localtimestamp) -6*3600)*1000),‘yyyy-MM-dd’)

,gmt_modify TIMESTAMP(3) – ‘更新时间’
,WATERMARK FOR gmt_modify AS gmt_modify - INTERVAL ‘60’ SECOND
TIMESTAMP_TO_MS(gmt_modify) - now()
参考文档:
https://ci.apache.org/projects/flink/flink-docs-master/zh/docs/dev/table/concepts/timezone/