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

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


  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);OKi                       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);OKtableName:mytableowner:clouderalocation:hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/year=2017/month=3/day=29inputformat:org.apache.hadoop.mapred.TextInputFormatoutputformat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormatcolumns:struct columns { i32 i}partitioned:truepartitionColumns:struct partition_columns { i32 year, byte month, byte day}totalNumberFiles:1totalFileSize:2maxFileSize:2minFileSize:2lastAccessTime:1490770378864lastUpdateTime:1490770379748hive> 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}


res=`hive -e "use {db}; show partitions {table} partition(country='india',state='MH')"`if [ ! -z "$res" ]; then   do sth if the partition existsfi

You can replicate for other partitions.


hive -e "use <db>; show partitions <table>;" egrep --color '<countryName>|<stateName>'

ex: hive -e "use db; show partitions table;" egrep --color 'India|MH'

this will give you all the matching partitions with results that match either India or MH or both