简体   繁体   中英

how to combine show partition query with select statement to get the latest partition with the table name

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;

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>');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM