ablog

不器用で落着きのない技術者のメモ

PySpark でタイムスタンプを UTC から JST に変換する

PySpark でタイムスタンプを UTC から JST に変換する例。

# 文字列をタイムスタンプ型に変換
df = df.withColumn("timestamp", col("timestamp").cast("Timestamp"))
# UTC から JST に変換
df = df.withColumn("timestamp", from_utc_timestamp(col("timestamp"),"Asia/Tokyo"))
# タイムスタンプ型から文字列型に変換
df = df.withColumn("timestamp", date_format(col("timestamp"), 'yyyy-MM-dd HH:mm:ss.S'))

コードサンプル

  • AWS Glue PySpark のコードサンプル
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
from pyspark.sql.functions import from_utc_timestamp
from awsglue.dynamicframe import DynamicFrame
from pyspark.sql.functions import col
from pyspark.sql.functions import *
from pyspark.sql.functions import date_format

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
## @type: DataSource
## @args: [database = "default", table_name = "timestamp_test", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "default", table_name = "timestamp_test", transformation_ctx = "datasource0")
## @type: ApplyMapping
## @args: [mapping = [("timestamp", "string", "timestamp", "string"), ("col2", "string", "col2", "string"), ("col3", "string", "col3", "string"), ("col4", "string", "col4", "string")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("timestamp", "string", "timestamp", "string"), ("col2", "string", "col2", "string"), ("col3", "string", "col3", "string"), ("col4", "string", "col4", "string")], transformation_ctx = "applymapping1")

df = DynamicFrame.toDF(applymapping1)
df = df.withColumn("timestamp", col("timestamp").cast("Timestamp"))
df = df.withColumn("timestamp", from_utc_timestamp(col("timestamp"),"Asia/Tokyo"))
df = df.withColumn("timestamp", date_format(col("timestamp"), 'yyyy-MM-dd HH:mm:ss.S'))
result = DynamicFrame.fromDF(df, glueContext, "result")

result = DynamicFrame.fromDF(df, glueContext, "result")
## @type: DataSink
## @args: [connection_type = "s3", connection_options = {"path": "s3://dl-sfdc-dm/test/timestamp_test"}, format = "csv", transformation_ctx = "datasink2"]
## @return: datasink2
## @inputs: [frame = applymapping1]
datasink2 = glueContext.write_dynamic_frame.from_options(frame = result, connection_type = "s3", connection_options = {"path": "s3://dl-sfdc-dm/test/timestamp_test"}, format = "csv", transformation_ctx = "datasink2")
job.commit()
"2021-05-07T04:59:43.000+0000","line1","line1","line1"
"2021-05-07T05:59:43.000+0000","line2","line2","line2"
"2021-05-07T06:59:43.000+0000","line3","line3","line3"
"2021-05-07T07:59:43.000+0000","line4","line4","line4"
"2021-05-07T08:59:43.000+0000","line5","line5","line5"

参考

UTCが入っているデータに対してJSTの列を追加してJSTの時間に変換してみます。

from pyspark.sql.functions import from_utc_timestamp
df = spark.createDataFrame([
        (1, "2020-12-19 00:00:00"),
        (2, "2020-12-19 20:00:00")
    ],
    ["id", "UTC"])

df = df.withColumn("UTC", col("UTC").cast("Timestamp"))
df = df.withColumn("JST", from_utc_timestamp(col("UTC"),"Asia/Tokyo"))
df.show()

JSTの列が +9 されていることが分かります。

+---+-------------------+-------------------+
| id|                UTC|                JST|
+---+-------------------+-------------------+
|  1|2020-12-19 00:00:00|2020-12-19 09:00:00|
|  2|2020-12-19 20:00:00|2020-12-20 05:00:00|
+---+-------------------+-------------------+
PySparkでUTCで入っている時刻をJSTに変換する | Simple is Beautiful.