1.建库
-
hive中有一个默认的库:
库名: default
库目录:你的hdfs地址/user/hive/warehouse -
新建库:
create database db_order;
库建好后,在hdfs中会生成一个库目录:
你的hdfs地址/user/hive/warehouse/db_order.db
2.建表
-
2.1基本建表语句
create table t_order(id string,create_time string,amount float,uid string);
表建好后,会在所属的库目录中生成一个表目录
/user/hive/warehouse/db_order.db/t_order
只是,这样建表的话,hive会认为表数据文件中的字段分隔符为 ^A(\001)- 正确的建表语句为:
create table t_order(id string,create_time string,amount float,uid string)
row format delimited
fields terminated by ',';
这样就指定了,我们的表数据文件中的字段分隔符为 “,”
-
2.2内部表与外部表
-
内部表(MANAGED_TABLE): 表目录按照hive的规范来部署,位于hive的仓库目录/user/hive/warehouse中
-
外部表(EXTERNAL_TABLE): 表目录由建表用户自己指定
external 外部表
location ‘/access/log’ 指定外部表位置
createexternaltable t_access(ip string,url string,access_time string)
row format delimited
fields terminated by ','
location'/access/log';
- 外部表和内部表的特性差别:
1.内部表的目录在hive的仓库目录中 VS 外部表的目录由用户指定
2.drop一个内部表时:hive会清除相关元数据,并删除表数据目录
3.drop一个外部表时:hive只会清除相关元数据;
-
-
2.3.分区表
- 创建分区表
create table t_access(ip string,url string,access_time string)
partitioned by(dt string)
row format delimited
fields terminated by ','; - 向分区表导入数据
load data local inpath '/root/access.log.2017-08-04.log' into table t_access
partition(dt=‘20190527’);
load data local inpath '/root/access.log.2017-08-05.log' into table t_access
partition(dt=‘20190528’);
- 创建分区表
3.删除表
drop table t_order;
删除表的效果是:
hive会从元数据库中清除关于这个表的信息;
hive还会从hdfs中删除这个表的表目录;
本文详细介绍了Hive的建库和建表操作,包括默认库default的目录,新建库时HDFS上的对应目录,以及如何指定字段分隔符。此外,还讲解了内部表与外部表的区别,如存储位置、drop操作的影响。最后提到了分区表的创建和数据导入,以及删除表的后果,即元数据和表目录的清除。
1047

被折叠的 条评论
为什么被折叠?



