疑难 :
hive not null,是不是感觉not null是基于column的,所以就感觉not null是column中的一个字段,道歉,还真不是,口说无凭,上代码看一下
表格信息:
// 表private String tableName; // required// dbprivate String dbName; // required// 表的所属,基于用于、者角色或者组private String owner; // required// 表创立事件private int createTime; // required// 最初拜访工夫private int lastAccessTime; // required// 保留字段,默认0private 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; // requiredprivate 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// 默认是hiveprivate String catName; // optional// 用户、角色或者所属组private PrincipalType ownerType; // optional
FieldSchema列信息:
// 列名称private String name; // required// 列类型private String type; // required// 形容private String comment; // required
是不是列中没有not null信息
设计
创立表的时候,如果配置了主键、外键、惟一索引、not null、默认值和自定义取值束缚,都会走如下接口:
@Overridepublic 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@Overridepublic void addNotNullConstraint(List<SQLNotNullConstraint> notNullConstraints) throws MetaException, NoSuchObjectException, TException { LOG.info("edap addNotNullConstraint : notNullConstraints : {}", notNullConstraints);}@Overridepublic 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 的构造:
@Overridepublic List<SQLNotNullConstraint> getNotNullConstraints(NotNullConstraintsRequest notNullConstraintsRequest) throws MetaException, NoSuchObjectException, TException { return null;}SQLNotNullConstraint 构造如下 :// 默认hive即可 private String catName; // required// dbprivate 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 为falseprivate boolean enable_cstr; // required// 这个字段以后只反对false NOVALIDATE,不反对VALIDATEprivate 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)]