Hive入門(2)
1.Hive DDL
1.1 database DDL
(1) 建立資料庫
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name //生產中if not exists都帶上
[COMMENT database_comment] //可以增加註釋
[LOCATION hdfs_path] //存的路徑不寫預設的就是/user/hive/warehouse
[WITH DBPROPERTIES (property_name=property_value,
練習:
create database if not exists hive2 comment 'this is test databaes' with DBPROPERTIES("cretor"='yunfei','teacher'='ruoze');
desc database extended hive2;
//不加這個extended看不到自己新增的屬性
(2)刪除資料庫
use database hive2; drop table xxx; drop database hive2; //這是生產中的正常操作,因為資料庫下有表是不能刪除的 強制的不學
(3)修改資料庫
ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...); -- (Note: SCHEMA added in Hive 0.14.0) ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role; -- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0) ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path; -- (Note: Hive 2.2.1, 2.4.0 and later) hive> alter database hive2 set dbproperties('teacher'='jepson'); OK Time taken: 0.076 seconds hive> desc database extended hive2; OK hive2 this is test databaes hdfs://192.168.137.251:9000/user/hive/warehouse/hive2.db root USER {teacher=jepson, cretor=yunfei} Time taken: 0.021 seconds, Fetched: 1 row(s)
1.2 table DDL
(1)建立表
create table hive2.ruozeperson (id int comment 'this is id',name string comment 'this is name') comment 'this is test table' row format delimited fields terminated by '\t';
create table person(id int,name string) comment 'this is a table' row format delimited fields terminated by '\t'
(2)查看錶的詳細資訊
desc formatted ruozeperson;
(3)載入資料到表裡面去
LOAD DATA LOCAL INPATH '/home/hadoop/data/emp.txt' OVERWRITE INTO TABLE ruozedata_emp;
local: 從本地檔案系統載入資料到hive表
非local:從HDFS檔案系統載入資料到hive表
LOAD DATA INPATH 'hdfs:192xxx/home/hadoop/data/emp.txt' OVERWRITE INTO TABLE ruozedata_emp;
OVERWRITE: 載入資料到表的時候資料的處理方式,覆蓋
非OVERWRITE:追加
(4)第二種建立表的方式
create table emp_test as select * from emp;
這是將整個表複製了一遍
create table emp_test like emp;
這種只複製表的結構!!
(5)改表名
alter table emp_test3 rename to emp_test4;
(6)修改表的欄位名和型別
alter table emp_test change column at empno string;
將at 列改為empno string型別
(7)快速檢視建立表的資訊
show create table emp;
倆個都是查看錶的結構,但上面這個能快速看錶是如何建立的。
desc formatted emp;
(8)第三種建立表的方式
create table ruozedata_emp4 like ruozedata_emp;
INSERT OVERWRITE TABLE ruozedata_emp4
select * FROM ruozedata_emp;
overwrite 是覆蓋, to 是追加
(9)到處Hive處理的資料
這是匯入到本地,匯入HDFS換成HDFS的路徑就好了
insert overwrite local directory '/home/hadoop/data' row format delimited fields terminated by '\t' select * from emp;
2.Hive的資料儲存在哪?
分倆部分:
(1)真是資料存放在HDFS之上。
(2)元資料存放在RDBMS之上。
3.Hive的資料儲存結構
(1)傳統的關係型資料庫,有database,但是Hive中的database,在HDFS之上,就是HDFS之上的一個頂層資料夾。
(2)傳統的關係型資料庫,有tables,Hive中的table就相當於第二層資料夾。
(3)Hive中還有一個更小的單位Partitions,一個表可以有多個partition,partition就是存放在資料夾之下。
(4)最後就是bucket,這是最底層的了,這就是具體的檔案資料了。
4.Hive中常用的資料型別
(1)int bigint long float string double boolean(這個也不建議用,可以用01表示更加省位元組) decimal(精度非常高的,很少用)
(2)常用的分隔符
行:\n
列:\001 tab ,空格,$$$ 就這些常用的
5.內外部表的不同
建立表預設使用的是MANAGED_TABLE:內部表
ruozedata_emp_managed
drop:hdfs+meta
EXTERNAL:外部表
create EXTERNAL table ruozedata_emp_external
(empno int, ename string, job string, mgr int, hiredate string, salary double, comm double, deptno int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LOCATION "/ruozedata/external/emp" ;
drop: drop meta
內部表刪除表會將hdfs上的資料和MySQL中的meta資訊都刪除,外部表則只刪除meta資訊。
6.操作出了問題,例如欄位名字反了
只有重跑,別想著花裡胡哨的回滾操作。
重跑:必須要要保證冪等性(就是執行n次結果是一樣的!)