hive修改列名(原)


CREATE   TABLE `detail_flow_test`(
  `union_id` string COMMENT '设备唯一标识'
  ) COMMENT '站边表' 
PARTITIONED BY ( 
  `partition_date`  string )
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
  LINES TERMINATED BY '\n' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://qunarcluster/user/traindev/tmp/detail_flow_test'

(2)新增字段:use mart_flow_test;
    alter table detail_flow_test add columns(original_union_id string);
 
 或者:alter table 表名 add columns (列名 STRING COMMENT 'Department name');
 alter table detail_flow_test add columns (original_old string Comment '哈哈')


(3)修改注释:use original_union_id;

 alter table detail_flow_test change column original_union_id original_union_id string COMMENT  '原始设备唯一性标识';

(4)Hive 添加表字段,给字段排序
 ALTER TABLE 表名 CHANGE column col_old_name col_new_name column_type [COMMENT col_comment] after 列名(放在哪个字段后);

 实例:把original_union_id 放到 original_old 后面
 ALTER TABLE detail_flow_test CHANGE column original_union_id original_union_id string  after original_old


 
 (5) 重命名分区
 
ALTER TABLE table_name PARTITION partition_spec RENAME TO PARTITION partition_spec;  --这个是重命名dt= 后面的内容
alter table detail_flow_test partition partition_date  rename to partition  dt ;  ---错误

alter table detail_flow_test CHANGE partition_date dt string


二、问题:  分区字段的列名无法修改?

    因为分区字段涉及到了HDFS的路径问题,所以不能像普通字段那样直接修改注释,我推荐你使用修改hive元数据的方式,以MYSQL为例:
UPDATE PARTITION_KEYS ps 
join TBLS ts ON ps.TBL_ID = ts.TBL_ID 
join DBS ds on ts.DB_ID = ds.DB_ID 
set ps.PKEY_COMMENT = '注释' 
 WHERE ds.NAME = '库名' and ts.TBL_NAME = '表名' AND ps.PKEY_NAME = '分区字段名';

-----测试 列名交换先后顺序,数据是否也跟着改变先后顺序
 CREATE TABLE `detail_flow_test`(
  `id` string COMMENT '设备唯一标识', 
  `original_old` string COMMENT '哈哈', 
  `original_union_id` string COMMENT '原始设备唯一性标识')
COMMENT '站边表'
PARTITIONED BY ( 
  `dt` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
  LINES TERMINATED BY '\n' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://qunarcluster/user/traindev/tmp/detail_flow_test'


  insert overwrite  table detail_flow_test partition  (dt) select id,order_no,package_product_id,dt from default.package_deliver where dt='2018-01-01' limit 10;

-----改变列的位置
 ALTER TABLE detail_flow_test CHANGE column original_old original_old string  after original_union_id

观察结果发现:并不能改变数据的列顺序。所以修改类的顺序是不能改变数据的位置。

注意:由于hive文件并没有修改,只是相当于修改了字段名字而已,数据并没有相应的移动。

因此,此方法适用于已建表,后续会重新刷新数据的情况;或者空表。

猜你喜欢

转载自blog.csdn.net/m0_38102941/article/details/84995528