1.Hive的直连数据库(mysql)模式
最常用的模式
2.Hive数据类型(data_type)
**基本类型(primitive type):**与mysql相同
array_type: Array<data_type>
map_type: MAP < primitive_type, data_type >
**struct_type:**STRUCT < col_name : data_type [COMMENT col_comment], …>
3.内部表和外部表
内部表:CREATE TABLE [IF NOT EXISTS] 表名
对于内部表,元数据与表中的数据都会被删除
外部表:CREATE EXTERNAL TABLE [IF NOT EXISTS] 表名 LOCATION hdfs路径
对于外部表,删除表时只会删除元数据,不会删除hdfs 的数据
4.Hive建表
create table person(
id int comment ‘主键’,
name string comment ‘名字’,
interest array comment ‘爱好’
)
row format delimited
fields terminated by “,” //字段之间
collections terminated by “-” //集合元素之间
map keys terminated by “:”
lines terminated by “\n”; //行与行之间
5.表分区
单分区:create table day_table (id int, content string) partitioned by (day string);
按天分区,在表结构中存在int,content,day三列
双分区:create table day_hour_table (id int, content string) partitioned by (day string, hour string);
按天和小时分区,在表结构中新增加了 day和hour两列
6.删除数据
truncate from person where id=1
无法使用delete 删除数据
7.添加数据
(1)加载文件load
load:(可以copy/move)
LOAD DATA INPATH 文件路径 INTO TABLE 表名
copy:load的是本地的数据
move:load的是hdfs上的文件,不适用local
(2)insert
适用于将大表中的数据拆分到小表中
insert overwrite 表示覆盖
insert into 表示追加
create table person2_1(
id int,
name string
);
create table person2_2(
id int,
likes array
);
from person
insert overwrite table person2_1
select id,name
insert into person2_2
select id,likes;
person表中一些列的数据添加到person2_1中,将另外一些列的数据添加到person2_2中。