1

I am looking for a hive/SQL query that gives the output in such a way it prints both the table name and the partitions respectively... Or similar query which executes fast and gives the latest partition and the table name

SELECT 'table1' AS table_name,* FROM(show partitions table1) AS a;
jishan siddique
  • 1,848
  • 2
  • 12
  • 23
Codeit
  • 21
  • 3

2 Answers2

1

It is not possible to use "SHOW PARTITIONS" in a HIVE statement. What you can do is to use "Distinct". However, this is not a good approach. Code example:

select "table1" as name, * from (select distinct(partitition_name) from test_table) as b;´

Another option is to use HDFS commands in a Shell Scrip, or use another programming language, like pyspark o scala.

Also, you can access the Hive MetaStrore tables. The partitions information is available in the table "PARTITIONS". Code Example:

select PART_NAME FROM PARTITIONS WHERE TBL_ID=(SELECT TBL_ID FROM TBLS WHERE TBL_NAME='<table_name>');
0

You can not execute the show partitions command inside the query instead of doing this you can run distinct query on partition column and use this query as nested query like shown below.

I have a table t2 which is partition on column roll

show partitions t2;
OK
roll=2
roll=3

to show partition and table name together you can refer below approach

select "t2",a.part 
from 
(
select distinct roll as part from t2
) a ;

Total MapReduce CPU Time Spent: 2 seconds 940 msec
OK
t2  2
t2  3

To be more specific result

 > select "t2",concat("roll=",a.part)
>     from 
>     (
>     select distinct roll as part from t2
>     ) a ;

Total MapReduce CPU Time Spent: 3 seconds 290 msec
OK
t2  roll=2
t2  roll=3
Strick
  • 1,512
  • 9
  • 15