1. 程式人生 > >Hive的DDL資料定義和DML資料操作

Hive的DDL資料定義和DML資料操作

Hive資料型別
Java資料型別 Hive資料型別 長度
byte TINYINT 1byte
short SMALINT 2byte
int INT 4byte
long BIGINT 8byte
float FLOAT 單精度浮點數
double DOUBLE 雙精度浮點數
string STRING 字元
TIMESTAMP 時間型別
BINARY 位元組陣列

DDL資料定義
1)建立資料庫
-》檢視資料庫
show databases;
-》建立資料庫
create database hive_db;
-》建立資料庫標準寫法
create database if not exist db_hive;
-》建立資料庫指定所在hdfs路徑
create database hive_db1 location ‘/hive_db’;
2)修改資料庫
-》檢視資料庫結構
desc database hive-db;
-》新增描述資訊
alter database hive_db set dbproperties(‘datais’=‘terry’); -》檢視拓展屬性
desc database extended hive_db;
3)查詢資料庫
-》顯示資料庫
show databases; -》篩選查詢的資料庫
show database like ‘db*’;

4)刪除資料庫
	 -》刪除資料庫
	drop dabase hive_db; -》刪除資料庫標準寫法
	drop database if exists hive_db;
5)建立表
	 -》建立表
	> create table db_h(id int,name string) > row format
	> delimited fields
	> terminated by "\t"
6)管理表(內部表)
	 不擅長做資料共享 刪除hive中管理表,資料刪除。
	-》載入資料
	load data local inpath '/root/terry.txt' into table emp; -》查詢並儲存到一張新的表
	create table if not exists emp2 as select * from emp where name = 'hunte r';
	-》查詢表結構
	desc formatted emp;
		Table Type: MANAGED_TABLE
7)外部表
	hive不認為這張表擁有這份資料,刪除該表,資料不刪除。 擅長做資料共享
	-》建立外部表
		> create external table if not exists emptable(empno int,ename string,job
		 string,mgr int,birthdate string,sal double,comm double,deptno int)
		> row format
		> delimited fields
		> terminated by '\t';
	-》匯入資料
	load data local inpath '/root/emp.txt' into table emp;
	-》查看錶結構
	desc formatted emp;
		Table Type: EXTERNAL_TABLE

	 -》刪除表
	drop table emptable;
	提示:再次建立相同的表 欄位相同 將自動關聯資料!
8)分割槽表`
	 -》建立分割槽表
	hive> create table dept_partitions(depno int,dept string,loc string)
	> partitioned by(day string)
	> row format delimited fields
	> terminated by '\t';

	-》載入資料
	load data local inpath '/root/dept.txt' into table dept_partitions; //出錯
	注意:不能直接匯入,需要指定分割槽
	load data local inpath '/root/dept.txt' into table dept_partitions partition(day='1112');

	-》新增分割槽,不導資料
	alter table dept_partitions add partition(day='1113');

	-》單分割槽查詢
	select * from dept_partitions where day='1112';
	
	-》全查詢
	select * from dept_partitions;
	
	-》查詢表結構
	desc formatted dept_partitions;
	
	-》刪除單個分割槽
	alter table dept_partitions drop partition(day='1112');

9)修改表
	 -》修改表名
	alter table emptable rename to new_table_name;
	-》新增列
	alter table dept_partitions add columns(desc string); 
	-》更新列(重新命名,更改資料型別)
	alter table dept_partitions change column desc desccc int; 
	-》替換(相當於重新定義,全部替換,資料也沒有了
	alter table dept_partitions replace column(desccc int);

DML資料操作
1)向表中載入資料
load data local inpath ‘/root/itstar.txt’ into table terry;

 2)載入hdfs中資料
load data inpath '/terry.txt' into table terry; 提示:相當於剪下

3)覆蓋原有的資料
load data local inpath '/root/itstar.txt' overwrite into table terry;

4)建立分割槽表
create table terry_partitions(id int,name string) partitioned by (month string) row format
delimited fields terminated by '\t';

5)向分割槽表插入資料
insert into table terry_partitions partition(month='201811') values (1,'tongliya');

6)按照條件查詢結果儲存到新表
create table if not exists terry_ppp as select * from terry_partitions where name='tongliya';

7)建立表時載入資料
> create table db_h(id int,name string) 
> row format
> delimited fields
> terminated by "\t"
> location '';

8)查詢結果匯出到本地
	1、
	insert overwrite local directory '/root/datas/yangmi.txt' select * from hh where name='yangmi';
		此時yangmi.txt是資料夾,不是檔案
	2、
	bin/hive -e 'select * from terry ' 
	> /root/terry.txt
		此時terry.txt是檔案
	bin/hive -e "select * from terry where name = 'yangmi' " 
	> /root/terry.txt
	3、
	hive> dfs -get /usr/hive/warehouse/00000 /root;