복붙노트

[HADOOP] 년 월 일 파티션 열 목록에서 최신 / 최근 파티션을 추출하는 방법

HADOOP

년 월 일 파티션 열 목록에서 최신 / 최근 파티션을 추출하는 방법

spark sql에서 show partitions를 사용하여 다음을 제공합니다.

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
part_year=2019
part_month=1
part_day=29 

내가 사용한 :

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

나는 그것을 얻는다.

2019/month

이것을 제거하기 위해 다른 데이터 프레임을 사용하여 regex_replace를 사용하여 달을 공백으로 바꾸고 다른 데이터 프레임을 만듭니다.

이는 많은 오버 헤드를 발생시킵니다. 내가 원했던 것은 이러한 모든 단계가 하나의 데이터 프레임에서 이루어 지므로 결과 데이터 프레임을 다음과 같이 얻을 수 있습니다.

part_year=2019
part_month=2
part_day=27

최신 파티션을 가져옵니다.

해결법

  1. ==============================

    1.spark.sql (s "show Partitions $ yourtablename") 후에는 작은 데이터이므로 아무런 문제가없는 데이터 프레임을 수집하게됩니다.

    spark.sql (s "show Partitions $ yourtablename") 후에는 작은 데이터이므로 아무런 문제가없는 데이터 프레임을 수집하게됩니다.

    일단 데이터 프레임 파티션을 수집하면 다음과 같은 배열을 얻을 수 있습니다.

           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)
    

    최종 결과 : 즉, 가장 최근 날짜가 2019-02-27이므로이를 바탕으로 하이브 데이터를 최적화 된 방식으로 쿼리 할 수 ​​있습니다.

     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
    
  2. from https://stackoverflow.com/questions/56319532/how-to-extract-latest-recent-partition-from-the-list-of-year-month-day-partition by cc-by-sa and MIT license