How to extract latest/recent partition from the li

2020-05-09 23:48发布

问题:

I have used show partitions in spark sql which gives me the following:

year=2019/month=1/day=21
year=2019/month=1/day=22
year=2019/month=1/day=23
year=2019/month=1/day=24
year=2019/month=1/day=25
year=2019/month=1/day=26
year=2019/month=2/day=27
  1. I need to extract latest partition
  2. I need to the year, month and day separately so I can use it in another dataframe as variables. I.e:
part_year=2019
part_month=1
part_day=29 

I have used:

val overwrite2 = overwrite.select(col("partition",8,8) as year

from which I get

2019/month

For removing this I use another dataframe where I use regex_replace to replace month with blank so another dataframe is created.

This is in turn creating a lot of overhead. What I want is for all these steps to be done in one dataframe so I can get the resultant dataframe as:

part_year=2019
part_month=2
part_day=27

with latest partition being picked up.

回答1:

Question : How to extract latest/recent partition from the list of year month day partition columns

1) I need to extract latest partition.

2) I need to the year, month and day separately so I can use it in another dataframe as variables.

  • Since final goal is to get latest/recent partition... You can use joda api DateTime by sorting with isAfter to get latest partition like given as below example.

After spark.sql(s"show Partitions $yourtablename") you will get a dataframe collect that since its small data no issue.

once you collect the dataframe partitions you will get an array like this

       val x = Array(
    "year=2019/month=1/day=21",
    "year=2019/month=1/day=22",
    "year=2019/month=1/day=23",
    "year=2019/month=1/day=24",
    "year=2019/month=1/day=25",
    "year=2019/month=1/day=26",
    "year=2019/month=2/day=27"
  )
  val finalPartitions = listKeys()

  import org.joda.time.DateTime

  def listKeys(): Seq[Map[String, DateTime]] = {
    val keys: Seq[DateTime] = x.map(row => {
      println(s" Identified Key: ${row.toString()}")
      DateTime.parse(row.replaceAll("/", "")
        .replaceAll("year=", "")
        .replaceAll("month=", "-")
        .replaceAll("day=", "-")
      )
    })
      .toSeq
    println(keys)
    println(s"Fetched ${keys.size} ")
    val myPartitions: Seq[Map[String, DateTime]] = keys.map(key => Map("businessdate" -> key))

    myPartitions
  }
  val mapWithMostRecentBusinessDate = finalPartitions.sortWith(
    (a, b) => a("businessdate").isAfter(b("businessdate"))
  ).head

  println(mapWithMostRecentBusinessDate)
  val latest: Option[DateTime] = mapWithMostRecentBusinessDate.get("businessdate")
  val year = latest.get.getYear();
  val month = latest.get.getMonthOfYear();
  val day = latest.get.getDayOfMonth();
  println("latest year "+ year + "  latest month " + month + "  latest day  " + day)

Final result : i.e. your most recent date is 2019-02-27 now based on this you can query hive data in an optimized way.

 Identified Key: year=2019/month=1/day=22
 Identified Key: year=2019/month=1/day=23
 Identified Key: year=2019/month=1/day=24
 Identified Key: year=2019/month=1/day=25
 Identified Key: year=2019/month=1/day=26
 Identified Key: year=2019/month=2/day=27
WrappedArray(2019-01-21T00:00:00.000-06:00, 2019-01-22T00:00:00.000-06:00, 2019-01-23T00:00:00.000-06:00, 2019-01-24T00:00:00.000-06:00, 2019-01-25T00:00:00.000-06:00, 2019-01-26T00:00:00.000-06:00, 2019-02-27T00:00:00.000-06:00)
Fetched 7 
Map(businessdate -> 2019-02-27T00:00:00.000-06:00)
latest year 2019  latest month 2  latest day  27