Spark 从另一个国家/地区获取当前日期



我需要获取另一个国家的日期和时间:

dateFormat = "%Y%m%d_%H%M"
ts=spark.sql(""" select current_timestamp() as ctime """).collect()[0]["ctime"]
ts.strftime(dateFormat)

你不需要pyspark来完成这样的任务,特别是当你调用.collect((时:

import pytz
from datetime import datetime
tz = pytz.timezone('Asia/Shanghai')
ts = datetime.now(tz)
ts.strftime('%Y%m%d_%H%M')

会话时区设置配置'spark.sql.session.timeZone',默认为JVM系统本地时区,您可以更改时区,添加您的时区将为您提供正确的日期

spark.conf.set("spark.sql.session.timeZone", "UTC")

可以使用以下代码从 pyspark 中的 utc 时间戳获取当前日期。可以根据要求更新时区。

import pytz
current_timestamp_utc = spark.sql("SELECT current_timestamp()").collect()[0][0]
awst_timezone = pytz.timezone('Australia/Perth')
current_date_awst = current_timestamp_utc.astimezone(awst_timezone).date()

最新更新