1、创建数据库
hive (default)> create database if not exists test01comment 'this is test01' 备注location '/user/hive/test01.db'; 指定数据库存放位置
查看数据库存放位置
hive (default)> dfs -ls /user/hive/warehouse;
查看数据库信息desc database test01 ;desc database extended test01 ;describe database extended test01 ;
删除一个空数据库drop database databasename;如果数据库不为空,使用 cascade 强制删除drop database databasename cascade;
4、创建内部表
create table t1(id int,name string,hobby array<string>,addr map<string, string>)row format delimitedfields terminated by ";"collection items terminated by ","map keys terminated by ":";
显示表的定义,显示的信息多,格式友好
desc formatted t1;创建数据dat[root@linux101 data]# pwd
/opt/lagou/servers/hadoop-2.9.2/data[root@linux101 data]# vi t1.dat加载数据hive (test01)> load data local inpath '/opt/lagou/servers/hadoop-2.9.2/data/t1.dat' into table t1;查看数据文件dfs -cat /user/hive/warehouse/test01.db/t1/t1.dat;
dfs -ls /user/hive/warehouse/test01.db/t1;
删除表
drop table t1;
create external table t2(id int,name string,hobby array<string>,addr map<string, string>)row format delimitedfields terminated by ";"collection items terminated by ","map keys terminated by ":";
6、内部表转外部表
内部表转外部表alter table t1 set tblproperties('EXTERNAL'='TRUE');查询表信息,是否转换成功desc formatted t1;外部表转内部表。EXTERNAL 大写,false 不区分大小alter table t1 set tblproperties('EXTERNAL'='FALSE');查询表信息,是否转换成功desc formatted t1;
create table if not exists t3(id int,name string,hobby array<string>,addr map<String,string>)partitioned by (dt string)row format delimitedfields terminated by ';'collection items terminated by ','map keys terminated by ':';
加载数据
load data local inpath " /opt/lagou/servers/hadoop-2.9.2/data/t1.dat " into table t3partition(dt="2020-06-01");load data local inpath " /opt/lagou/servers/hadoop-2.9.2/data/t1.dat " into table t3partition(dt="2020-06-02");
查看分区
show partitions t3;
dfs -ls /user/hive/warehouse/test01.db/t3;
新增分区及数据
增加一个分区,不加载数据alter table t3 add partition(dt='2020-06-03');增加多个分区,不加载数据alter table t3add partition(dt='2020-06-05') partition(dt='2020-06-06');增加多个分区dfs -cp /user/hive/warehouse/test01.db/t3/dt=2020-06-01 /user/hive/warehouse/test01.db/t3/dt=2020-06-07;dfs -cp /user/hive/warehouse/test01.db/t3/dt=2020-06-01 /user/hive/warehouse/test01.db/t3/dt=2020-06-08;增加多个分区,加载数据alter table t3 add partition(dt='2020-06-07') location '/user/hive/warehouse/test01.db/t3/dt=2020-06-07';查看数据hive (test01)> dfs -ls /user/hive/warehouse/test01.db/t3;
8、创建分桶表
course.dat
1 java 90
1 c 78
1 python 91
1 hadoop 80
2 java 75
2 c 76
2 python 80
2 hadoop 93
3 java 98
3 c 74
3 python 89
3 hadoop 91
5 java 93
6 c 76
7 python 87
8 hadoop 88
创建分桶表
create table course(id int,name string,score int)clustered by (id) into 3 bucketsrow format delimited fields terminated by "\t";
创建普通表
create table course_common(id int,name string,score int)row format delimited fields terminated by "\t";
普通表加载数据
hive (test01)> load data local inpath '/opt/lagou/servers/hadoop-2.9.2/data/course.dat' into table course_common;
通过 insert ... select ... 给桶表加载数据
insert into table course select * from course_common;
查看桶表详情
desc formatted course;
查看桶表数据
dfs -ls /user/hive/warehouse/test01.db/course;
查看桶表具体数据
dfs -cat /user/hive/warehouse/test01.db/course/000000_0_copy_3;
9、修改表 & 删除表
修改表名。renamealter table course_common rename to course_common1;修改列名。change columnalter table course_common1 change column id cid int;修改字段类型。change columnalter table course_common1 change column cid cid string;修改字段数据类型时,要满足数据类型转换的要求。如int可以转为string,但是string不能转为int增加字段。add columnsalter table course_common1 add columns (common string);删除字段:replace columns这里仅仅只是在元数据中删除了字段,并没有改动hdfs上的数据文件alter table course_common1 replace columns(id string, cname string, score int);删除表drop table course_common1;