Hive基本操作

2021-09-13 03:14:05 字數 2975 閱讀 3998

3、hive中的表

4、分割槽(partition)

5、靜態分割槽和動態分割槽

6、分桶(bucket)

hive的資料都是儲存在hdfs上的,預設有乙個根目錄,在hive-site.xml中,由引數hive.metastore.warehouse.dir指定。預設值為/user/hive/warehouse.

show databases;
use kaidy;
語法:create database|schema [if not exists]

#hive中的資料庫在hdfs上的儲存路徑為:

$/databasename.db

比如,名為kaidy的資料庫儲存路徑為:

/user/hive/warehouse/kaidy.db

show tables;
語法create [temporary] [external] table [if not exists] [db_name.] table_name

[(col_name data_type [comment col_comment], …)]

[comment table_comment]

[row format row_format]

[stored as file_format]

語法drop table [if exists] table_name;

#hive中的表在hdfs上的儲存路徑為:

$/databasename.db/tablename/

describe extended db_name.table_name

–formatted關鍵字替代extended能提供更加可讀和冗長的輸出資訊

describe mydb.employee.salary

–如果只想檢視某一列的資訊,則使用extended也不會提供更多的資訊

語法:show create table ([db_name.]table_name|view_name)

語法:show tblproperties table_name

select * from table_name;

select count(*) from table_name;

select * from table_name limit 5;

hive中的表分為內部表(managed_table)和外部表(external_table)。

內部表和外部表最大的區別:

內部表drop時候會刪除hdfs上的資料;

外部表drop時候不會刪除hdfs上的資料;

內部表適用場景:

hive中間表、結果表、一般不需要從外部(如本地檔案、hdfs上load資料)的情況。

外部表適用場景:

源表,需要定期將外部資料對映到表中。

使用場景:

每天將收集到的**日誌定期流入hdfs文字檔案,一天乙個目錄;

在hive中建立外部表作為源表,通過新增分割槽的方式,將每天hdfs上的原始日誌對映到外部表的天分割槽中;

在外部表(原始日誌表)的基礎上做大量的統計分析,用到的中間表、結果表使用內部表儲存,資料通過select+insert進入內部表。

create table if not exists alarm3 ( eid int, name string, salary float, destination string, month string) comment 『alarm details』 partitioned by (p_province string) clustered by (eid) into 8 buckets stored as orc tblproperties (『transactional』=『true』);

語法:show partitions table_name [partition(partition_desc)]

p_date=2019-01-01/p_province=beijingshi

p_date=2019-02-01/p_province=beijingshi

p_date=2019-02-01/p_province=tianjinshi

p_date=2019-02-01/p_province=xinjiangweiwuerzuzizh

p_date=2019-02-01/p_province=zhejiangsheng

##基於單個分割槽查詢例子:

select * from table_name where p_date=『2019-02-01』 limit 100;

##基於兩級分割槽查詢例子:

select * from table_name where concat_ws(』,』,p_date,p_province)=『2019-02-01,beijingshi』 limit 100;

alter table table_name drop partition(p_province=『anhuisheng』);

alter table table_name drop if exists

partition(dt=『20180401』),

partition(dt=『20180402』),

partition(dt=『20180403』),

partition(dt=『20180404』);

多個分割槽欄位時,可以實現半自動分割槽(部分字段靜態分割槽,注意靜態分割槽欄位要在動態分割槽字段前面)

#建立表分桶示例:

create table if not exists alarm2 ( eid int, name string, salary float, destination string, month string, province string) comment 『alarm details』 clustered by (eid) into 8 buckets stored as orc tblproperties (『transactional』=『true』);

hive基本操作

1.顯示所有資料庫 show databases 2.使用某個資料庫 use xx xx表示某個資料庫名 3.顯示某資料庫下的所有表 show tables 4.檢視表結構 顯示各欄位 desc 表名 5.資料匯出到本地 在hive中操作 insert overwrite local directo...

hive 基本操作

檢視表的詳細資訊 desc 表名 desc formatted 表名 建立外部表 create external table emp ext empno int,ename string,job string,mgr int,hiredate string,sal double,comm doubl...

HIVE基本操作

hive操作 一 建立分割槽 乙個表可以有多個分割槽,為避免過多的小檔案,建議只能對離散字段進行分割槽 create table if not exists stocks ymd date,price open float,price high float,price low float,price...