spark partition data writing by timestamp

前端 未结 2 687
迷失自我
迷失自我 2021-02-08 14:56

I have some data which has timestamp column field which is long and its epoch standard , I need to save that data in split-ted format like yyyy/mm/dd/hh using spark scala

<
2条回答
  •  故里飘歌
    2021-02-08 15:04

    First, I would caution you with over-partitioning. That is, make sure you have sufficient data to make it worth partitioning by hour otherwise you could end up with lots of partition folders with small files. The second caution I would make is from using a partition hierarchy (year/month/day/hour) since it will require a recursive partition discovery.

    Having said that, if you definitely want to partition by hour segments I would suggest truncating your timestamp to the hour into a new column and partitioning by that. Then, Spark will be smart enough to recognize the format as a timestamp when you read it back in and you can actually perform full filtering as needed.

    input
      .withColumn("ts_trunc", date_trunc("HOUR", 'timestamp)) // date_trunc added in Spark 2.3.0
      .write
      .partitionBy("ts_trunc")
      .save("/mnt/warehouse/part-test")
    
    spark.read.load("/mnt/warehouse/part-test").where("hour(ts_trunc) = 10")
    

    The other option would to partition by date and hour of day as so:

    input
      .withColumn("date", to_date('timestamp))
      .withColumn("hour", hour('timestamp))
      .write
      .partitionBy("date", "hour")
      .save("/mnt/warehouse/part-test")
    

提交回复
热议问题