HiveQL命令行的使用

实验步骤

注意:由于数据库对大小写不敏感,因此大写和小写都可以使用。

1. HiveQL:数据操作

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

hive

《HiveQL命令行的使用》 image

(2)Hive创建数据库:创建一个名为userdb的数据库

命令:

create database userdb;

《HiveQL命令行的使用》 image

随时可以通过show命令来查看Hive中所包含的数据库:

命令:

show databases;

《HiveQL命令行的使用》 image

Hive会为每个数据库创建一个目录。数据库中的表将会以这个数据库目录的子目录形式存储。

(3)Hive创建表

首先需要使用use命令使用数据库userdb:

use userdb;

《HiveQL命令行的使用》 image

再根据相应要求在userdb数据库中创建表(仅仅是创建表结构,表中并没有内容):

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

《HiveQL命令行的使用》 image

(4)Hive加载数据:向建的表employee中加载数据

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

《HiveQL命令行的使用》 image

(5)查看数据是否加载成功

select * from employee;

《HiveQL命令行的使用》 image

(5)Hive修改表

①. 重命名表,将表名由employee修改为emp

alter table employee rename to emp;

《HiveQL命令行的使用》 image

查看名称是否正确修改:

show tables;

《HiveQL命令行的使用》 image

②. 删除表emp

drop table emp;

《HiveQL命令行的使用》 image

查看是否删除成功:

show tables;

《HiveQL命令行的使用》 image

(6)删除数据库

①. 若删除的数据库不为空则会报错。如下图所示:

drop database if exists userdb1;

《HiveQL命令行的使用》 image

②. 若删除的数据库为空则可以成功删除,如下图所示:

drop database if exists userdb;

《HiveQL命令行的使用》 image

此时,再查看数据库则会看到userdb1没有被删除,而userdb已被删除。

show databases;

《HiveQL命令行的使用》 image

(7)使用数据库userdb1,接下来所有操作均在此数据库中执行。

use userdb1;

《HiveQL命令行的使用》 image

(8)Hive分区

我们已经有一张分区表,名为employee1,下面为这张表添加分区。

①. 添加分区

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

《HiveQL命令行的使用》 image

②. 查看分区:

show partitions employee1;

《HiveQL命令行的使用》 image

③. 写入数据:

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

《HiveQL命令行的使用》 image

(9)导出数据

①. 将表employee1中的数据导出到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命令行的使用》 image

查看(注意:在集群中查看,需要先退出hiveQL,输入quit;命令来退出数据库到root用户下执行查询命令)

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

《HiveQL命令行的使用》 image

②. 将表employee1中的数据导出到本地文件系统(注意:在HiveQL中操作,需要先进入hiveQL,输入命令hive。每次重新进入hiveQL都要使用“use userdb1;”命令来指定需要操作的数据库。)

<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命令行的使用》 image

查看(注意:在集群中查看,需要先退出hiveQL,输入quit;命令来退出数据库到root用户下执行查询命令)

cat /home/Test/sample/000000_0

《HiveQL命令行的使用》 image

③. 将表employee1中的数据导出到已经存在的表employee2(注意:在HiveQL中操作,需要先进入hiveQL,输入命令hive。每次重新进入hiveQL都要使用“use userdb1;”命令来指定需要操作的数据库。)

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

《HiveQL命令行的使用》 image

查看数据是否导入到表employee2中:

select * from employee2;

《HiveQL命令行的使用》 image

④. 给分区重命名,并且查看重命名是否成功:

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

《HiveQL命令行的使用》 image

show partitions employee1;

《HiveQL命令行的使用》 image

⑤. 删除分区,并且查看分区是否删除成功:

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

《HiveQL命令行的使用》 image

show partitions employee1;

[图片上传中…(image-78082e-1509677357636-21)]

(10)外部表的基本操作

①. 创建外部表,表名为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)]

②. 向外部表中加载数据:

load data local inpath '/opt/empdata/file4' into table exter_emp;

《HiveQL命令行的使用》 image

③. 查看表中是否成功导入数据:

select * from exter_emp;

《HiveQL命令行的使用》 image

④. 删除外部表exter_emp:

drop table exter_emp;

《HiveQL命令行的使用》 image

⑤. 查看文件本体依然存在:删除外部表的时候,Hive仅仅删除外部表的元数据,数据是不会删除的。

dfs -ls /home/Test/External;

《HiveQL命令行的使用》 image

2. HiveQL查询操作

(1)创建外部表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>

《HiveQL命令行的使用》 image

向外部表employees中加载数据:

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

《HiveQL命令行的使用》 image

(2)SELECT…FROM语句的使用

①. 查看employees表中的薪水数据

select name,salary from employees;

《HiveQL命令行的使用》 image

②. 查看employees表中的下属数据信息

select name,subordinates from employees;

《HiveQL命令行的使用》 image

③. 查看employees表中的扣除税数据

select name,deductions from employees;

《HiveQL命令行的使用》 image

④. 引用复合字段查看下级数据

select name,subordinates[0] from employees;

《HiveQL命令行的使用》 image

⑤. 选择一个deductions元素进行查询

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

《HiveQL命令行的使用》 image

(3)使用列值计算

①. 把查询得到的员工姓名转换为大写并计算员工的税后薪资

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

《HiveQL命令行的使用》 image

②. 内置函数查询表employees中有多少员工,以及计算员工平均薪水

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

《HiveQL命令行的使用》 image

③. 将employees表中每行记录中的subordinates字段内容转换成0个或者多个新的记录行。如果某行员工记录subordinates字段内容为空的话,就不会产生记录;如果不为空,那么这个数组的每个元素都将产生一行新记录:

select explode(subordinates) as sub from employees;

《HiveQL命令行的使用》 image

(4)SELECT…WHERE语句的使用

查看薪资超过30000的员工

select * from employees2 where salary > 30000;

《HiveQL命令行的使用》 image

(5)GROUP BY 语句的使用

查询获取每个部门的员工人数的查询语句如下:

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

《HiveQL命令行的使用》 image

(6)ORDER BY语句的使用

获取员工的详细信息,并把结果按照部门名称排序:

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

《HiveQL命令行的使用》 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);

《HiveQL命令行的使用》 image

②. HiveQL LEFT OUTER JOIN(左外连接)返回所有行左表,即使是在右边的表中没有匹配。这意味着,如果ON子句匹配的右表0(零)记录,JOIN还是返回结果行,但在右表中的每一列的值为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);

《HiveQL命令行的使用》 image

③. HiveQL RIGHT OUTER JOIN(右外连接)返回右边表的所有行,即使在左表中没有匹配。如果ON子句的左表匹配0(零)的记录,JOIN结果返回一行,但在左表中的每一列为NULL。这个RIGHT OUTER JOIN操作实际上就是获取所有订单的订单和下单客户的情况,即使某个订单没有下单客户ID,也会返回该订单的情况,有关客户信息项将为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命令行的使用》 image

④. HiveQL FULL OUTER JOIN(完全外连接)将会返回两个表的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。这个FULL OUTER JOIN操作实际上就是首先进行LEFT OUTER JOIN,再进行RIGHT OUTER JOIN。也就是说,首先获取所有客户的订单信息,如果没有订单,相应的订单信息项就返回NULL;然后再获取所有订单的客户信息,如果没有客户,那么有关客户信息项将为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>

《HiveQL命令行的使用》 image

    原文作者:一花一世界30951091
    原文地址: https://www.jianshu.com/p/7f88d0c5f63b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞