1. 配置
-
初始化默認(rèn)的 derby 數(shù)據(jù)庫,作為 Hive 的元數(shù)據(jù)庫(MetaStore)
bin/schematool -dbType derby -initSchema
注意:初始化 Derby 作為元數(shù)據(jù)庫時橡卤,會在當(dāng)前 shell 路徑下生成 metastore_db/
目錄箍铲。如在別處啟動 Hive藻三,而此處無 metastore_db/
目錄時苗踪,將無法啟動 Hive潜叛,需要再在此路徑下秽褒,重新初始化壶硅,生成該目錄秦士。
-
配置 MySQL 作為 Hive 的元數(shù)據(jù)庫
-
在 conf 目錄下創(chuàng)建 hive-site.xml编矾,配置如下
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://hm02:3306/hive?createDatabaseIfNotExist=true</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>123</value> </property> </configuration>
將
com.mysql.jdbc.Driver
復(fù)制到lib/
目錄下-
初始化 MySQL 的 MetaStore
bin/schematool -dbType mysql -initSchema
-
注意 MySQL 需要授權(quán)遠(yuǎn)程登錄
2. 使用
-
啟動 Hive
bin/hive
-
DQL
--查看數(shù)據(jù)庫 show databases; --查看當(dāng)前數(shù)據(jù)庫的表信息 (Derby 默認(rèn)使用 default 作為數(shù)據(jù)庫) show tables; --查看表結(jié)構(gòu) show create table student; --啟用 MapReduce 進(jìn)行查詢 select * from student order by id desc; select count(1) from student;
-
DDL
--創(chuàng)建表 create table student(id int, name string); --創(chuàng)建表 (指定分隔符) create table student(id int, name string) row format delimited fields terminated by '\t';
-
DML
--Hive 獨有語法,將指定路徑的文件 導(dǎo)入至數(shù)據(jù)倉庫 ----如未指定分隔符央串,則導(dǎo)入的數(shù)據(jù)無法被解析蚂踊,全部為 NULL load data local inpath '/opt/stu.txt' into table student;
3. 說明
Derby 數(shù)據(jù)庫 為只能允許一個會話連接的文件數(shù)據(jù)庫约谈,因此需要修改為 MySQL 數(shù)據(jù)庫,從而支持多用戶會話犁钟。
MySQL 作為元數(shù)據(jù)庫時的表信息:
- TBLS
- 表信息
- COLUMNS_V2
- 列信息
- SDS
- 存放在 HDFS 中的位置信息棱诱、輸入輸出格式等
- PARTITION_*
- 分區(qū)表
4. 進(jìn)階
1. 內(nèi)部表、外部表
- 內(nèi)部表
- 由 Hive 管理涝动,文件保存在 HDFS 的 Hive 目錄內(nèi)
- 先有表迈勋,后有數(shù)據(jù)
- 刪除表后,刪除元數(shù)據(jù)和存儲的數(shù)據(jù)
- 外部表(加
external
修飾醋粟,指定location
)- 由用戶自行管理靡菇,文件需要指定保存的 HDFS 路徑
- 先有數(shù)據(jù),后建表
- 刪除表后米愿,僅刪除元數(shù)據(jù)厦凤,存儲數(shù)據(jù)不會被刪除
- 建表后,自動從指定 HDFS 導(dǎo)入元數(shù)據(jù)
-
準(zhǔn)備數(shù)據(jù)育苟,保存到
/opt/person.txt
1,xiaoming,book-TV-code,beijing:chaoyang 2,lilei,book-code,nanjing:yuhua 3,lihua,music-book,heilongjiang:haerbin
-
創(chuàng)建內(nèi)部表
--單元格分隔:row format delimited fields terminated by ',' --集合分隔:collection items terminated by '-' --Map分隔:map keys terminated by ':' create table person1 ( id int, name string, hobby array<string>, addr map<string,string> ) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':'; --導(dǎo)入 load data local inpath '/opt/person.txt' into table person1;
-
創(chuàng)建外部表
--外部表:多一個 external 修飾较鼓;需要指定 location create external table person2 ( id int, name string, hobby array<string>, addr map<string,string> ) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':' location '/user/person2'; --導(dǎo)入 load data local inpath '/opt/person.txt' into table person2;
-
此時查看 Hive 元數(shù)據(jù)庫的 TBLS
- TBL_TYPE 列中,內(nèi)部表為
MANAGED_TABLE
违柏,外部表為EXTERNAL_TABLE
- TBL_TYPE 列中,內(nèi)部表為
-
查看詳細(xì)的表信息(可以看到 HDFS 保存路徑的變化)
desc formatted person1
2. 分區(qū)
-
創(chuàng)建內(nèi)部表時博烂,定義分區(qū)
create table person3 ( id int, name string, hobby array<string>, addr map<string,string> ) partitioned by (p_dt string) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':';
-
導(dǎo)入時,指定分區(qū)
load data local inpath '/opt/person.txt' into table person3 partition(p_dt='201907');
-
查看指定表的所有分區(qū)
show partitions person3;
-
添加一個分區(qū)
alter table person3 add partition(p_dt='201908')