Hive基本操作
HiveQL是Hive的查询语言,和SQL比较相似,对Hive的操作都是通过编写HiveQL语句来实现的。
基本操作
1、create:创建数据库、表、视图
(1)创建数据库
①创建数据库 hive:
hive>create database hive;
②创建数据库hive,因为hive 已经存在,所以会批出异常,加上if not exists 关键字,则不会拋出异常:
hive>create database if not exists hive;
(2)创建表
① 在hive 数据库中,创建usr 表,含3个属性 id、name 和 age:
hive>use hive;
hive>create table if not exists usr(id bigint, name string, age int);
② 在hive 数据库中,创建 usr 表,含3个属性 id、name 和 age,存储路径为 “usr/ocal/hive/warehouse/hive/usr":
hive>create table if not exists hive.usr(id bigint, name string, age int)
>location'/usr/local/hive/warehouse/hive/usr';
③ 在hive 数据库中,创建外部 ust 表,含3个属性 id、name 和 age,可以读取路径“/usr/local/data”下以“,”分隔的数据:
hive>create external table if not exists hive.usr(id bigint, name string, age int)
>row format delimited fields terminated by ','
>Location'/usr/local/data";
④ 在hive 数据库中,创建分区 usr 表,含3个属性 id、name 和 age,还存在分区字段sex:
hive>create table hive.usr(id bigint, name string, age int) partitioned by (sex boolean);
⑤ 在hive 数据库中,创建分区 usr1 表,它通过复制 usr 表得到:
hive>use hive;
hive>create table if not exists usr1 like usr;
(3)创建视图
创建视图 little_usr,只包含表usr 中id 和 age 属性:
hive>create view little_usr as select id, age from usr;
2、drop:删除数据库、表、视图
(1)删除数据库
①删除数据库 hive,如果不存在会出现警告:
hive>drop database hive;
②删除数据库 hive,因为有 if exists 关键字,即使不存在也不会抛出异常:
hive>drop database if exists hive;
③删除数据库 hive,加上 cascade 关键宇,可以删除当前数据库和该数据库中的表:
hive>drop database if exists hive cascade;
(2)删除表
删除 usr 表,如果是内部表,元数据和实际数据都会被删除;如果是外部表,则只删除元数据,不删除实际数据:
hive>drop table if exists usr;
(3)删除视图
删除视图 little_ usr:
hive>drop view if exists little_usr;
3、alter:修改数据库、表、视图
(1)修改数据库
为 hive 数据库设置 dbproperties 键值对属性值来描述数据库属性信息:
hive>alter database hive set dbproperties ('edited-by'='lily');
(2)修改表
①重命名usr 表为 user :
hive>alter table usr rename to user;
② 为 usr 表增加新分区:
hive>alter table usr add if not exists partition(sex=true);
hive>alter table usr add if not exists partition(sex=false);
③删除 usr 表中分区:
hive>alter table usr drop if exists partition(sex=true);
④ 把 usr 表中列名 name 修改为 username,并把该列置于age列后:
hive>alter table usr change name username string after age;
⑤ 在对 usr 表分区字段之前,增加一个新列 sex:
hive>alter table usr add columns (sex boolean);
⑥删除 usr 表中所有字段并重新指定新字段 newid、newname 和 newage:
hive>alter table usr replace columns(newid bigint, newname string, newage int);
⑦为 ust 表设置tblproperties 键值对属性值来描述表的属性信息
hive>alter table usr set tblproperties('notes'='the columns in usr may be null except id');
(3)修改视图
修改 little_usr 视图元数据中的tblproperties 属性信息:
hive>alter view little_usr set tblproperties('create_at'='refer to timestamp');
4、show:查看数据库、表、视图
(1)查看数据库
① 查看 Hive 中包含的所有数据库:
hive>show databases;
② 查看 Hive 中以h开头的所有数据库:
hive>show databases 1ike'h.*';
(2)查看表和视图
① 查看数据库 hive 中所有表和视图:
hive>use hive;
hive>show tables;
② 查看数据库 hive 中以u开头的所有表和视图:
hive>show tables in hive 1ike'u.*';
5、describe:描述数据库、表、视图
(1)描述数据库
①查看数据库 hive 的基本信息,包括数据库中文件位置信息等:
hive>describe database hive;
② 查看数据库 hive 的详细信息,包括数据库的基本信息及属性信息等:
hive>describe database extended hive;
(2)描述表和视图
① 查看usr 表和视图 little_usr 的基本信息,包括列信息等:
hive>describe hive.usr;
hive>describe hive.little_usr;
② 查看 usr 表和视图 little_usr 的详细信息,包括列信息、位置信息、属性信息等:
hive>describe extended hive.usr;
hive>describe extended hive.little_usr;
③ 查看 usr 表中列id 的信息:
hive>describe extended usr.id;
6、load:向表中装载数据
①把目录’/usr/local/data’下的数据文件中的数据装载进 usr 表并覆盖原有数据:
hive>load data local inpath'/usr/local/data'overwrite into table usr;
②把目录’/usr/local/data’下的数据文件中的数据装载进 usr 表不覆盖原有数据:
hive>load data local inpath'/usr/local/data'into table usr;
③ 把分布式文件系统目录 hdfs://master_server/usr/local/data 下的数据文件数据装载进usr并覆盖原有数据:
hive>load data inpath'hdfs://master_server/usr/local/data'overwrite into table usr;
7、select:查询表中数据
该命令和 SQL 语句完全相同这里不赘述。
8、insert:向表中插入数据或从表中导出数据
①向 usr1 表中插人来自 usr 表的数据并覆盖原有数据:
hive>insert overwrite table usr1
>select * from usr where age=10;
② 向 usr1 表中插人来自usr 表的数据并追加在原有数据后:
hive>insert into table usr1
>select * from usr where age=10;