Как сводить данные в Hive с агрегацией - PullRequest
0 голосов
/ 05 сентября 2018

У меня есть данные таблицы, как показано ниже, и я хочу объединить данные с агрегацией.

ColumnA    ColumnB            ColumnC
1          complete            Yes
1          complete            Yes
2          In progress         No
2          In progress         No 
3          Not yet started     initiate 
3          Not yet started     initiate 

Хотите развернуться, как показано ниже

ColumnA          Complete    In progress     Not yet started
1                 2               0                0
2                 0               2                0
3                 0               0                2

В любом случае, мы можем достичь этого в улье или Импале?

Ответы [ 2 ]

0 голосов
/ 06 сентября 2018

Вот как вы можете сделать это в искровом скале.

     val conf = spark.sparkContext.hadoopConfiguration
        val test = spark.sparkContext.parallelize(List(  ("1", "Complete", "yes"),
                                        ("1", "Complete", "yes"),
                                        ("2", "Inprogress", "no"),
                                        ("2", "Inprogress", "no"),
                                       ("3", "Not yet started", "initiate"),
                                        ("3", "Not yet started", "initiate"))


                                        ).toDF("ColumnA","ColumnB","ColumnC")
      test.show()
       val test_pivot = test.groupBy("ColumnA")
                           .pivot("ColumnB")
                           .agg(count("columnC"))

  test_pivot.na.fill(0)show(false)


       }

и вывод

|ColumnA|Complete|Inprogress|Not yet started|
+-------+--------+----------+---------------+
|3      |0       |0         |2              |
|1      |2       |0         |0              |
|2      |0       |2         |0              |
+-------+--------+----------+---------------+
0 голосов
/ 05 сентября 2018

Использование case с sum агрегацией:

select ColumnA,    
       sum(case when ColumnB='complete'        then 1 else 0 end) as Complete,
       sum(case when ColumnB='In progress'     then 1 else 0 end) as In_progress,
       sum(case when ColumnB='Not yet started' then 1 else 0 end) as Not_yet_started
  from table
 group by ColumnA
 order by ColumnA --remove if order is not necessary
;
...