[一起學(xué)Hive]之三–Hive中的數(shù)據(jù)庫(Database)和表(Table)

前面的文章中,介紹了可以把Hive當(dāng)成一個(gè)“數(shù)據(jù)庫”,它也具備傳統(tǒng)數(shù)據(jù)庫的數(shù)據(jù)單元,數(shù)據(jù)庫(Database/Schema)和表(Table)。

本文介紹一下Hive中的數(shù)據(jù)庫(Database/Schema)和表(Table)的基礎(chǔ)知識(shí),由于篇幅原因,這里只是一些常用的、基礎(chǔ)的。

二、Hive的數(shù)據(jù)庫和表

先看一張草圖:

Hive結(jié)構(gòu)

Hive結(jié)構(gòu)

從圖上可以看出,Hive作為一個(gè)“數(shù)據(jù)庫”,在結(jié)構(gòu)上積極向傳統(tǒng)數(shù)據(jù)庫看齊,也分?jǐn)?shù)據(jù)庫(Schema),每個(gè)數(shù)據(jù)庫下面有各自的表組成。

1. Hive在HDFS上的默認(rèn)存儲(chǔ)路徑

Hive的數(shù)據(jù)都是存儲(chǔ)在HDFS上的,默認(rèn)有一個(gè)根目錄,在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)有一個(gè)default數(shù)據(jù)庫,進(jìn)入Hive-Cli之后,即到default數(shù)據(jù)庫下。
  • 使用use databasename;可以切換到某個(gè)數(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;">

  1. hive> show databases;
  2. OK
  3. default
  4. lxw1234
  5. usergroup_mdmp
  6. userservice_mdmp
  7. Time taken: 0.442 seconds, Fetched: 4 row(s)
  8. hive> use lxw1234;
  9. OK
  10. Time taken: 0.023 seconds
  11. hive>

</pre>

  • Hive****中的數(shù)據(jù)庫在HDFS****上的存儲(chǔ)路徑為

${hive.metastore.warehouse.dir}/databasename.db

比如,名為lxw1234的數(shù)據(jù)庫存儲(chǔ)路徑為:

/user/hive/warehouse/lxw1234.db

  • 創(chuàng)建Hive****數(shù)據(jù)庫

使用HDFS超級(jí)用戶,進(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;">

  1. CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
  2. [COMMENT database_comment]
  3. [LOCATION hdfs_path]
  4. [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;">

  1. CREATE DATABASE IF NOT EXISTS lxw1234
  2. COMMENT 'lxw的大數(shù)據(jù)田地-lxw1234.com'
  3. localtion 'hdfs://namenode/user/lxw1234/lxw1234.db/';

</pre>

創(chuàng)建時(shí)候可以指定數(shù)據(jù)庫在HDFS上的存儲(chǔ)位置。

注意:使用HDFS超級(jí)用戶創(chuàng)建數(shù)據(jù)庫后,該數(shù)據(jù)庫在HDFS上的存儲(chǔ)路徑的屬主為超級(jí)用戶,如果該數(shù)據(jù)庫是為某個(gè)或者某些用戶使用的,則需要修改路徑屬主,或者在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不允許刪除一個(gè)里面有表存在的數(shù)據(jù)庫,如果想刪除數(shù)據(jù)庫,要么先將數(shù)據(jù)庫中的表全部刪除,要么可以使用CASCADE關(guān)鍵字,使用該關(guān)鍵字后,Hive會(huì)自己將數(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;">

  1. hive> show tables;
  2. OK
  3. lxw1
  4. lxw1234
  5. table1
  6. t_site_log

</pre>

3.2 表的存儲(chǔ)路徑

默認(rèn)情況下,表的存儲(chǔ)路徑為:

${hive.metastore.warehouse.dir}/databasename.db/tablename/

可以使用desc formatted tablename;命令查看表的詳細(xì)信息,其中包括了存儲(chǔ)路徑:

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時(shí)候會(huì)刪除HDFS上的數(shù)據(jù);

外部表DROP時(shí)候不會(huì)刪除HDFS上的數(shù)據(jù);

  • 內(nèi)部表適用場(chǎng)景:

Hive中間表、結(jié)果表、一般不需要從外部(如本地文件、HDFS上load數(shù)據(jù))的情況。

  • 外部表適用場(chǎng)景:

源表,需要定期將外部數(shù)據(jù)映射到表中。

  • 我們的使用場(chǎng)景:

每天將收集到的網(wǎng)站日志定期流入HDFS文本文件,一天一個(gè)目錄;

在Hive中建立外部表作為源表,通過添加分區(qū)的方式,將每天HDFS上的原始日志映射到外部表的天分區(qū)中;

在外部表(原始日志表)的基礎(chǔ)上做大量的統(tǒng)計(jì)分析,用到的中間表、結(jié)果表使用內(nèi)部表存儲(chǔ),數(shù)據(jù)通過SELECT+INSERT進(jìn)入內(nèi)部表。

3.4 創(chuàng)建表

創(chuàng)建表的語法選項(xiàng)特別多,這里只列出常用的選項(xiàng)。

其他請(qǐng)參見Hive官方文檔:

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable

以一個(gè)例子來說吧:

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 ‘,’ //指定每行中字段分隔符為逗號(hào)

LINES TERMINATED BY ‘\n’ //指定行分隔符

COLLECTION ITEMS TERMINATED BY ‘,’ //指定集合中元素之間的分隔符

MAP KEYS TERMINATED BY ‘:’ //指定數(shù)據(jù)中Map類型的Key與Value之間的分隔符

舉個(gè)例子:

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上的文件存儲(chǔ)格式,可選的文件存儲(chǔ)格式有:

TEXTFILE //文本,默認(rèn)值

SEQUENCEFILE // 二進(jìn)制序列文件

RCFILE //列式存儲(chǔ)格式文件 Hive0.6以后開始支持

ORC //列式存儲(chǔ)格式文件,比RCFILE有更高的壓縮比和讀寫效率,Hive0.11以后開始支持

PARQUET //列出存儲(chǔ)格式文件,Hive0.13以后開始支持

  • 關(guān)鍵詞LOCATION

指定表在HDFS上的存儲(chǔ)位置。

Hive相關(guān)文章(持續(xù)更新)

一起學(xué)Hive系列

Hive分析函數(shù)系列

Hive索引

hive優(yōu)化之——控制hive任務(wù)中的map數(shù)和reduce數(shù)

如果覺得本博客對(duì)您有幫助,請(qǐng) 贊助作者 。

轉(zhuǎn)載請(qǐng)注明:lxw的大數(shù)據(jù)田地 ? [一起學(xué)Hive]之三–Hive中的數(shù)據(jù)庫(Database)和表(Table)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容