HIVE ddl - zhongjiajie/zhongjiajie.github.com GitHub Wiki

hive-ddl(Data Definition Language)

alter

  • Hive 删除所有的分区alter table table_name drop if exists partition (partition_dec<>'')
  • 删除一个分区一个范围的值ALTER TABLE myTable DROP PARTITION (date < 'date1'), PARTITION (date >'date2');
  • 重命名表: ALTER TABLE old_name RENAME TO new_name;
  • 表增加字段: ALTER TABLE invites ADD COLUMNS (new_col2 INT COMMENT 'a comment');
  • ALTER TABLE invites REPLACE COLUMNS (foo INT, bar STRING, baz INT COMMENT 'baz replaces new_col2');,REPLACE COLUMNS替换全部已存在字段且仅改变schema,不包括数据。所以replace columns也可用户删除字段

create table

  • 创建临时表create temporary table temp_table(……):Hive从0.14.0开始,注意点如下:
    • 如果创建的临时表表名已存在,那么当前session引用到该表名时实际用的是临时表,只有drop或rename临时表名才能使用原始表
    • 临时表限制:不支持分区字段和创建索引

TBLPROPERTIES

部分的TBLPROPERTIES默认就有的,例如last_modified_user以及last_modified_time

  • auto.purge: 可以提高insert overwrite的效率,但失败会丢失之前的数据默认是false,设置方法是TBLPROPERTIES ("auto.purge"="true") or ("auto.purge"="false"),在hive2.3以后insert overwrite如果设置了属性为true,源表的数据不会放进Trash文件夹,这个属性只对managed tables内部表生效,对外部表不生效.
  • serialization.null.format'='': 将格式为对应格式的值设置成空值, 由于是文件系统,可能从外面导数据进来,导入的数据可能对空值的描述都不一样,所以有空值格式描述,一般设置的值为'\N' 'null' 'NULL' ''serialization.null.format'=''仅对Text or SequenceFile文件类型有效这里
    • TBLPROPERTIES('serialization.null.format'='')的含义如下,:
      • An empty field in the data files will be treated as NULL when you query the table
      • When inserting rows to the table, NULL values will be written to the data files as empty fields
      • 如果插入的是空字符串,我的测试环境(Hive 1.2.1 and spark 1.6)和答主的结果不一样,我的会把空字符串视为NULL(在评论中可以看到)

MSCK

  • msck repair table partition <TABLE_NAME>可能会失败,参考可能是文件夹权限问题,此时可以单独运行alter table add partition <PARTITION>或者直接去文件目录查看文件夹权限,只有权限问题解决了,才能使用msck repair tble

Managed and External Tables(内部表和外部表)

By default Hive creates managed tables, where files, metadata and statistics are managed by internal Hive processes. For details on the differences between managed and external table see Managed vs. External Tables.

⚠️ **GitHub.com Fallback** ⚠️