Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether any particular partition exist or not in HIVE

Tags:

shell

hadoop

hive

How to check whether any particular partition exist or not in HIVE:

I have partitions in my hive table as below:

country=india/state=MH country=US/state=NY

i want to check whether country = "something and state="something" exist or not in HIVE or using shell script.Please help

like image 341
BigD Avatar asked Mar 29 '17 06:03

BigD


2 Answers

  1. desc mytable partition (...)
  2. show table extended like mytable partition (...)

Execute from shell using hive -e '...'

Demo

create table mytable (i int) 
partitioned by (year int,month tinyint,day tinyint)
;

insert into mytable partition(year,month,day) values (1,2017,3,29)
;

hive> desc mytable partition (year=2017,month=3,day=29);
OK
i                       int                                         
year                    int                                         
month                   tinyint                                     
day                     tinyint                                     
         
# Partition Information      
# col_name              data_type               comment             
         
year                    int                                         
month                   tinyint                                     
day                     tinyint        

hive> desc mytable partition (year=2017,month=4,day=1);
FAILED: SemanticException [Error 10006]: Partition not found {year=2017, month=4, day=1}


hive> show table extended like mytable partition (year=2017,month=3,day=29);
OK
tableName:mytable
owner:cloudera
location:hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/year=2017/month=3/day=29
inputformat:org.apache.hadoop.mapred.TextInputFormat
outputformat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
columns:struct columns { i32 i}
partitioned:true
partitionColumns:struct partition_columns { i32 year, byte month, byte day}
totalNumberFiles:1
totalFileSize:2
maxFileSize:2
minFileSize:2
lastAccessTime:1490770378864
lastUpdateTime:1490770379748

hive> show table extended like mytable partition (year=2017,month=4,day=1);
FAILED: SemanticException [Error 10006]: Partition not found {year=2017, month=4, day=1}
like image 114
David דודו Markovitz Avatar answered Sep 29 '22 12:09

David דודו Markovitz


res=`hive -e "use {db}; show partitions {table} partition(country='india',state='MH')"`

if [ ! -z "$res" ]; then
   do sth if the partition exists
fi

You can replicate for other partitions.

like image 42
Junchen Avatar answered Sep 29 '22 12:09

Junchen