如何检查HIVE中是否存在任何特定分区

时间:2017-03-29 06:41:23

标签: shell hadoop hive

如何检查HIVE中是否存在任何特定分区:

我的hive表中有分区,如下所示:

国=印度/状态= MH   国= US /状态= NY

我想检查国家/地区是否"某事和状态="某事"是否存在于HIVE或使用shell脚本。请帮助

3 个答案:

答案 0 :(得分:4)

  1. desc mytable partition(...)
  2. show table extended mytable partition(...)
  3. 使用hive -e '...'

    从shell执行

    演示

    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}
    

答案 1 :(得分:1)

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

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

您可以复制其他分区。

答案 2 :(得分:0)

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

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

这将为您提供所有匹配的分区,其结果与印度或MH或两者都匹配