庫操作
# 標(biāo)準(zhǔn)做法
create database if not exists test;
# 直接創(chuàng)建
create database test;
# 指定數(shù)據(jù)庫位置
create database if not exists test location '/user/zz/hive/workspace/test' ;
# 查詢所有庫
show databases;
# 模糊查詢庫(查詢test開頭的庫)
show databases like "test*"
# 使用庫(庫的名字是test)
use test;
#描述庫
desc database test
desc database extended test ;
#刪除庫
drop database test;
#展示庫中的表
show tables in test;
表操作
創(chuàng)建內(nèi)部表认轨,默認存儲在/user/hive/warehouse下绅络,也可以通過location指定,刪除表時,會刪除hdfs中的數(shù)據(jù)以及元數(shù)據(jù);
場景外部表恩急,外部表稱之為EXTERNAL_TABLE节视,在創(chuàng)建表時可以自己指定目錄位置(LOCATION),刪除表時假栓,只會刪除元數(shù)據(jù)不會刪除hdfs中的數(shù)據(jù)。
創(chuàng)建表語法:
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]
| STORED BY 'storage.handler.class.name' [ WITH SERDEPROPERTIES (...) ]
]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)] [AS select_statement] CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
LIKE existing_table_name
[LOCATION hdfs_path]
data_type
: primitive_type
| array_type
| map_type
| struct_type
primitive_type
: TINYINT
| SMALLINT
| INT
| BIGINT
| BOOLEAN
| FLOAT
| DOUBLE
| STRING
array_type
: ARRAY < data_type >
map_type
: MAP < primitive_type, data_type >
struct_type
: STRUCT < col_name : data_type [COMMENT col_comment], ...>
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, ...)]
file_format:
: SEQUENCEFILE
| TEXTFILE
| RCFILE (Note: only available starting with 0.6.0)
| INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
1霍掺、CREATE TABLE
創(chuàng)建一個指定名字的表匾荆。如果相同名字的表已經(jīng)存在,則拋出異常杆烁;
用戶可以用 IF NOT EXISTS 選項來忽略這個異常牙丽。
2、EXTERNAL
可以讓用戶創(chuàng)建一個外部表兔魂,在建表的同時指定一個指向?qū)嶋H數(shù)據(jù)的路徑(LOCATION)烤芦。Hive 創(chuàng)建內(nèi)部表時,會將數(shù)據(jù)移動到數(shù)據(jù)倉庫指向的路徑析校;若創(chuàng)建外部表构罗,僅記錄數(shù)據(jù)所在的路徑,不對數(shù)據(jù)的位置做任何改變智玻。在刪除表的時候遂唧,內(nèi)部表的元數(shù)據(jù)和數(shù)據(jù)會被一起刪除,而外部表只刪除元數(shù)據(jù),不刪除數(shù)據(jù).
3、LIKE
允許用戶復(fù)制現(xiàn)有的表結(jié)構(gòu)吊奢,但是不復(fù)制數(shù)據(jù)盖彭。
4、ROW FORMAT DELIMITED
是用來設(shè)置創(chuàng)建的表在加載數(shù)據(jù)的時候页滚,支持的列分隔符召边。
Hive默認的分隔符是\001,屬于不可見字符裹驰,這個字符在vi里是^A
5隧熙、STORED AS
文件數(shù)據(jù)格式。
如果文件數(shù)據(jù)是純文本邦马,可以使用 STORED AS TEXTFILE贱鼻。
如果數(shù)據(jù)需要壓縮,使用 STORED AS SEQUENCEFILE滋将。
6邻悬、CLUSTERED BY
對于每一個表(table)或者分區(qū), Hive可以進一步組織成桶随闽,也就是說桶是更為細粒度的數(shù)據(jù)范圍劃分父丰。Hive也是 針對某一列進行桶的組織。Hive采用對列值哈希,然后除以桶的個數(shù)求余的方式?jīng)Q定該條記錄存放在哪個桶當(dāng)中蛾扇。
7攘烛、LOCATION
指定加載數(shù)據(jù)路徑(指定在hdfs上的位置).針對的extetnal外部表,要指定存儲路徑镀首,不指定也沒事坟漱,默認路徑。內(nèi)部表不用指定更哄,默認路徑/user/hive/warehouse
CREATE TABLE創(chuàng)建一個具有給定名稱的表芋齿。如果已存在具有相同名稱的表或視圖,則會引發(fā)錯誤成翩。您可以使用IF NOT EXISTS跳過錯誤觅捆。
8、PARTITIONED BY
給表做分區(qū)麻敌,決定了表是否是分區(qū)表栅炒。
9.TBLPROPERTIES
指定配置屬性
10.SKEWED BY
指定傾斜字段及值
三種創(chuàng)建表的方式
1.直接創(chuàng)建
create table temp.temp_create_table_test (
col1 string comment "字段1",
col2 int comment "字段2"
)
comment "測試"
partitioned by (part_col string comment "分區(qū)字段")
clustered by (col1) sorted by (col1 desc) into 2 buckets
skewed by (col1) on ((2), (3))
row format delimited fields terminated by "\t"
stored as parquet
location "/user/hive/warehouse/test.db/create_table_test"
tblproperties("parquet.compression"="snappy");
2.復(fù)制表結(jié)構(gòu)創(chuàng)建
create table test.create_table_like_test like test.create_table_test;
3.通過子查詢創(chuàng)建表
create table test.create_table_as_test as
select col1, col2 from test.create_table_test where part_col = '2';
展示表的創(chuàng)建語句:
show create table create_table_test;
展示表結(jié)構(gòu)
desc create_table_test;
查找表數(shù)據(jù)
基本與sql語句一致
#查詢?nèi)?select * from create_table_test;
#根據(jù)分區(qū)查詢
select col1, col2 from create_table_test where part_col = '2';
刪除表
drop table create_table_test;
#刪除表中數(shù)據(jù)保留表結(jié)構(gòu)
truncate table iot_devicelocation;
hive作為數(shù)據(jù)倉庫,與數(shù)據(jù)庫還是有本質(zhì)的區(qū)別术羔,hive不支持單條數(shù)據(jù)的刪除和修改赢赊,按照網(wǎng)上的說法是可以操作,需要進行一些配置聂示,但是配置成功后域携,操作還是有很多問題的,而且這也違背了數(shù)據(jù)倉庫的本來作用鱼喉,所以不建議去增加單條數(shù)據(jù)修改的配置
分區(qū)表下一篇單獨說明秀鞭,這篇沒對分區(qū)表做過多介紹