Hive基本操作

HiveQL是Hive的查询语言,和SQL语言比较类似,对Hive的操作都是通过编写HiveQL语句来实现的,接下来介绍一下Hive中常用的几个基本操作。

首先,启动Hadoop进程。

cd /usr/local/hadoop
./sbin/start-dfs.sh

然后,启动MySQL服务后再执行如下命令启动Hive:

sudo service mysql start
cd /usr/local/hive
./bin/hive

1.创建数据库、表、视图

(1)创建数据库

① 创建数据库hive。

create database hive;

② 创建数据库hive,因为hive已经存在,所以会抛出异常,加上if not exists关键字,则不会抛出异常。

create database if not exists hive;

(2)创建表

① 在hive数据库中,创建表usr,含三个属性id,name,age。

use hive;
create table if not exists usr(id bigint,name string,age int);

在hive数据库中,创建表usr,含三个属性id,name,age,存储路径为“/usr/local/hive/warehouse/hive/usr”。

create table if not exists hive.usr(id bigint,name string,age int) location '/usr/local/hive/warehouse/hive/usr';

③ 在hive数据库中,创建外部表usr_a,含三个属性id,name,age,可以读取路径“/usr/local/data”下以“,”分隔的数据。

create external table if not exists hive.usr_a(id bigint,name string,age int) row format delimited fields terminated by ',' location '/usr/local/data';

④ 在hive数据库中,创建分区表usr_b,含三个属性id,name,age,还存在分区字段sex。

create table hive.usr_b(id bigint,name string,age int) partitioned by(sex boolean);

⑤ 在hive数据库中,创建分区表usr1,它通过复制表usr得到。

use hive;
create table if not exists usr1 like usr;

(3) 创建视图

① 创建视图little_usr,只包含usr表中id,age属性。

create view little_usr as select id,age from usr;

2.修改数据库、表、视图

(1)修改数据库

① 为hive数据库设置dbproperties键值对属性值来描述数据库属性信息。

alter database hive set dbproperties('edited-by'='lily');

(2)修改表

① 重命名表usr为user。

alter table usr rename to usr2;

为了后续实验的进行,将表usr2的名字改回usr。

alter table usr2 rename to usr;

② 为表usr_b增加新分区。

alter table usr_b add if not exists partition(sex=true);
alter table usr_b add if not exists partition(sex=false);

③ 删除表usr_b中分区。

alter table usr_b drop if exists partition(sex=true);

④   在对表usr分区字段之前,增加一个新列sex。

alter table usr add columns(sex boolean);

⑤ 删除表usr中所有字段并重新指定新字段newid,newname,newage。

alter table usr replace columns(newid bigint,newname string,newage int);

⑥ 为usr表设置tblproperties键值对属性值来描述表的属性信息。

alter table usr set tblproperties('notes'='the columns in usr may be null except id');

(3)修改视图

修改little_usr视图元数据中的tblproperties属性信息。

alter view little_usr set tblproperties('create_at'='refer to timestamp');

3.查看数据库、表、视图

(1)查看数据库

① 查看Hive中包含的所有数据库。

show databases;

查看Hive中以h开头的所有数据库。

show databases like 'h.*';

(2)查看表和视图

① 查看数据库hive中所有表和视图。

use hive;
show tables;

② 查看数据库hive中以u开头的所有表和视图。

show tables in hive like 'u.*';

4.描述数据库、表、视图

(1)描述数据库

① 查看数据库hive的基本信息,包括数据库中文件位置信息等。

describe database hive;

② 查看数据库hive的详细信息,包括数据库的基本信息及属性信息等。

describe database extended hive;

(2)描述表和视图

① 查看表usr和视图little_usr的基本信息,包括列信息等。

describe hive.usr;
describe hive.little_usr;

② 查看表usr和视图little_usr的详细信息,包括列信息、位置信息、属性信息等。

describe extended hive.usr;
describe extended hive.little_usr;

5.删除数据库、表、视图

(1)删除表

① 删除表usr,如果是内部表,元数据和实际数据都会被删除;如果是外部表,只删除元数据,不删除实际数据。

drop table if exists usr;

(2)删除视图

① 删除视图little_usr。

drop view if exists little_usr;

(3)删除数据库

① 删除数据库hive,如果不存在会出现警告。

drop database hive;

如果hive数据库中没有表,则这句命令可以直接将hive数据库删除;如果数据库里创建了表,则会向上图一样,删除失败。

② 删除数据库hive,因为有if exists关键字,即使不存在也不会抛出异常。

drop database if exists hive;

删除失败是因为hive数据库非空。

③ 删除数据库hive,加上cascade关键字,可以删除当前数据库和该数据库中的表。

drop database if exists hive cascade;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值