一、视图:

### --- 普通视图和物化视图

~~~ 普通视图:不保存数据,只是一层单纯的select查询映射,起着简化查询、明晰语义的作用。
~~~ 物化视图:保存数据,如果源表被写入新数据,物化视图也会同步更新。
~~~ POPULATE修饰符:决定在创建物化视图的过程中是否将源表的数据同步到物化视图里。
### --- 数据表的基本操作:

~~~ 只有MergeTree、Merge、Distribution这三类表引擎支持alter操作。
### --- 追加字段,两种方式:

alter table partition_v1 add column os String default 'mac';
alter table partition_v1 add column IP String after ID;
### --- 视图示例

~~~ # 查看表结构
hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID │ String │ │ │ │ │ │
│ URL │ String │ │ │ │ │ │
│ EventTime │ Date │ │ │ │ │ │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 追加字段

hadoop01 :) alter table partition_v1 add column os String default 'mac';

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID │ String │ │ │ │ │ │
│ URL │ String │ │ │ │ │ │
│ EventTime │ Date │ │ │ │ │ │
│ os │ String │ DEFAULT │ 'mac' │ │ │ │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 追加字段

hadoop01 :) alter table partition_v1 add column IP String after ID;

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID │ String │ │ │ │ │ │
│ IP │ String │ │ │ │ │ │
│ URL │ String │ │ │ │ │ │
│ EventTime │ Date │ │ │ │ │ │
│ os │ String │ DEFAULT │ 'mac' │ │ │ │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 修改数据类型:

~~~ alter...modify column...
~~~ 注意:类型需要相互兼容
~~~     # 修改数据类型

hadoop01 :) alter table partition_v1 modify column IP IPv4;

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID │ String │ │ │ │ │ │
│ IP │ IPv4 │ │ │ │ │ │
│ URL │ String │ │ │ │ │ │
│ EventTime │ Date │ │ │ │ │ │
│ os │ String │ DEFAULT │ 'mac' │ │ │ │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 修改备注:

alter...comment column ...
~~~     # 修改备注:主键ID

hadoop01 :) ALTER TABLE partition_v1 COMMENT COLUMN ID '主键ID';

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID │ String │ │ │ 主键ID │ │ │
│ IP │ IPv4 │ │ │ │ │ │
│ URL │ String │ │ │ │ │ │
│ EventTime │ Date │ │ │ │ │ │
│ os │ String │ DEFAULT │ 'mac' │ │ │ │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 删除已有字段:

~~~ 会把该字段下的数据一起删除
~~~     # 删除已有字段

hadoop01 :) alter table partition_v1 drop column URL;

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID │ String │ │ │ 主键ID │ │ │
│ IP │ IPv4 │ │ │ │ │ │
│ EventTime │ Date │ │ │ │ │ │
│ os │ String │ DEFAULT │ 'mac' │ │ │ │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

七、移动数据表

### --- 移动数据表

~~~ rename... to...
~~~ 注意:表的移动只能在单节点内完成
### --- 移动数据表示例

~~~ # 移动数据表
hadoop01 :) rename table default.partition_v1 to system.partition_v1;
~~~ # 查看移动的数据表
hadoop01 :) use system;
hadoop01 :) show tables;
~~~输出参数
partition_v1











Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart

                                                                                                                                                   ——W.S.Landor