第二章
4. 内部表与外部表:
外部表创建:
create external table peson{
id int,
name string,
age uint
};
外部表删除:
drop table person;
删除后:内部表的元数据和表数据都会删掉
外部表仅删除了元数据,表的数据还保存在目录下
外部表的使用场景:
原始日志文件或同时被多个部门同时操作的数据集,就需要使用到外部表
如果不小心将meta data表删除,HDFS上的data还可以恢复,增加了数据的安全性。
内部表创建:
create table person2{
id int,
name string,
age uint
};
insert数据后产生临时表
使用insert会产生临时表,重新连接后临时表会消失,所以,如果要插入大批量的数据,不那使用用insert。
Hive数据库路径下有多个文件: /apps/hive/warehouse
5.查询和加载表
查询建表(有数据)
create table preson_2
as
select *from person_1;
查询建表(不带数据)
create table person3 like person_1;
加载数据到表
create table if not exists person5(
id int comment 'ID number',
name string comment 'people name',
age int comment 'people age'
)
comment 'write by leo'
row format delimited --指定序列化的原则:
fields terminated by ',' --字段用逗号分隔
lines terminated by '\n' --每条数据行分隔
---file format (HDFS文件存放的格式,默认TEXTFILE,即文本格式,可以直接打开)
---location "input/wc"指定表在HDFS上的位置
load data local inpath'/opt/person5'into table person5;
------expode 将数组拆分成行,lateral view 来实现聚合
select hobby,count(*) as c1 from s1 lateral view explode(likes) likes as hobby
group by hobby order by c1 desc;
select id,name,hobby from s1 lateral vies explode(likes) likes as hobby;
Hive特殊数据类型
Map是一组键值对元组集合,使用数组表示,可以访问元素
eg:map01('first','max','second','demo')
map01['first']-->'max'
STRUCT可以通过“点”符号访问元素内容。