在前面的文章中憨降,介紹了可以把Hive當(dāng)成一個“數(shù)據(jù)庫”,它也具備傳統(tǒng)數(shù)據(jù)庫的數(shù)據(jù)單元,數(shù)據(jù)庫(Database/Schema)和表(Table)。
本文介紹一下Hive中的數(shù)據(jù)庫(Database/Schema)和表(Table)的基礎(chǔ)知識毫缆,由于篇幅原因,這里只是一些常用的乐导、基礎(chǔ)的苦丁。
二、Hive的數(shù)據(jù)庫和表
先看一張草圖:
Hive結(jié)構(gòu)
從圖上可以看出物臂,Hive作為一個“數(shù)據(jù)庫”旺拉,在結(jié)構(gòu)上積極向傳統(tǒng)數(shù)據(jù)庫看齊,也分?jǐn)?shù)據(jù)庫(Schema)棵磷,每個數(shù)據(jù)庫下面有各自的表組成蛾狗。
1. Hive在HDFS上的默認(rèn)存儲路徑
Hive的數(shù)據(jù)都是存儲在HDFS上的,默認(rèn)有一個根目錄仪媒,在hive-site.xml中淘太,由參數(shù)hive.metastore.warehouse.dir指定。默認(rèn)值為/user/hive/warehouse.
2. Hive中的數(shù)據(jù)庫(Database)
- 進(jìn)入Hive命令行规丽,執(zhí)行show databases;命令,可以列出hive中的所有數(shù)據(jù)庫撇贺,默認(rèn)有一個default數(shù)據(jù)庫赌莺,進(jìn)入Hive-Cli之后,即到default數(shù)據(jù)庫下松嘶。
- 使用use databasename;可以切換到某個數(shù)據(jù)庫下艘狭,同mysql;
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
- hive> show databases;
- OK
- default
- lxw1234
- usergroup_mdmp
- userservice_mdmp
- Time taken: 0.442 seconds, Fetched: 4 row(s)
- hive> use lxw1234;
- OK
- Time taken: 0.023 seconds
- hive>
</pre>
- Hive****中的數(shù)據(jù)庫在HDFS****上的存儲路徑為:
${hive.metastore.warehouse.dir}/databasename.db
比如,名為lxw1234的數(shù)據(jù)庫存儲路徑為:
/user/hive/warehouse/lxw1234.db
- 創(chuàng)建Hive****數(shù)據(jù)庫
使用HDFS超級用戶巢音,進(jìn)入Hive-Cli遵倦,語法為:
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
- CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
- [COMMENT database_comment]
- [LOCATION hdfs_path]
- [WITH DBPROPERTIES (property_name=property_value, ...)];
</pre>
比如,創(chuàng)建名為lxw1234的數(shù)據(jù)庫:
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
- CREATE DATABASE IF NOT EXISTS lxw1234
- COMMENT 'lxw的大數(shù)據(jù)田地-lxw1234.com'
- localtion 'hdfs://namenode/user/lxw1234/lxw1234.db/';
</pre>
創(chuàng)建時候可以指定數(shù)據(jù)庫在HDFS上的存儲位置官撼。
注意:使用HDFS超級用戶創(chuàng)建數(shù)據(jù)庫后梧躺,該數(shù)據(jù)庫在HDFS上的存儲路徑的屬主為超級用戶,如果該數(shù)據(jù)庫是為某個或者某些用戶使用的傲绣,則需要修改路徑屬主掠哥,或者在Hive中進(jìn)行授權(quán)。
- 修改數(shù)據(jù)庫
修改數(shù)據(jù)庫屬性:
ALTER (DATABASE|SCHEMA) database_name
SET DBPROPERTIES (property_name=property_value, …);
修改數(shù)據(jù)庫屬主:
ALTER (DATABASE|SCHEMA) database_name
SET OWNER [USER|ROLE] user_or_role;
- 刪除數(shù)據(jù)庫
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name
[RESTRICT|CASCADE];
默認(rèn)情況下秃诵,Hive不允許刪除一個里面有表存在的數(shù)據(jù)庫续搀,如果想刪除數(shù)據(jù)庫,要么先將數(shù)據(jù)庫中的表全部刪除菠净,要么可以使用CASCADE關(guān)鍵字禁舷,使用該關(guān)鍵字后,Hive會自己將數(shù)據(jù)庫下的表全部刪除毅往。RESTRICT關(guān)鍵字就是默認(rèn)情況牵咙,即如果有表存在,則不允許刪除數(shù)據(jù)庫煞抬。
3. Hive中的表(Table)
3.1 查看所有的表
進(jìn)入Hive-Cli霜大,使用use databasename;切換到數(shù)據(jù)庫之后,執(zhí)行show tables; 即可查看該數(shù)據(jù)庫下所有的表:
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
- hive> show tables;
- OK
- lxw1
- lxw1234
- table1
- t_site_log
</pre>
3.2 表的存儲路徑
默認(rèn)情況下革答,表的存儲路徑為:
${hive.metastore.warehouse.dir}/databasename.db/tablename/
可以使用desc formatted tablename;命令查看表的詳細(xì)信息战坤,其中包括了存儲路徑:
Location: hdfs://cdh5/hivedata/warehouse/lxw1234.db/lxw1234
3.3 內(nèi)部表和外部表
Hive中的表分為內(nèi)部表(MANAGED_TABLE)和外部表(EXTERNAL_TABLE)。
- 內(nèi)部表和外部表最大的區(qū)別
內(nèi)部表DROP時候會刪除HDFS上的數(shù)據(jù);
外部表DROP時候不會刪除HDFS上的數(shù)據(jù);
- 內(nèi)部表適用場景:
Hive中間表残拐、結(jié)果表途茫、一般不需要從外部(如本地文件、HDFS上load數(shù)據(jù))的情況溪食。
- 外部表適用場景:
源表囊卜,需要定期將外部數(shù)據(jù)映射到表中。
- 我們的使用場景:
每天將收集到的網(wǎng)站日志定期流入HDFS文本文件错沃,一天一個目錄栅组;
在Hive中建立外部表作為源表,通過添加分區(qū)的方式枢析,將每天HDFS上的原始日志映射到外部表的天分區(qū)中玉掸;
在外部表(原始日志表)的基礎(chǔ)上做大量的統(tǒng)計分析,用到的中間表醒叁、結(jié)果表使用內(nèi)部表存儲司浪,數(shù)據(jù)通過SELECT+INSERT進(jìn)入內(nèi)部表泊业。
3.4 創(chuàng)建表
創(chuàng)建表的語法選項特別多,這里只列出常用的選項啊易。
其他請參見Hive官方文檔:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable
以一個例子來說吧:
CREATE EXTERNAL TABLE t_lxw1234 (
id INT,
ip STRING COMMENT ‘訪問者IP’,
avg_view_depth DECIMAL(5,1),
bounce_rate DECIMAL(6,5)
) COMMENT ‘lxw的大數(shù)據(jù)田地-lxw1234.com’
PARTITIONED BY (day STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘,’
STORED AS textfile
LOCATION ‘hdfs://cdh5/tmp/lxw1234/';
- 關(guān)鍵字EXTERNAL:
表示該表為外部表吁伺,如果不指定EXTERNAL關(guān)鍵字,則表示內(nèi)部表
- 關(guān)鍵字COMMENT
為表和列添加注釋
- 關(guān)鍵字PARTITIONED BY
表示該表為分區(qū)表租谈,分區(qū)字段為day,類型為string
- 關(guān)鍵字ROW FORMAT DELIMITED
指定表的分隔符篮奄,通常后面要與以下關(guān)鍵字連用:
FIELDS TERMINATED BY ‘,’ //指定每行中字段分隔符為逗號
LINES TERMINATED BY ‘\n’ //指定行分隔符
COLLECTION ITEMS TERMINATED BY ‘,’ //指定集合中元素之間的分隔符
MAP KEYS TERMINATED BY ‘:’ //指定數(shù)據(jù)中Map類型的Key與Value之間的分隔符
舉個例子:
create table score(name string, score map<string,int>)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘\t’
COLLECTION ITEMS TERMINATED BY ‘,’
MAP KEYS TERMINATED BY ‘:';
要加載的文本數(shù)據(jù)為:
biansutao ‘?dāng)?shù)學(xué)':80,’語文':89,’英語':95
jobs ‘語文':60,’數(shù)學(xué)':80,’英語':99
- 關(guān)鍵字STORED AS
指定表在HDFS上的文件存儲格式,可選的文件存儲格式有:
TEXTFILE //文本垦垂,默認(rèn)值
SEQUENCEFILE // 二進(jìn)制序列文件
RCFILE //列式存儲格式文件 Hive0.6以后開始支持
ORC //列式存儲格式文件宦搬,比RCFILE有更高的壓縮比和讀寫效率,Hive0.11以后開始支持
PARQUET //列出存儲格式文件劫拗,Hive0.13以后開始支持
- 關(guān)鍵詞LOCATION
指定表在HDFS上的存儲位置间校。
Hive相關(guān)文章(持續(xù)更新):
hive優(yōu)化之——控制hive任務(wù)中的map數(shù)和reduce數(shù)
如果覺得本博客對您有幫助,請 贊助作者 页慷。
轉(zhuǎn)載請注明:lxw的大數(shù)據(jù)田地 ? [一起學(xué)Hive]之三–Hive中的數(shù)據(jù)庫(Database)和表(Table)