Hive DDL操作
DDL(Data Definition Language)
create,delete,alter
Hive數(shù)據(jù)抽象/結(jié)構(gòu)
database(HDFS一個(gè)目錄)->table(HDFS一個(gè)目錄)->partition分區(qū)表(HDFS一個(gè)目錄)->bucket桶(HDFS一個(gè)文件)
數(shù)據(jù)庫(kù)操作
CREATE 創(chuàng)建數(shù)據(jù)庫(kù)
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name[COMMENT database_comment][LOCATION hdfs_path][WITH DBPROPERTIES (property_name=property_value, ...)];例如:CREATE DATABASE IF NOT EXISTS hive;;
如果想知道我們剛創(chuàng)建的hive庫(kù)存放在哪里,應(yīng)該去對(duì)應(yīng)的mysql中查看:
去HDFS中查看對(duì)應(yīng)的位置:
刪除數(shù)據(jù)庫(kù)
DROP database hive;
如果這個(gè)庫(kù)中存在表,那么上面的語(yǔ)句會(huì)顯示無(wú)法刪除數(shù)據(jù)庫(kù)。如果要?jiǎng)h除表需要執(zhí)行:
drop database hive CASCADE級(jí)聯(lián)刪除表,但是一定要慎用
創(chuàng)建數(shù)據(jù)庫(kù)時(shí)修改HDFS存儲(chǔ)路徑
CREATE DATABASE IF NOT EXISTS hive2 LOCATION '/test/location';將創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)位于/test/location
首先確認(rèn)在HDFS中對(duì)應(yīng)的路徑是否存在,方便我們驗(yàn)證:
然后執(zhí)行創(chuàng)建語(yǔ)句:
然后在HDFS中查看:
查看數(shù)據(jù)庫(kù)的信息
使用命令desc database hive
創(chuàng)建數(shù)據(jù)庫(kù)時(shí)添加屬性
CREATE DATABASE IF NOT EXISTS hive3 WITH DBPROPERTIES('creator'='vincent');
查看詳細(xì)數(shù)據(jù)庫(kù)信息:
顯示當(dāng)前正在操作的數(shù)據(jù)庫(kù)
select current_database();可以顯示當(dāng)前正在使用的數(shù)據(jù)庫(kù)。
在hive中set KEY表示獲取到這個(gè)KEY的值,set KEY=VALUE表示設(shè)置這個(gè)KEY的值。
正則匹配數(shù)據(jù)庫(kù)
表操作
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name -- (Note: TEMPORARY available in Hive 0.14.0 and later)[(col_name data_type [column_constraint_specification] [COMMENT col_comment], ... [constraint_specification])][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][SKEWED BY (col_name, col_name, ...) -- (Note: Available in Hive 0.10.0 and later)]ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)[STORED AS DIRECTORIES][[ROW FORMAT row_format] [STORED AS file_format]| STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)] -- (Note: Available in Hive 0.6.0 and later)][LOCATION hdfs_path][TBLPROPERTIES (property_name=property_value, ...)] -- (Note: Available in Hive 0.6.0 and later)[AS select_statement]; -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)create 創(chuàng)建表
創(chuàng)建一張表:
CREATE TABLE emp( empno int, ename string, job string, mgr int, hiredate string, sal double, comm double, deptno int ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';Hive加載csv文件數(shù)據(jù)時(shí)跳過(guò)第一行
在創(chuàng)建表時(shí)添加tblproperties(“skip.header.line.count”=”1”);到后面。
create table sales (transactionId int, customerId int, itemId int, amountPaid int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' tblproperties("skip.header.line.count"="1");查看表信息以及詳細(xì)信息
這種方式不方便查看,常用的方式是desc formatted emp:
導(dǎo)入數(shù)據(jù)
LOAD DATA LOCAL INPATH '/home/' OVERWRITE INTO TABLE emp;導(dǎo)入本地的一個(gè)文件
表數(shù)據(jù)在HDFS中查看
進(jìn)一步查看HDFS的文件內(nèi)容:
跟本地的數(shù)據(jù)內(nèi)容是一樣的。
修改表名
ALTER TABLE emp rename to emp2;查看HDFS中的數(shù)據(jù):
總結(jié)
以上是生活随笔為你收集整理的Hive DDL操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Hive概述
- 下一篇: Hive DML操作