HiveQL命令行的使用

實(shí)驗(yàn)步驟

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

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

(1)進(jìn)入HiveQL苫幢,命令如下:

hive

image

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

命令:

create database userdb;

image

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

命令:

show databases;

image

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

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

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

use userdb;

image

再根據(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>

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ù)庫不為空則會(huì)報(bào)錯(cuò)。如下圖所示:

drop database if exists userdb1;

image

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

drop database if exists userdb;

image

此時(shí)板丽,再查看數(shù)據(jù)庫則會(huì)看到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)導(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>

image

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

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

image

②. 將表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>

image

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

cat /home/Test/sample/000000_0

image

③. 將表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>

image

查看數(shù)據(jù)是否導(dǎo)入到表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

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

select * from exter_emp;

image

④. 刪除外部表exter_emp:

drop table exter_emp;

image

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

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表中的薪水?dāng)?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

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

select name,subordinates[0] from employees;

image

⑤. 選擇一個(gè)deductions元素進(jìn)行查詢

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

image

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

image

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

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

image

③. 將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;

image

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

查看薪資超過30000的員工

select * from employees2 where salary > 30000;

image

(5)GROUP BY 語句的使用

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

?

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

image

(6)ORDER BY語句的使用

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

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

image

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

image

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

image

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

image

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


image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末温赔,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子鬼癣,更是在濱河造成了極大的恐慌陶贼,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件待秃,死亡現(xiàn)場離奇詭異骇窍,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)锥余,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進(jìn)店門腹纳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事嘲恍∽慊” “怎么了?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵佃牛,是天一觀的道長淹辞。 經(jīng)常有香客問我,道長俘侠,這世上最難降的妖魔是什么象缀? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮爷速,結(jié)果婚禮上央星,老公的妹妹穿的比我還像新娘。我一直安慰自己惫东,他們只是感情好莉给,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著廉沮,像睡著了一般颓遏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上滞时,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天叁幢,我揣著相機(jī)與錄音,去河邊找鬼坪稽。 笑死曼玩,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的刽漂。 我是一名探鬼主播演训,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贝咙!你這毒婦竟也來了样悟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤庭猩,失蹤者是張志新(化名)和其女友劉穎窟她,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蔼水,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡震糖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了趴腋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吊说。...
    茶點(diǎn)故事閱讀 38,100評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡论咏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出颁井,到底是詐尸還是另有隱情厅贪,我是刑警寧澤,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布雅宾,位于F島的核電站养涮,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏眉抬。R本人自食惡果不足惜贯吓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蜀变。 院中可真熱鬧悄谐,春花似錦、人聲如沸昏苏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贤惯。三九已至,卻和暖如春棒掠,著一層夾襖步出監(jiān)牢的瞬間孵构,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工烟很, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留颈墅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓雾袱,卻偏偏與公主長得像恤筛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子芹橡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評論 2 345

推薦閱讀更多精彩內(nèi)容