ablog

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

2021-06-12から1日間の記事一覧

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

PySpark でタイムスタンプを UTC から JST に変換する例。 # 文字列をタイムスタンプ型に変換 df = df.withColumn("timestamp", col("timestamp").cast("Timestamp")) # UTC から JST に変換 df = df.withColumn("timestamp", from_utc_timestamp(col("times…

Glue PySpark で CSV 出力時に全カラムをダブルクオートで囲む

Glue PySpark で CSV 出力時に全カラムをダブルクオートで囲みたいときは DataDrame で write するときに quoteAll=True を指定してやればよい。 outputDf = newDf.repartition(1) s3OutputPath ="s3://dl-sfdc-dm/test/newline_test" outputDf.write.mode('…

Glue PySpark で CSV のカラム内の改行コードを置換する

Glue PySpark で CSV のカラム内の改行コードを置換する例。Spark では正規表現は Java の記法になる。 newDf = df.withColumn("col2", regexp_replace(col("col2"), "\\n|\\r", " ")) サンプルコード全量 import sys from awsglue.transforms import * from…

PySpark は Java の正規表現記法を使う

PySpark では Java の正規表現を使う Regex in pyspark internally uses java regex.One of the common issue with regex is escaping backslash as it uses java regex and we will pass raw python string to spark.sql we can see it with a sample examp…