HiveQL命令行的使用

實驗步驟

注意:由于數(shù)據(jù)庫對大小寫不敏感,因此大寫和小寫都可以使用。

1. HiveQL:數(shù)據(jù)操作

(1)進入HiveQL,命令如下:

hive

image

(2)Hive創(chuàng)建數(shù)據(jù)庫:創(chuàng)建一個名為userdb的數(shù)據(jù)庫

命令:

create database userdb;

image

隨時可以通過show命令來查看Hive中所包含的數(shù)據(jù)庫:

命令:

show databases;

image

Hive會為每個數(shù)據(jù)庫創(chuàng)建一個目錄。數(shù)據(jù)庫中的表將會以這個數(shù)據(jù)庫目錄的子目錄形式存儲。

(3)Hive創(chuàng)建表

首先需要使用use命令使用數(shù)據(jù)庫userdb:

use userdb;

image

再根據(jù)相應要求在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>

image

(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>

image

(5)查看數(shù)據(jù)是否加載成功

select * from employee;

image

(5)Hive修改表

①. 重命名表,將表名由employee修改為emp

alter table employee rename to emp;

image

查看名稱是否正確修改:

show tables;

image

②. 刪除表emp

drop table emp;

image

查看是否刪除成功:

show tables;

image

(6)刪除數(shù)據(jù)庫

①. 若刪除的數(shù)據(jù)庫不為空則會報錯。如下圖所示:

drop database if exists userdb1;

image

②. 若刪除的數(shù)據(jù)庫為空則可以成功刪除,如下圖所示:

drop database if exists userdb;

image

此時,再查看數(shù)據(jù)庫則會看到userdb1沒有被刪除,而userdb已被刪除。

show databases;

image

(7)使用數(shù)據(jù)庫userdb1,接下來所有操作均在此數(shù)據(jù)庫中執(zhí)行。

use userdb1;

image

(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>

image

②. 查看分區(qū):

show partitions employee1;

image

③. 寫入數(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>

image

(9)導出數(shù)據(jù)

①. 將表employee1中的數(shù)據(jù)導出到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>

image

查看(注意:在集群中查看,需要先退出hiveQL,輸入quit;命令來退出數(shù)據(jù)庫到root用戶下執(zhí)行查詢命令)

hadoop fs -cat /home/Test/hdfs/000000_0

image

②. 將表employee1中的數(shù)據(jù)導出到本地文件系統(tǒng)(注意:在HiveQL中操作,需要先進入hiveQL,輸入命令hive。每次重新進入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>

image

查看(注意:在集群中查看,需要先退出hiveQL,輸入quit;命令來退出數(shù)據(jù)庫到root用戶下執(zhí)行查詢命令)

cat /home/Test/sample/000000_0

image

③. 將表employee1中的數(shù)據(jù)導出到已經(jīng)存在的表employee2(注意:在HiveQL中操作,需要先進入hiveQL,輸入命令hive。每次重新進入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>

image

查看數(shù)據(jù)是否導入到表employee2中:

select * from employee2;

image

④. 給分區(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>

image

show partitions employee1;

image

⑤. 刪除分區(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>

image

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;

image

③. 查看表中是否成功導入數(shù)據(jù):

select * from exter_emp;

image

④. 刪除外部表exter_emp:

drop table exter_emp;

image

⑤. 查看文件本體依然存在:刪除外部表的時候,Hive僅僅刪除外部表的元數(shù)據(jù),數(shù)據(jù)是不會刪除的。

dfs -ls /home/Test/External;

image

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>

image

向外部表employees中加載數(shù)據(jù):

load data local inpath '/opt/empdata/file5' into table employees;

image

(2)SELECT...FROM語句的使用

①. 查看employees表中的薪水數(shù)據(jù)

select name,salary from employees;

image

②. 查看employees表中的下屬數(shù)據(jù)信息

select name,subordinates from employees;

image

③. 查看employees表中的扣除稅數(shù)據(jù)

select name,deductions from employees;

image

④. 引用復合字段查看下級數(shù)據(jù)

select name,subordinates[0] from employees;

image

⑤. 選擇一個deductions元素進行查詢

select name,deductions["FedTax"] from employees;

image

(3)使用列值計算

①. 把查詢得到的員工姓名轉(zhuǎn)換為大寫并計算員工的稅后薪資

<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>

image

②. 內(nèi)置函數(shù)查詢表employees中有多少員工,以及計算員工平均薪水

select count(*),avg(salary) from employees;

image

③. 將employees表中每行記錄中的subordinates字段內(nèi)容轉(zhuǎn)換成0個或者多個新的記錄行。如果某行員工記錄subordinates字段內(nèi)容為空的話,就不會產(chǎn)生記錄;如果不為空,那么這個數(shù)組的每個元素都將產(chǎn)生一行新記錄:

select explode(subordinates) as sub from employees;

image

(4)SELECT...WHERE語句的使用

查看薪資超過30000的員工

select * from employees2 where salary > 30000;

image

(5)GROUP BY 語句的使用

查詢獲取每個部門的員工人數(shù)的查詢語句如下:

?

select dept,count(*) from employees2 group by dept;

image

(6)ORDER BY語句的使用

獲取員工的詳細信息,并把結(jié)果按照部門名稱排序:

select id,name, dept from employees2 order by dept;

image

(7)JOIN語句的使用

①. 下面的查詢對customers和orders進行連接,找出每個客戶下的訂單信息。連接的條件是customers表中的id必須與orders表中的customer_id相同。這個JOIN操作實際上就是獲取每個下了訂單的客戶的訂單情況。如果某個客戶沒有下過任何訂單,那么該客戶的信息將不會返回。

<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);

image

②. HiveQL LEFT OUTER JOIN(左外連接)返回所有行左表,即使是在右邊的表中沒有匹配。這意味著,如果ON子句匹配的右表0(零)記錄,JOIN還是返回結(jié)果行,但在右表中的每一列的值為NULL。這個LEFT OUTER JOIN操作實際上就是獲取每個客戶的訂單情況,不管客戶是不是下過訂單,該客戶的信息都將會返回。通過這個操作,用戶可以了解到整個客戶的情況,包括下過訂單的客戶和沒有下過訂單的客戶。

<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);

image

③. HiveQL RIGHT OUTER JOIN(右外連接)返回右邊表的所有行,即使在左表中沒有匹配。如果ON子句的左表匹配0(零)的記錄,JOIN結(jié)果返回一行,但在左表中的每一列為NULL。這個RIGHT OUTER JOIN操作實際上就是獲取所有訂單的訂單和下單客戶的情況,即使某個訂單沒有下單客戶ID,也會返回該訂單的情況,有關(guān)客戶信息項將為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);

image

④. HiveQL FULL OUTER JOIN(完全外連接)將會返回兩個表的所有記錄。如果任一表的指定字段沒有符合條件的值的話,那么就使用NULL值替代。這個FULL OUTER JOIN操作實際上就是首先進行LEFT OUTER JOIN,再進行RIGHT OUTER JOIN。也就是說,首先獲取所有客戶的訂單信息,如果沒有訂單,相應的訂單信息項就返回NULL;然后再獲取所有訂單的客戶信息,如果沒有客戶,那么有關(guān)客戶信息項將為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>


image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容