實(shí)驗(yàn)步驟
注意:由于數(shù)據(jù)庫對大小寫不敏感退疫,因此大寫和小寫都可以使用。
1. HiveQL:數(shù)據(jù)操作
(1)進(jìn)入HiveQL苫幢,命令如下:
hive
(2)Hive創(chuàng)建數(shù)據(jù)庫:創(chuàng)建一個(gè)名為userdb的數(shù)據(jù)庫
命令:
create database userdb;
隨時(shí)可以通過show命令來查看Hive中所包含的數(shù)據(jù)庫:
命令:
show databases;
Hive會(huì)為每個(gè)數(shù)據(jù)庫創(chuàng)建一個(gè)目錄职员。數(shù)據(jù)庫中的表將會(huì)以這個(gè)數(shù)據(jù)庫目錄的子目錄形式存儲。
(3)Hive創(chuàng)建表
首先需要使用use命令使用數(shù)據(jù)庫userdb:
use userdb;
再根據(jù)相應(yīng)要求在userdb數(shù)據(jù)庫中創(chuàng)建表(僅僅是創(chuàng)建表結(jié)構(gòu)赊琳,表中并沒有內(nèi)容):
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
create table if not exists employee ( id int, name String, salary String, designation String)
comment 'employee1 details'
row format delimited
fields terminated BY '\t'
lines terminated BY '\n'
stored as textfile;
</pre>
(4)Hive加載數(shù)據(jù):向建的表employee中加載數(shù)據(jù)
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n65" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
load data local inpath '/opt/sample.txt'
overwrite into table employee;
</pre>
(5)查看數(shù)據(jù)是否加載成功
select * from employee;
(5)Hive修改表
①. 重命名表街夭,將表名由employee修改為emp
alter table employee rename to emp;
查看名稱是否正確修改:
show tables;
②. 刪除表emp
drop table emp;
查看是否刪除成功:
show tables;
(6)刪除數(shù)據(jù)庫
①. 若刪除的數(shù)據(jù)庫不為空則會(huì)報(bào)錯(cuò)。如下圖所示:
drop database if exists userdb1;
②. 若刪除的數(shù)據(jù)庫為空則可以成功刪除躏筏,如下圖所示:
drop database if exists userdb;
此時(shí)板丽,再查看數(shù)據(jù)庫則會(huì)看到userdb1沒有被刪除,而userdb已被刪除。
show databases;
(7)使用數(shù)據(jù)庫userdb1埃碱,接下來所有操作均在此數(shù)據(jù)庫中執(zhí)行猖辫。
use userdb1;
(8)Hive分區(qū)
我們已經(jīng)有一張分區(qū)表,名為employee1砚殿,下面為這張表添加分區(qū)啃憎。
①. 添加分區(qū)
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n132" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
alter table employee1
add partition (year='2012') location '/opt/empdata/2012/part2012.txt'
partition (year='2013') location '/opt/empdata/2013/part2013.txt';
</pre>
②. 查看分區(qū):
show partitions employee1;
③. 寫入數(shù)據(jù):
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n143" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
load data local inpath '/opt/empdata/2013/file3'
into table employee1 partition(year='2013');
load data local inpath '/opt/empdata/2012/file2'
into table employee1 partition(year='2012');
</pre>
(9)導(dǎo)出數(shù)據(jù)
①. 將表employee1中的數(shù)據(jù)導(dǎo)出到HDFS
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n150" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
insert overwrite directory '/home/Test/hdfs'
row format delimited
fields terminated by '\t'
select * from employee1;
</pre>
查看(注意:在集群中查看,需要先退出hiveQL似炎,輸入quit;命令來退出數(shù)據(jù)庫到root用戶下執(zhí)行查詢命令)
hadoop fs -cat /home/Test/hdfs/000000_0
②. 將表employee1中的數(shù)據(jù)導(dǎo)出到本地文件系統(tǒng)(注意:在HiveQL中操作辛萍,需要先進(jìn)入hiveQL,輸入命令hive羡藐。每次重新進(jìn)入hiveQL都要使用“use userdb1;”命令來指定需要操作的數(shù)據(jù)庫贩毕。)
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n161" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
insert overwrite local directory '/home/Test/sample'
row format delimited
fields terminated by '\t'
select * from employee1;
</pre>
查看(注意:在集群中查看,需要先退出hiveQL仆嗦,輸入quit;命令來退出數(shù)據(jù)庫到root用戶下執(zhí)行查詢命令)
cat /home/Test/sample/000000_0
③. 將表employee1中的數(shù)據(jù)導(dǎo)出到已經(jīng)存在的表employee2(注意:在HiveQL中操作辉阶,需要先進(jìn)入hiveQL,輸入命令hive瘩扼。每次重新進(jìn)入hiveQL都要使用“use userdb1;”命令來指定需要操作的數(shù)據(jù)庫睛藻。)
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n172" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
insert into table employee2
select * from employee1;
</pre>
查看數(shù)據(jù)是否導(dǎo)入到表employee2中:
select * from employee2;
④. 給分區(qū)重命名,并且查看重命名是否成功:
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n182" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
alter table employee1 partition (year='2013')
rename to partition (year='2014');
</pre>
show partitions employee1;
⑤. 刪除分區(qū)邢隧,并且查看分區(qū)是否刪除成功:
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n191" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
alter table employee1 drop
partition (year='2014');
</pre>
show partitions employee1;
[圖片上傳中...(image-78082e-1509677357636-21)]
(10)外部表的基本操作
①. 創(chuàng)建外部表店印,表名為exter_emp:
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n202" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
create external table exter_emp( id int, name String, age int, tel String)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile
location '/home/Test/External';
</pre>
[圖片上傳中...(image-c08f8d-1509677357636-20)]
②. 向外部表中加載數(shù)據(jù):
load data local inpath '/opt/empdata/file4' into table exter_emp;
③. 查看表中是否成功導(dǎo)入數(shù)據(jù):
select * from exter_emp;
④. 刪除外部表exter_emp:
drop table exter_emp;
⑤. 查看文件本體依然存在:刪除外部表的時(shí)候,Hive僅僅刪除外部表的元數(shù)據(jù)倒慧,數(shù)據(jù)是不會(huì)刪除的按摘。
dfs -ls /home/Test/External;
2. HiveQL查詢操作
(1)創(chuàng)建外部表employees
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n232" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
create external table employees(eid int,name String,salary float,age int,birthday date,
subordinates array<String>,
deductions map<String,float>,
address struct<street:String,city:String,state:String>)
row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
location '/home/Test/external-1';
</pre>
向外部表employees中加載數(shù)據(jù):
load data local inpath '/opt/empdata/file5' into table employees;
(2)SELECT...FROM語句的使用
①. 查看employees表中的薪水?dāng)?shù)據(jù)
select name,salary from employees;
②. 查看employees表中的下屬數(shù)據(jù)信息
select name,subordinates from employees;
③. 查看employees表中的扣除稅數(shù)據(jù)
select name,deductions from employees;
④. 引用復(fù)合字段查看下級數(shù)據(jù)
select name,subordinates[0] from employees;
⑤. 選擇一個(gè)deductions元素進(jìn)行查詢
select name,deductions["FedTax"] from employees;
(3)使用列值計(jì)算
①. 把查詢得到的員工姓名轉(zhuǎn)換為大寫并計(jì)算員工的稅后薪資
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n277" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
select upper(name),salary,deductions["FedTax"],
round(salary*(1-deductions["FedTax"])) from employees;
</pre>
②. 內(nèi)置函數(shù)查詢表employees中有多少員工,以及計(jì)算員工平均薪水
select count(*),avg(salary) from employees;
③. 將employees表中每行記錄中的subordinates字段內(nèi)容轉(zhuǎn)換成0個(gè)或者多個(gè)新的記錄行纫谅。如果某行員工記錄subordinates字段內(nèi)容為空的話炫贤,就不會(huì)產(chǎn)生記錄;如果不為空付秕,那么這個(gè)數(shù)組的每個(gè)元素都將產(chǎn)生一行新記錄:
select explode(subordinates) as sub from employees;
(4)SELECT...WHERE語句的使用
查看薪資超過30000的員工
select * from employees2 where salary > 30000;
(5)GROUP BY 語句的使用
查詢獲取每個(gè)部門的員工人數(shù)的查詢語句如下:
?
select dept,count(*) from employees2 group by dept;
(6)ORDER BY語句的使用
獲取員工的詳細(xì)信息兰珍,并把結(jié)果按照部門名稱排序:
select id,name, dept from employees2 order by dept;
(7)JOIN語句的使用
①. 下面的查詢對customers和orders進(jìn)行連接,找出每個(gè)客戶下的訂單信息询吴。連接的條件是customers表中的id必須與orders表中的customer_id相同掠河。這個(gè)JOIN操作實(shí)際上就是獲取每個(gè)下了訂單的客戶的訂單情況。如果某個(gè)客戶沒有下過任何訂單猛计,那么該客戶的信息將不會(huì)返回唠摹。
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n320" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
select c.id, c.name, c.age, o.amount
from customers c join orders o
on (c.id = o.customer_id);
②. HiveQL LEFT OUTER JOIN(左外連接)返回所有行左表,即使是在右邊的表中沒有匹配奉瘤。這意味著勾拉,如果ON子句匹配的右表0(零)記錄,JOIN還是返回結(jié)果行,但在右表中的每一列的值為NULL藕赞。這個(gè)LEFT OUTER JOIN操作實(shí)際上就是獲取每個(gè)客戶的訂單情況成肘,不管客戶是不是下過訂單,該客戶的信息都將會(huì)返回斧蜕。通過這個(gè)操作双霍,用戶可以了解到整個(gè)客戶的情況,包括下過訂單的客戶和沒有下過訂單的客戶惩激。
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n325" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
select c.id, c.name, o.amount, o.time
from customers c
left outer join orders o
on (c.id = o.customer_id);
③. HiveQL RIGHT OUTER JOIN(右外連接)返回右邊表的所有行店煞,即使在左表中沒有匹配蟹演。如果ON子句的左表匹配0(零)的記錄风钻,JOIN結(jié)果返回一行,但在左表中的每一列為NULL酒请。這個(gè)RIGHT OUTER JOIN操作實(shí)際上就是獲取所有訂單的訂單和下單客戶的情況骡技,即使某個(gè)訂單沒有下單客戶ID,也會(huì)返回該訂單的情況羞反,有關(guān)客戶信息項(xiàng)將為NULL.
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n330" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
select c.id, c.name, o.amount, o.time
from customers c
right outer join orders o
on (c.id = o.customer_id);
④. HiveQL FULL OUTER JOIN(完全外連接)將會(huì)返回兩個(gè)表的所有記錄布朦。如果任一表的指定字段沒有符合條件的值的話,那么就使用NULL值替代昼窗。這個(gè)FULL OUTER JOIN操作實(shí)際上就是首先進(jìn)行LEFT OUTER JOIN是趴,再進(jìn)行RIGHT OUTER JOIN。也就是說澄惊,首先獲取所有客戶的訂單信息唆途,如果沒有訂單,相應(yīng)的訂單信息項(xiàng)就返回NULL掸驱;然后再獲取所有訂單的客戶信息肛搬,如果沒有客戶,那么有關(guān)客戶信息項(xiàng)將為NULL毕贼。
<pre class="md-fences md-end-block" lang="" contenteditable="false" cid="n335" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: pre; display: block; break-inside: avoid; text-align: left; background-image: ; background-position: var(--code-block-bg-color); background-size: ; background-repeat: var(--code-block-bg-color); background-attachment: ; background-origin: ; background-clip: ; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(221, 221, 221); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
select c.id, c.name, o.amount, o.time
from customers c
full outer join orders o
on (c.id = o.customer_id);
</pre>