疑问 :
hive not null,是不是感觉not null是基于column的,所以就感觉not null是column中的一个字段,抱歉,还真不是,口说无凭,上代码看一下
表格信息:
// 表
private String tableName; // required
// db
private String dbName; // required
// 表的所属,基于用于、者角色或者组
private String owner; // required
// 表创建事件
private int createTime; // required
// 最后访问时间
private int lastAccessTime; // required
// 保留字段,默认0
private int retention; // required
// 存储相关,location,inputformat,outputformat,序列化反序列化器
private StorageDescriptor sd; // required
// 这个就是列信息了
private List<FieldSchema> partitionKeys; // required
// table properties信息
private Map<String,String> parameters; // required
// 视图相关
private String viewOriginalText; // required
private String viewExpandedText; // required
// 表类型,管理表、外部表、虚拟视图和物化视图
private String tableType; // required
// 表的权限相关
private PrincipalPrivilegeSet privileges; // optional
// session级别的临时表,create temporary table txx(id int);
private boolean temporary; // optional
// rewrite简单来说其实就是一个SQL能不能要使用物化视图来优化查询
private boolean rewriteEnabled; // optional
// 物化视图相关,物化视图关联的表等等
private CreationMetadata creationMetadata; // optional
// 默认是hive
private String catName; // optional
// 用户、角色或者所属组
private PrincipalType ownerType; // optional
FieldSchema列信息:
// 列名称
private String name; // required
// 列类型
private String type; // required
// 描述
private String comment; // required
是不是列中没有not null信息
设计
创建表的时候,如果配置了主键、外键、唯一索引、not null、默认值和自定义取值约束,都会走如下接口:
@Override
public void createTableWithConstraints(Table table,
List<SQLPrimaryKey> primaryKeys,
List<SQLForeignKey> foreignKeys,
List<SQLUniqueConstraint> uniqueConstraints,
List<SQLNotNullConstraint> notNullConstraints,
List<SQLDefaultConstraint> defaultConstraints,
List<SQLCheckConstraint> checkConstraints)
throws AlreadyExistsException, InvalidObjectException, MetaException, NoSuchObjectException, TException {
LOG.info("edap createTableWithConstraints -> primaryKeys : {}, foreignKeys : {}, uniqueConstraints : {}, notNullConstraints : {}, defaultConstraints : {}, checkConstraints : {}",
primaryKeys, foreignKeys, uniqueConstraints, notNullConstraints, defaultConstraints, checkConstraints);
throw new UnsupportedOperationException("createTableWithConstraints is not supported");
}
// ALTER TABLE t_user1 CHANGE COLUMN id id string CONSTRAINT id NOT NULL ENABLE;
// 不支持 ALTER TABLE t_user1,t_user2 CHANGE COLUMN id id string CONSTRAINT id NOT NULL ENABLE; 所以List<SQLNotNullConstraint>中只能有一个相同的db和table
@Override
public void addNotNullConstraint(List<SQLNotNullConstraint> notNullConstraints)
throws MetaException, NoSuchObjectException, TException {
LOG.info("edap addNotNullConstraint : notNullConstraints : {}", notNullConstraints);
}
@Override
public List<SQLNotNullConstraint> getNotNullConstraints(NotNullConstraintsRequest notNullConstraintsRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
对应的获取元数据信息调用的接口是如下接口 :
@Override
public List<SQLPrimaryKey> getPrimaryKeys(PrimaryKeysRequest primaryKeysRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
@Override
public List<SQLForeignKey> getForeignKeys(ForeignKeysRequest foreignKeysRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
@Override
public List<SQLUniqueConstraint> getUniqueConstraints(UniqueConstraintsRequest uniqueConstraintsRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
@Override
public List<SQLNotNullConstraint> getNotNullConstraints(NotNullConstraintsRequest notNullConstraintsRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
@Override
public List<SQLDefaultConstraint> getDefaultConstraints(DefaultConstraintsRequest defaultConstraintsRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
@Override
public List<SQLCheckConstraint> getCheckConstraints(CheckConstraintsRequest checkConstraintsRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
只分析SQLNotNullConstraint 的结构:
@Override
public List<SQLNotNullConstraint> getNotNullConstraints(NotNullConstraintsRequest notNullConstraintsRequest)
throws MetaException, NoSuchObjectException, TException {
return null;
}
SQLNotNullConstraint 结构如下 :
// 默认hive即可
private String catName; // required
// db
private String table_db; // required
// 表名
private String table_name; // required
// 列名
private String column_name; // required
// 约束名称,比如说 : ALTER TABLE t_user2 CHANGE COLUMN id id1 int CONSTRAINT id_not_null NOT NULL ENABLE;
private String nn_name; // required
// 是否开启not null的约束,开启为true,DISABLE NOVALIDATE 为false
private boolean enable_cstr; // required
// 这个字段当前只支持false NOVALIDATE,不支持VALIDATE
private boolean validate_cstr; // required
// 是否将约束用于查询优化
private boolean rely_cstr; // required
简单了解 :
主键约束:PRIMARY KEY
取值唯一性约束:UNIQUE
非空值约束:NOT NULL
默认值约束:DEFAULT [default_value]
自定义取值约束:CHECK [check_expression]
关闭约束(可以对约束列的数据进行修改):DISABLE NOVALIDATE
是否将约束用于查询优化:RELY/NORELY
测试结果 :
NOT NULL :
notNullConstraints : [SQLNotNullConstraint(catName:hive, table_db:default, table_name:constraints1, column_name:id1, nn_name:null, enable_cstr:true, validate_cstr:false, rely_cstr:false)]
NOT NULL ENABLE :
notNullConstraints : [SQLNotNullConstraint(catName:hive, table_db:default, table_name:constraints1, column_name:id1, nn_name:null, enable_cstr:true, validate_cstr:false, rely_cstr:false)]
DISABLE NOVALIDATE RELY :
notNullConstraints : [SQLNotNullConstraint(catName:hive, table_db:default, table_name:constraints1, column_name:id1, nn_name:null, enable_cstr:false, validate_cstr:false, rely_cstr:true)]
DISABLE NOVALIDATE NORELY :
notNullConstraints : [SQLNotNullConstraint(catName:hive, table_db:default, table_name:constraints1, column_name:id1, nn_name:null, enable_cstr:false, validate_cstr:false, rely_cstr:false)]
如感兴趣,点赞加关注,谢谢!!!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。