Hive資料定義

2021-10-02 15:44:27 字數 2685 閱讀 5542

更多資訊

附書**github工程:

hiveql是hive查詢語言,作為ansi sql的一種方言。但是其還是有和關聯式資料庫支援的sql中有本質的差異。如:hive不支援事務,且預設情況下不支援行級別插入,更新,刪除操作。

雖然可以在hiveql中使用關聯子句,但hiveql中並沒有主外來鍵概念,也就沒有強制的約束控制,這樣資料的儲存可以寬泛很多。

hive中的資料庫概念本質上僅僅是目錄或者命名空間,然而使用者來說,這樣可以避免表命名衝突。通常使用資料庫將生產表組織成邏輯組。

資料庫在hive中有乙個預設的庫 default。

hive 為每個資料庫建立乙個目錄。資料庫中的表將會以這個資料庫目錄的子目錄形式存在。

建立建立庫:

create database if not exists db_name;

獲取資料庫列表 :

show databases;

show databases like 'h.*'

檢視資料庫的詳細資訊:

describe database db_name;

describe database extended db_name;

刪除資料庫:

drop database if exists db_name;

修改資料庫,可以用來對資料庫的dbproperties設定鍵值對屬性,資料庫的元資料資訊不可以修改:

alter database db_name set dbproperties('a'='b');

表 create table語句遵守sql語法慣例。但hive中具有擴充套件性,有更廣泛的靈活性。如可以定標資料檔案儲存在什麼位置,使用什麼樣的儲存格式。
建立表的3中方法

直接建立:

create table if not exists table_name (col_name data_type);

查詢建表:

create table if not exists table_name

select * from table_name_2 ;

like建表:(複製了表結構)

create table table_name like table_name_2 ;

location 子句指定表資料檔案的儲存路徑,預設情況下是預設的資料倉儲的路徑,也可以指定不同的路徑,針對這種情況多是針對外部表的設定。

檢視表資訊:

describe(desc) table_name ;

describe(desc) extended table_name[.col_name] ; -- 查詢更加資訊的資訊

describe(desc) formatted table_name[.col_name] ; -- 更多的資訊,且資訊的可讀性更強

內部表(管理表)和外部表:

內部表: 內部表刪除後,表元資料和表資料都被刪除。對於內部表不方便與其他工作共享資料,只屬於hive,有完整的許可權進行操作。

建立表使用的常規的建立表的語句。

外部表: 資料是和hive進行共享的,當表刪除時,僅刪除表的元資料,但是資料還是保留的。

建立表使用的語句,location 指明了資料位於哪個位置下。

create external table if not exists table_name (col_name data_type)

location 'path/paht2' ;

分割槽表:

通過使用分割槽來水平分散壓力,將資料從物理上轉移到使用最頻繁的使用者更近的地方,及其他目的。在hive中分割槽的概念,其具有效能優勢,

分割槽表還可以將資料以一種符合邏輯的方式進行組織,比如分層儲存。使用關鍵字partitioned,使用的分割槽表會改變資料儲存組織方式。

create table if not exists table_name (col_name data_type)

partitioned by(col_name date_type , col_name data_type);

刪除表:

drop table if exists table_name ;

修改表:

表重新命名: alter table table_name rename new_name ;

增加,修改和刪除表分割槽:

alter table table_name add if not exists partition (col_name=''...) location 'path'

修改列資訊:可以進行重新命名,修改位置,型別或者注釋。

alter table table_name change column column_name

增加列: 新增列到以後欄位後

alter table table_name add columns(col_name date_type,.....);

刪除或替換列:

alter table table_name replace columns(

col_name col_type comment '',

......

)修改儲存屬性:

alter table table_name partition(col_name....) set fileformat sequencefile ;

Hive 資料定義

分割槽表與資料關聯的三種方式 上傳資料後修復 dfs mkdir p user hive warehouse dept month 201709 day 12 dfs put opt module datas dept.txt hive default msck repair table stu p...

Hive學習 HiveQL 資料定義

hiveql可能和mysql語句接近,但是兩者還是存在顯著差異。hive不支援行級插入操作 更新操作和刪除操作。hive也不支援事務。建立資料庫 create database financials 檢視hive中包含的資料庫 show databases 使用正則匹配篩選出需要的資料庫 show ...

Hive中的資料定義(DDL)

create database schema if not exists database name comment database comment location hdfs path with dbproperties property name property value,說明 1.在所有...