版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangguosb/article/details/83796168
创建分区表
创建表时,添加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);
}
参考: