Hive 資料模型

2021-09-20 16:28:55 字數 3045 閱讀 2195

hive 資料模型

hive 資料表有五種型別:內部表,外部表,分割槽表,桶表,檢視表,預設以 tab 分隔

* mysql (oracle) 表預設以逗號分隔,因此,要想匯入 mysql(oracle) 資料,需要設定分隔符,在建表語句後加:

row  format   delimited   fields   terminated  by  ',';

內部表: 相當於 mysql 中的表,將資料儲存到hive 自己的資料倉儲目錄中:/usr/hive/warehouse

例子:create table emp

(empno int,

ename string,

job string,

mgr int,

hiredate string,

sal int,

comm int,

deptno int

);匯入資料到表中:本地、hdfs

load語句、insert語句

load語句相當於ctrl+x

load data inpath '/scott/emp.csv' into table emp;   ----> 匯入hdfs

load data local inpath '/root/temp/***' into table emp;   ----> 匯入本地檔案

建立表,並且指定分隔符

create table emp1

(empno int,

ename string,

job string,

mgr int,

hiredate string,

sal int,

comm int,

deptno int

)row format delimited fields terminated by ',';

建立部門表,儲存部門資料

create table dept

(deptno int,

dname string,

loc string

)row format delimited fields terminated by ',';

load data inpath '/scott/dept.csv' into table dept;

外部表:相對於內部表,資料不在自己的資料倉儲中,只儲存資料的元資訊

例子:(*)實驗的資料

[root@bigdata11 ~]# hdfs dfs -cat /students/student01.txt

1,tom,23

2,mary,24

[root@bigdata11 ~]# hdfs dfs -cat /students/student02.txt

3,mike,26

(*)定義:(1)表結構  (2)指向的路徑

create external table students_ext

(sid int,sname string,age int)

row format delimited fields terminated by ','

location '/students';

分割槽表:將資料按照設定的條件分開儲存,提高查詢效率,分割槽----->  目錄

例子:(*)根據員工的部門號建立分割槽

create table emp_part

(empno int,

ename string,

job string,

mgr int,

hiredate string,

sal int,

comm int

)partitioned by (deptno int)

row format delimited fields terminated by ',';

往分割槽表中匯入資料:指明分割槽

insert into table emp_part partition(deptno=10) select empno,ename,job,mgr,hiredate,sal,comm from emp1 where deptno=10;

insert into table emp_part partition(deptno=20) select empno,ename,job,mgr,hiredate,sal,comm from emp1 where deptno=20;

insert into table emp_part partition(deptno=30) select empno,ename,job,mgr,hiredate,sal,comm from emp1 where deptno=30;

桶  表: 本質上也是一種分割槽表,類似 hash 分割槽   桶 ----> 檔案

例子:建立乙個桶表,按照員工的職位job分桶

create table emp_bucket

(empno int,

ename string,

job string,

mgr int,

hiredate string,

sal int,

comm int,

deptno int

)clustered by (job) into 4 buckets

row format delimited fields terminated by ',';

使用桶表,需要開啟乙個開關

set hive.enforce.bucketing=true;

使用子查詢插入資料

insert into emp_bucket select * from emp1;

檢視表:檢視表是乙個虛表,不儲存資料,用來簡化複雜的查詢

例子:查詢部門名稱、員工的姓名

create view myview

asselect dept.dname,emp1.ename

from emp1,dept

where emp1.deptno=dept.deptno;

select * from myview;

Hive筆記 Hive資料模型

hive資料模型 hive中的資料模型主要分為 除了原始列型別 整數integers,浮點數floating point numbers,泛型字串generic strings,日期和布林值dates and booleans 之外,hive還支援陣列 array 和對映 map 此外,使用者可以基...

HIVE的資料模型

hive 中所有的資料都儲存在 hdfs 中,hive 中包含以下資料模型 示例 建立表 level string,leader string,dep string,ips array row format delimited fields terminated by collection item...

Hive資料模型基礎

hive將為每個資料庫建立乙個目錄。hdfs上建立乙個與之對應的目錄 該資料庫中的表將儲存在資料庫目錄的子目錄中。例外情況是預設資料庫中的表,它沒有自己的目錄。2 直接輸入 一.資料庫的基本操作 1.預設路徑 資料庫目錄是在屬性hive.metastore.warehouse.dir指定的頂級目錄下...