1. 程式人生 > >HIVE --- 分割槽表

HIVE --- 分割槽表

建立分割槽表

  建立表時,新增partitioned by欄位,如下:

create table table_name (
  id                int,
  dtDontQuery       string,
  name              string
)
partitioned by (date string)

注意:用於分割槽的欄位不能在表字段中重複定義;

查詢分割槽表

  where條件中增加查詢欄位,如下:

select * from table whre date = ...

是否為分割槽表的判斷

  思路:判斷表的元資訊中partitionKeys陣列是否為空;
對於非分割槽表,partitionKeys陣列為空,如下:
在這裡插入圖片描述


對於分割槽表,partitionKeys陣列羅列出具體的分割槽欄位,如下:
在這裡插入圖片描述

static public boolean isPartitionTable(final Statement stmt, final String tableFullName) throws SQLException{
        stmt.execute(DDL_OUTPUT_FORMAT);
        String sql = String.format(TABLE_HDFS_PATH, tableFullName);
        try(ResultSet rs = stmt.executeQuery(sql)) {
            if(rs.next()) {
                String data = rs.getString(1);
                JSONObject obj = JSON.parseObject(data);
                JSONArray jsonArray = obj.getJSONObject("tableInfo").getJSONArray("partitionKeys");
                return !jsonArray.isEmpty();
            }
        }
        throw new BusinessException(BusinessCode.JUDGE_PARTITION_TABLE_ERROR);
    }

參考:

  1. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-PartitionedTables;