Hive數(shù)據(jù)類型
基本數(shù)據(jù)類型
Hive數(shù)據(jù)類型 | Java數(shù)據(jù)類型 | 長(zhǎng)度 |
---|---|---|
TINYINT | byte | 1byte有符號(hào)整數(shù) |
SMALINT | short | 2byte有符號(hào)整數(shù) |
INT | int | 4byte有符號(hào)整數(shù) |
BIGINT | long | 8byte有符號(hào)整數(shù) |
BOOLEAN | boolean | 布爾類型,true或者false |
FLOAT | float | 單精度浮點(diǎn)數(shù) |
DOUBLE | double | 雙精度浮點(diǎn)數(shù) |
STRING | string | 字符系列耍目「嘟铮可以指定字符集。 可以使用單引號(hào)或者雙引號(hào)制妄。 |
TIMESTAMP | 時(shí)間類型 | |
BINARY | 字節(jié)數(shù)組 |
對(duì)于Hive的String類型相當(dāng)于數(shù)據(jù)庫(kù)的varchar類型掸绞,該類型是一個(gè)可變的字符串,不過(guò)它不能聲明其中最多能存儲(chǔ)多少個(gè)字符耕捞,理論上它可以存儲(chǔ)2GB的字符數(shù)衔掸。
集合數(shù)據(jù)類型
數(shù)據(jù)類型 | 描述 |
---|---|
STRUCT | 和c語(yǔ)言中的struct類似,都可以通過(guò)“點(diǎn)”符號(hào)訪問(wèn)元素內(nèi)容俺抽。例如敞映,如果某個(gè)列的數(shù)據(jù)類型是STRUCT{first STRING, last STRING},那么第1個(gè)元素可以通過(guò)字段.first來(lái)引用。 |
MAP | MAP是一組鍵-值對(duì)元組集合,使用數(shù)組表示法可以訪問(wèn)數(shù)據(jù)。例如姊途,如果某個(gè)列的數(shù)據(jù)類型是MAP调榄,其中鍵->值對(duì)是’first’->’John’和’last’->’Doe’,那么可以通過(guò)字段名[‘last’]獲取最后一個(gè)元素 |
ARRAY | 數(shù)組是一組具有相同類型和名稱的變量的集合壤追。這些變量稱為數(shù)組的元素,每個(gè)數(shù)組元素都有一個(gè)編號(hào),編號(hào)從零開(kāi)始档桃。例如,數(shù)組值為[‘John’,‘Doe’]憔晒,那么第2個(gè)元素可以通過(guò)數(shù)組名[1]進(jìn)行引用藻肄。 |
Hive有三種復(fù)雜數(shù)據(jù)類型ARRAY、MAP 和 STRUCT拒担。ARRAY和MAP與Java中的Array和Map類似嘹屯,而STRUCT與C語(yǔ)言中的Struct類似,它封裝了一個(gè)命名字段集合从撼,復(fù)雜數(shù)據(jù)類型允許任意層次的嵌套州弟。
案例說(shuō)明
- 假設(shè)某表有如下一行,我們用JSON格式來(lái)表示其數(shù)據(jù)結(jié)構(gòu)。在Hive下訪問(wèn)的格式為
{
"name": "songsong",
"friends": ["bingbing" , "lili"] , //列表Array,
"children": { //鍵值Map,
"xiao song": 18 ,
"xiaoxiao song": 19
}
"address": { //結(jié)構(gòu)Struct,
"street": "hui long guan" ,
"city": "beijing"
}
}
- 基于上述數(shù)據(jù)結(jié)構(gòu)婆翔,我們?cè)贖ive里創(chuàng)建對(duì)應(yīng)的表桐经,并導(dǎo)入數(shù)據(jù)。
- 創(chuàng)建本地測(cè)試文件test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
- 注意:MAP浙滤,STRUCT和ARRAY里的元素間關(guān)系都可以用同一個(gè)字符表示阴挣,這里用“_”。
- Hive上創(chuàng)建測(cè)試表test
create table test(
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
字段解釋:
row format delimited fields terminated by ','
-- 列分隔符
collection items terminated by '_'
--MAP STRUCT 和 ARRAY 的分隔符(數(shù)據(jù)分割符號(hào))
map keys terminated by ':'
-- MAP中的key與value的分隔符
lines terminated by '\n';
-- 行分隔符
- 導(dǎo)入文本數(shù)據(jù)到測(cè)試表
hive (default)> load data local inpath ‘/opt/module/datas/test.txt’into table test
- 訪問(wèn)三種集合列里的數(shù)據(jù)纺腊,以下分別是ARRAY畔咧,MAP,STRUCT的訪問(wèn)方式
hive (default)> select friends[1],children['xiao song'],address.city from test
where name="songsong";
OK
_c0 _c1 city
lili 18 beijing
Time taken: 0.076 seconds, Fetched: 1 row(s)
類型轉(zhuǎn)化
Hive的原子數(shù)據(jù)類型是可以進(jìn)行隱式轉(zhuǎn)換的揖膜,類似于Java的類型轉(zhuǎn)換誓沸,例如某表達(dá)式使用INT類型,TINYINT會(huì)自動(dòng)轉(zhuǎn)換為INT類型壹粟,但是Hive不會(huì)進(jìn)行反向轉(zhuǎn)化拜隧,例如,某表達(dá)式使用TINYINT類型趁仙,INT不會(huì)自動(dòng)轉(zhuǎn)換為TINYINT類型洪添,它會(huì)返回錯(cuò)誤,除非使用CAST操作雀费。
隱式類型轉(zhuǎn)換規(guī)則如下:
任何整數(shù)類型都可以隱式地轉(zhuǎn)換為一個(gè)范圍更廣的類型干奢,如TINYINT可以轉(zhuǎn)換成INT,INT可以轉(zhuǎn)換成BIGINT盏袄。
所有整數(shù)類型忿峻、FLOAT和STRING類型都可以隱式地轉(zhuǎn)換成DOUBLE。
TINYINT辕羽、SMALLINT逛尚、INT都可以轉(zhuǎn)換為FLOAT。
BOOLEAN類型不可以轉(zhuǎn)換為任何其它的類型刁愿。
可以使用CAST操作顯示進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換
- 例如CAST('1' AS INT)將把字符串'1' 轉(zhuǎn)換成整數(shù)1绰寞;
- 如果強(qiáng)制類型轉(zhuǎn)換失敗,如執(zhí)行CAST('X' AS INT)酌毡,表達(dá)式返回空值 NULL克握。
DDL數(shù)據(jù)定義
創(chuàng)建數(shù)據(jù)庫(kù)
- 創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)蕾管,數(shù)據(jù)庫(kù)在HDFS上的默認(rèn)存儲(chǔ)路徑是/user/hive/warehouse/*.db
hive (default)> create database db_hive;
- 避免要?jiǎng)?chuàng)建的數(shù)據(jù)庫(kù)已經(jīng)存在錯(cuò)誤枷踏,增加if not exists判斷。(標(biāo)準(zhǔn)寫(xiě)法)
hive (default)> create database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db_hive already exists
hive (default)> create database if not exists db_hive;
- 創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)掰曾,指定數(shù)據(jù)庫(kù)在HDFS上存放的位置
hive (default)> create database db_hive2 location '/db_hive2.db';
查詢數(shù)據(jù)庫(kù)
顯示數(shù)據(jù)庫(kù)
- 顯示數(shù)據(jù)庫(kù)
hive> show databases;
- 過(guò)濾顯示查詢的數(shù)據(jù)庫(kù)
hive> show databases like 'db_hive*';
OK
db_hive
db_hive_1
查看數(shù)據(jù)庫(kù)
- 顯示數(shù)據(jù)庫(kù)信息
hive> desc database db_hive;
OK
db_hive hdfs://hadoop102:9000/user/hive/warehouse/db_hive.db atguiguUSER
- 顯示數(shù)據(jù)庫(kù)詳細(xì)信息旭蠕,extended
hive> desc database extended db_hive;
OK
db_hive hdfs://hadoop102:9000/user/hive/warehouse/db_hive.db atguiguUSER
切換當(dāng)前數(shù)據(jù)庫(kù)
hive (default)> use db_hive;
修改數(shù)據(jù)庫(kù)
用戶可以使用ALTER DATABASE命令為某個(gè)數(shù)據(jù)庫(kù)的DBPROPERTIES設(shè)置鍵-值對(duì)屬性值,來(lái)描述這個(gè)數(shù)據(jù)庫(kù)的屬性信息。數(shù)據(jù)庫(kù)的其他元數(shù)據(jù)信息都是不可更改的掏熬,包括數(shù)據(jù)庫(kù)名和數(shù)據(jù)庫(kù)所在的目錄位置佑稠。
hive (default)> alter database db_hive set dbproperties('createtime'='20170830');
在hive中查看修改結(jié)果
hive> desc database extended db_hive;
db_name comment location owner_name owner_type parameters
db_hive hdfs://hadoop102:8020/user/hive/warehouse/db_hive.db atguigu USER {createtime=20170830}
刪除數(shù)據(jù)庫(kù)
- 刪除空數(shù)據(jù)庫(kù)
hive>drop database db_hive2;
- 如果刪除的數(shù)據(jù)庫(kù)不存在,最好采用 if exists判斷數(shù)據(jù)庫(kù)是否存在
hive> drop database db_hive;
FAILED: SemanticException [Error 10072]: Database does not exist: db_hive
hive> drop database if exists db_hive2;
- 如果數(shù)據(jù)庫(kù)不為空旗芬,可以采用cascade命令舌胶,強(qiáng)制刪除
hive> drop database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database db_hive is not empty. One or more tables exist.)
hive> drop database db_hive cascade;
創(chuàng)建表
建表語(yǔ)法
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
字段解釋說(shuō)明
(1)CREATE TABLE 創(chuàng)建一個(gè)指定名字的表。如果相同名字的表已經(jīng)存在疮丛,則拋出異常幔嫂;用戶可以用 IF NOT EXISTS 選項(xiàng)來(lái)忽略這個(gè)異常。
(2)EXTERNAL關(guān)鍵字可以讓用戶創(chuàng)建一個(gè)外部表誊薄,在建表的同時(shí)指定一個(gè)指向?qū)嶋H數(shù)據(jù)的路徑(LOCATION)履恩,Hive創(chuàng)建內(nèi)部表時(shí),會(huì)將數(shù)據(jù)移動(dòng)到數(shù)據(jù)倉(cāng)庫(kù)指向的路徑呢蔫;若創(chuàng)建外部表切心,僅記錄數(shù)據(jù)所在的路徑,不對(duì)數(shù)據(jù)的位置做任何改變片吊。在刪除表的時(shí)候绽昏,內(nèi)部表的元數(shù)據(jù)和數(shù)據(jù)會(huì)被一起刪除,而外部表只刪除元數(shù)據(jù)俏脊,不刪除數(shù)據(jù)而涉。
(3)COMMENT:為表和列添加注釋。
(4)PARTITIONED BY創(chuàng)建分區(qū)表
(5)CLUSTERED BY創(chuàng)建分桶表
(6)SORTED BY不常用
(7)ROW FORMAT
DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char]
[MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
| SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
用戶在建表的時(shí)候可以自定義SerDe或者使用自帶的SerDe联予。如果沒(méi)有指定ROW FORMAT 或者ROW FORMAT DELIMITED啼县,將會(huì)使用自帶的SerDe。在建表的時(shí)候沸久,用戶還需要為表指定列季眷,用戶在指定表的列的同時(shí)也會(huì)指定自定義的SerDe,Hive通過(guò)SerDe確定表的具體的列的數(shù)據(jù)卷胯。
SerDe是Serialize/Deserilize的簡(jiǎn)稱子刮,目的是用于序列化和反序列化。
(8)STORED AS指定存儲(chǔ)文件類型
常用的存儲(chǔ)文件類型:SEQUENCEFILE(二進(jìn)制序列文件)窑睁、TEXTFILE(文本)挺峡、RCFILE(列式存儲(chǔ)格式文件)
如果文件數(shù)據(jù)是純文本,可以使用STORED AS TEXTFILE担钮。如果數(shù)據(jù)需要壓縮橱赠,使用 STORED AS SEQUENCEFILE。
(9)LOCATION :指定表在HDFS上的存儲(chǔ)位置箫津。
(10)LIKE允許用戶復(fù)制現(xiàn)有的表結(jié)構(gòu)狭姨,但是不復(fù)制數(shù)據(jù)宰啦。
管理表
理論
默認(rèn)創(chuàng)建的表都是所謂的管理表,有時(shí)也被稱為內(nèi)部表饼拍。
因?yàn)檫@種表赡模,Hive會(huì)(或多或少地)控制著數(shù)據(jù)的生命周期。
Hive默認(rèn)情況下會(huì)將這些表的數(shù)據(jù)存儲(chǔ)在由配置項(xiàng)hive.metastore.warehouse.dir(例如师抄,/user/hive/warehouse)所定義的目錄的子目錄下漓柑。
當(dāng)我們刪除一個(gè)管理表時(shí),Hive也會(huì)刪除這個(gè)表中數(shù)據(jù)叨吮。管理表不適合和其他工具共享數(shù)據(jù)欺缘。
案例實(shí)操
(1)普通創(chuàng)建表
create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student2';
(2)根據(jù)查詢結(jié)果創(chuàng)建表(查詢的結(jié)果會(huì)添加到新創(chuàng)建的表中)
create table if not exists student3 as select id, name from student;
(3)根據(jù)已經(jīng)存在的表結(jié)構(gòu)創(chuàng)建表
create table if not exists student4 like student;
(4)查詢表的類型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
外部表
理論
因?yàn)楸硎峭獠勘恚訦ive并非認(rèn)為其完全擁有這份數(shù)據(jù)挤安。刪除該表并不會(huì)刪除掉這份數(shù)據(jù)谚殊,不過(guò)描述表的元數(shù)據(jù)信息會(huì)被刪除掉。
管理表和外部表使用場(chǎng)景
每天將收集到的網(wǎng)站日志定期流入HDFS文本文件蛤铜。在外部表(原始日志表)的基礎(chǔ)上做大量的統(tǒng)計(jì)分析嫩絮,用到的中間表、結(jié)果表使用內(nèi)部表存儲(chǔ)围肥,數(shù)據(jù)通過(guò)SELECT+INSERT進(jìn)入內(nèi)部表剿干。
案例實(shí)操
分別創(chuàng)建部門和員工外部表,并向表中導(dǎo)入數(shù)據(jù)穆刻。
(1)原始數(shù)據(jù)
(2)建表語(yǔ)句
創(chuàng)建部門表
create external table if not exists default.dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by '\t';
創(chuàng)建員工表
create external table if not exists default.emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by '\t';
(3)查看創(chuàng)建的表
hive (default)> show tables;
OK
tab_name
dept
emp
(4)向外部表中導(dǎo)入數(shù)據(jù)
導(dǎo)入數(shù)據(jù)
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept;
hive (default)> load data local inpath '/opt/module/datas/emp.txt' into table default.emp;
查詢結(jié)果
hive (default)> select * from emp;
hive (default)> select * from dept;
(5)查看表格式化數(shù)據(jù)
hive (default)> desc formatted dept;
Table Type: EXTERNAL_TABLE
管理表與外部表的互相轉(zhuǎn)換
(1)查詢表的類型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
(2)修改內(nèi)部表student2為外部表
alter table student2 set tblproperties('EXTERNAL'='TRUE');
(3)查詢表的類型
hive (default)> desc formatted student2;
Table Type: EXTERNAL_TABLE
(4)修改外部表student2為內(nèi)部表
alter table student2 set tblproperties('EXTERNAL'='FALSE');
(5)查詢表的類型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
注意:
('EXTERNAL'='TRUE')
和('EXTERNAL'='FALSE')
為固定寫(xiě)法置尔,區(qū)分大小寫(xiě)!
分區(qū)表
分區(qū)表實(shí)際上就是對(duì)應(yīng)一個(gè)HDFS文件系統(tǒng)上的獨(dú)立的文件夾氢伟,該文件夾下是該分區(qū)所有的數(shù)據(jù)文件榜轿。
Hive中的分區(qū)就是分目錄,把一個(gè)大的數(shù)據(jù)集根據(jù)業(yè)務(wù)需要分割成小的數(shù)據(jù)集朵锣。
在查詢時(shí)通過(guò)WHERE子句中的表達(dá)式選擇查詢所需要的指定的分區(qū)谬盐,這樣的查詢效率會(huì)提高很多。
分區(qū)表基本操作
引入分區(qū)表
/user/hive/warehouse/log_partition/20170702/20170702.log
/user/hive/warehouse/log_partition/20170703/20170703.log
/user/hive/warehouse/log_partition/20170704/20170704.log
創(chuàng)建分區(qū)表
hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
加載數(shù)據(jù)到分區(qū)表
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201709');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201708');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201707’);
查詢分區(qū)表中數(shù)據(jù)
- 單分區(qū)查詢
hive (default)> select * from dept_partition where month='201709';
- 多分區(qū)聯(lián)合查詢
hive (default)> select * from dept_partition where month='201709'
union
select * from dept_partition where month='201708'
union
select * from dept_partition where month='201707';
_u3.deptno _u3.dname _u3.loc _u3.month
10 ACCOUNTING NEW YORK 201707
10 ACCOUNTING NEW YORK 201708
10 ACCOUNTING NEW YORK 201709
20 RESEARCH DALLAS 201707
20 RESEARCH DALLAS 201708
20 RESEARCH DALLAS 201709
30 SALES CHICAGO 201707
30 SALES CHICAGO 201708
30 SALES CHICAGO 201709
40 OPERATIONS BOSTON 201707
40 OPERATIONS BOSTON 201708
40 OPERATIONS BOSTON 201709
增加分區(qū)
- 創(chuàng)建單個(gè)分區(qū)
hive (default)> alter table dept_partition add partition(month='201706') ;
- 創(chuàng)建多個(gè)分區(qū)
hive (default)> alter table dept_partition add partition(month='201705') partition(month='201704');
刪除分區(qū)
- 刪除單個(gè)分區(qū)
hive (default)> alter table dept_partition drop partition (month='201704');
- 刪除多個(gè)分區(qū)
hive (default)> alter table dept_partition drop partition (month='201705'), partition (month='201706');
查看分區(qū)表有多少分區(qū)
hive> show partitions dept_partition;
查看分區(qū)表結(jié)構(gòu)
hive> desc formatted dept_partition;
# Partition Information
# col_name data_type comment
month string
分區(qū)表注意事項(xiàng)
創(chuàng)建二級(jí)分區(qū)表
hive (default)> create table dept_partition2(
deptno int, dname string, loc string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t';
正常的加載數(shù)據(jù)
- 加載數(shù)據(jù)到二級(jí)分區(qū)表中
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
default.dept_partition2 partition(month='201709', day='13');
- 查詢分區(qū)數(shù)據(jù)
hive (default)> select * from dept_partition2 where month='201709' and day='13';
-
把數(shù)據(jù)直接上傳到分區(qū)目錄上诚些,讓分區(qū)表和數(shù)據(jù)產(chǎn)生關(guān)聯(lián)的三種方式
- 方式一:上傳數(shù)據(jù)后修復(fù)
上傳數(shù)據(jù) hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=12; hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=12; 查詢數(shù)據(jù)(查詢不到剛上傳的數(shù)據(jù)) hive (default)> select * from dept_partition2 where month='201709' and day='12'; 執(zhí)行修復(fù)命令 hive> msck repair table dept_partition2; 再次查詢數(shù)據(jù) hive (default)> select * from dept_partition2 where month='201709' and day='12';
- 方式二:上傳數(shù)據(jù)后添加分區(qū)
上傳數(shù)據(jù) hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=11; hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201709/day=11; 執(zhí)行添加分區(qū) hive (default)> alter table dept_partition2 add partition(month='201709', day='11'); 查詢數(shù)據(jù) hive (default)> select * from dept_partition2 where month='201709' and day='11';
- 方式三:創(chuàng)建文件夾后load數(shù)據(jù)到分區(qū)
創(chuàng)建目錄 hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201709/day=10; 上傳數(shù)據(jù) hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201709',day='10'); 查詢數(shù)據(jù) hive (default)> select * from dept_partition2 where month='201709' and day='10';
修改表
重命名表
1.語(yǔ)法
ALTER TABLE table_name RENAME TO new_table_name
2.實(shí)操案例
hive (default)> alter table dept_partition2 rename to dept_partition3;
增加飞傀、修改和刪除表分區(qū)
詳見(jiàn)分區(qū)表基本操作
增加/修改/替換列信息
語(yǔ)法
更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
增加和替換列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
注:ADD是代表新增一字段,字段位置在所有列后面(partition列前)诬烹,REPLACE則是表示替換表中所有字段砸烦。
實(shí)操案例
(1)查詢表結(jié)構(gòu)
hive> desc dept_partition;
(2)添加列
hive (default)> alter table dept_partition add columns(deptdesc string);
(3)查詢表結(jié)構(gòu)
hive> desc dept_partition;
(4)更新列
hive (default)> alter table dept_partition change column deptdesc desc int;
(5)查詢表結(jié)構(gòu)
hive> desc dept_partition;
(6)替換列
hive (default)> alter table dept_partition replace columns(deptno string, dname
string, loc string);
(7)查詢表結(jié)構(gòu)
hive> desc dept_partition;
刪除表
hive (default)> drop table dept_partition;
DML 數(shù)據(jù)操作
數(shù)據(jù)導(dǎo)入
向表中裝載數(shù)據(jù)(load)
語(yǔ)法
hive> load data [local] inpath '/opt/module/datas/student.txt' overwrite | into table student [partition (partcol1=val1,…)];
(1)load data:表示加載數(shù)據(jù)
(2)local:表示從本地加載數(shù)據(jù)到hive表;否則從HDFS加載數(shù)據(jù)到hive表
(3)inpath:表示加載數(shù)據(jù)的路徑
(4)overwrite:表示覆蓋表中已有數(shù)據(jù)绞吁,否則表示追加
(5)into table:表示加載到哪張表
(6)student:表示具體的表
(7)partition:表示上傳到指定分區(qū)
案例實(shí)操
(0)創(chuàng)建一張表
hive (default)> create table student(id string, name string) row format delimited fields terminated by '\t';
(1)加載本地文件到hive
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;
(2)加載HDFS文件到hive中
上傳文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/atguigu/hive;
加載HDFS上數(shù)據(jù)
hive (default)> load data inpath '/user/atguigu/hive/student.txt' into table default.student;
(3)加載數(shù)據(jù)覆蓋表中已有的數(shù)據(jù)
上傳文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/atguigu/hive;
加載數(shù)據(jù)覆蓋表中已有的數(shù)據(jù)
hive (default)> load data inpath '/user/atguigu/hive/student.txt' overwrite into table default.student;
通過(guò)查詢語(yǔ)句向表中插入數(shù)據(jù)(Insert)
1.創(chuàng)建一張分區(qū)表
hive (default)> create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2.基本插入數(shù)據(jù)
hive (default)> insert into table student partition(month='201709') values(1,'wangwu');
3.基本模式插入(根據(jù)單張表查詢結(jié)果)
hive (default)> insert overwrite table student partition(month='201708')
select id, name from student where month='201709';
4.多插入模式(根據(jù)多張表查詢結(jié)果)
hive (default)> from student
insert overwrite table student partition(month='201707')
select id, name where month='201709'
insert overwrite table student partition(month='201706')
select id, name where month='201709';
查詢語(yǔ)句中創(chuàng)建表并加載數(shù)據(jù)(As Select)
詳見(jiàn)創(chuàng)建表
根據(jù)查詢結(jié)果創(chuàng)建表(查詢的結(jié)果會(huì)添加到新創(chuàng)建的表中)
create table if not exists student3
as select id, name from student;
創(chuàng)建表時(shí)通過(guò)Location指定加載數(shù)據(jù)路徑
1.創(chuàng)建表幢痘,并指定在hdfs上的位置
hive (default)> create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
2.上傳數(shù)據(jù)到hdfs上
hive (default)> dfs -put /opt/module/datas/student.txt
/user/hive/warehouse/student5;
3.查詢數(shù)據(jù)
hive (default)> select * from student5;
Import數(shù)據(jù)到指定Hive表中
注意:先用export導(dǎo)出后,再將數(shù)據(jù)導(dǎo)入掀泳。
hive (default)> import table student2 partition(month='201709') from
'/user/hive/warehouse/export/student';
數(shù)據(jù)導(dǎo)出
Insert導(dǎo)出
1.將查詢的結(jié)果導(dǎo)出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
2.將查詢的結(jié)果格式化導(dǎo)出到本地
hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
3.將查詢的結(jié)果導(dǎo)出到HDFS上(沒(méi)有l(wèi)ocal)
hive (default)> insert overwrite directory '/user/atguigu/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
Hadoop命令導(dǎo)出到本地
hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0
/opt/module/datas/export/student3.txt;
Hive Shell 命令導(dǎo)出
基本語(yǔ)法:(hive -f/-e 執(zhí)行語(yǔ)句或者腳本 > file)
[atguigu@hadoop102 hive]$ bin/hive -e 'select * from default.student;' >
/opt/module/datas/export/student4.txt;
Export導(dǎo)出到HDFS上
hive(default)> export table default.student to
'/user/hive/warehouse/export/student';
Sqoop導(dǎo)出
后續(xù)講
清除表中數(shù)據(jù)(Truncate)
注意:Truncate只能刪除管理表雪隧,不能刪除外部表中數(shù)據(jù)
hive (default)> truncate table student;