HIVE中的表操作

2021-09-24 15:15:07 字數 2943 閱讀 6408

1.內部表

2.外部表

3.分割槽表

4.分通表

擴充套件: 臨時表 只有在程序中有效 程序結束 表所有資料刪除 與內部表類似

show databases ----- 檢視資料庫

show tables -----檢視表

use 資料庫名----進入資料庫

drop 資料庫名 ----刪除資料庫

drop 表名----刪除表

內部表建立的方式與mysql中的建立表的方式相同

create table 表名(

field type,

fields type,

…)row format delimited 設定行格式分隔

fields terminated by " 自己資料的格式" 根據格式解析文字的字段

collection items terminated by 「,」 集合以,分隔

map keys terminated by 「:」 map key value 以:分隔

lines terminated by 「\n」 ; 每條記錄以回車分隔

插入資料分為兩種

insert

insert into 表名 values(value,value…);

本地載入:

load data local inpath 『本地資料檔案路徑』 into table 表名

hdfs載入:

load data inpath 『hdfs://主機名:9000/檔案路徑』 into table 表名

外部表

外部表中的關鍵字 external(外部的) 與內部表的建立**不同的就是 table 前加不加external

建立外部表

create external table 表名(

field type,

fields type,

…)row format delimited 設定行格式分隔

fields terminated by " 自己資料的格式" 根據格式解析文字的字段

collection items terminated by 「,」 集合以,分隔

map keys terminated by 「:」 map key value 以:分隔

lines terminated by 「\n」 ; 每條記錄以回車分隔

匯入別的表資料
insert into table 新錶

select (與新錶中字段對應) from 有資料的表

區表的優點:提高處理計算效率

hive中的分割槽表 的partition就是分成不同的檔案目錄進行儲存

分割槽表又分為兩種 : 靜態分割槽 和 動態分割槽

分割槽表中也分內部表和外部表

在建立的時候來設定分割槽 注意 分割槽的字段和表裡設定的字段不能一直 不然會出現兩個一樣的字段

create [external] table 表名(

field type,

fields type,

…)partitioned by (fields type)

row format delimited 設定行格式分隔

fields terminated by " 自己資料的格式" 根據格式解析文字的字段

collection items terminated by 「,」 集合以,分隔

map keys terminated by 「:」 map key value 以:分隔

lines terminated by 「\n」 ; 每條記錄以回車分隔

只有乙個分割槽 在hdfs中 想當於檔案/partition

兩個分割槽 /partition/partition2

多個分割槽 /partition/partition2/…/…

分割槽的時候要根據業務需求,提前進行相應的設定 年月日時分秒----為了 乙個分割槽中的內容,提高計算效率

新增分割槽

alter table 表名

add partition (fields=value);

刪除分割槽
alter table 表名

drop partition (fields=value));

修改許可權的方式

在hive/conf/hive-site.xml 中新增屬性

啟動hive 後 用set進行設定

hive啟動時 hive --conf hive.exec.dynamic.partition=true

修改許可權

1.set hive.exec.dynamic.partition=true 開啟動態分割槽

2.set hive.exec.dynamice.partition.mode=nostrict 預設strct 至少有乙個靜態分割槽

開啟分桶

set hive.enforce.bucketing=true

create table 表名 (

id int,

name string,

age int)

clustered by (age) into 4 buckets

row format delimited

fields terminated by 『,』

載入資料

insert into table 表名select id,name,age from 表名(不可以是分桶表)

抽樣select * from bucket_table tablesample(bucket 1 out of 4 on age)

hive表的常見操作

一 建立表 1 建立外部表 create external table ad rule result source string comment entity type int comment partitioned by day string,hour string row format deli...

Hive中庫和表的常見操作

目錄 表的常見操作 create database schema if not exists database name comment database comment 庫的注釋說明 location hdfs path 庫在hdfs上的路徑 with dbproperties property ...

Hive操作表分割槽

建立分割槽表語句,使用關鍵字partition a 單分割槽建表語句 create table table name id int,content string partitioned by dt string 單分割槽表,按天分割槽,在表結構增加了dt列。以dt為資料夾區分 b 雙分割槽建表語句 ...