Hive基本操作命令

Hive DDL 操作

创建表

1. CREATE table

2. CREAT Table AS

拷贝表结构的同时会,拷贝表中的数据。

3. CREATE TABLE table_name LIKE table_name_other;

只拷贝表结构,并不会拷贝数据。

修改表

1. 修改表名称

alter table t1 rename to t2

2. 修改表属性

alter table t2 set tblproperties('comment'='table comment');

3. 添加序列化属性

alter table t2 set serdeproperties('field.delim'=',')

修改列

1. 修改列名,类型,位置,注释

alter table t1 change a a1 string after b;

after b表示修改的列放到b的后面。

2. 添加,替换列

alter table t1 add colums(a int);

alter table t1 replace  colums(a int , b tint);

修改分区

1. 添加分区

alter table t1 add partition(dt=2018-12-12', country='cn') location '/user/dfs/data/hive/t1';

location必须是一个目录。

2. 修改分区

alter table t1 add partition(dt=2018-12-12', country='cn') set location '/user/dfs/data/hive/t2';

3. 重命名分区

alter table t1 add partition(dt=2018-12-12', country='cn') rename to partition(dt=2017-12-12', country='cn')

4. 修复分区

msck repair table t1

在hdfs上已经存在分区数据,在元数据中没有对应的分区信息。此时需要执行修复分区,将分区数据对应的元数据添加到元数据库中,以便在后续的查询中能够查找到对应的分区数据。

5. 清空表,清空分区

truncate table t1 partition(dt=2018-12-12', country='cn')

不能清空外部表的分区。

6. 删除分区

drop table t1

alter table t1 drop partition(dt=2018-12-12', country='cn')

动态分区

1. 动态分区是指分区字段不是固定的,可以动态指定。

2. 创建动态分区表

create table t1(c1 string, c2 int, c3 string, c4 string)  partitioned by(year string, month string) 

3. 加载数据

insert into table t1 partion(year, month) select c1, c2, c3, c4, substr(c1,0,4), substr(c1, 6, 2) from t2

只有在非严格模式下,才能使用动态分区加载数据。

Hive DML 操作

加载本地数据:load data local inpath '/path/file.txt' into table table_name

加载HDFS数据:load data inpath '/hdfs/path/file.txt' into table table_name

覆盖数据:load data [local] inpath '/path/file.txt' overwrite into table table_name

插入数据

单表插入:insert into table t1 select * from t2

多表插入:

SQL方式:insert into table t1 values('col1', 'col2');

输出数据

单输出:insert overwrite [local] directory dir1 select * from t1

多输出:from t1

insert overwrite local directory dir1 select *

insert overwrite local directory dir2 select *

导入导出数据

导出数据:export table t1 to '/opt/path'

导入数据:import from '/opt/path'

猜你喜欢

转载自blog.csdn.net/liaomingwu/article/details/85522189