栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hive基本操作

Hive基本操作

文章目录
  • 前言
  • 一、 create:创建数据库、表、视图
    • 1. 创建数据库
    • 2. 创建表
    • 3. 创建视图
  • 二、drop:删除数据库、表、视图
    • 1. 删除数据库
    • 2. 删除表
    • 3. 删除视图
  • 三、alter:修改数据库、表、视图
    • 1. 修改数据库
    • 2. 修改表
    • 3. 修改视图
  • 四、show:查看数据库、表、视图
    • 1. 查看数据库
    • 2. 查看表和视图
  • 五、describe:描述数据库、表、视图
    • 1. 描述数据库
    • 2. 描述表和视图
  • 六、load:向表中装载数据
  • 七、select:查询表中数据
    • 1. 查询记录
    • 2. 查询不重复的记录
    • 3. 排序和限制
  • 八、insert:向表中插入数据或从表中导出数据
  • 总结


前言

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


一、 create:创建数据库、表、视图 1. 创建数据库

(1) 创建数据库 hive:

hive> create database hive;

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

hive> create database if not exists hive;
2. 创建表

(1)在 hive 数据库中,创建 usr 表,含 3 个属性 id、name 和 age:

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

(2)在 hive 数据库中,创建 usr 表,含 3 个属性 id、name 和 age,存储路径为 “/usr/local/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';

(3)在 hive 数据库中,创建外部 usr 表,含 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';

(4)在 hive 数据库中,创建分区 usr 表,含 3 个属性 id、name 和 age,还存在分区字段 sex:

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

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

二、drop:删除数据库、表、视图 1. 删除数据库

(1)删除数据库 hive,如果不存在会出现警告:

hive> drop database hive;

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

hive> drop database if exists hive;

(3)删除数据库 hive,加上 cascade 关键字,可以删除当前数据库和该数据库中的表:

hive> drop database if exists hive cascade;
2. 删除表

(1)删除 usr 表,如果是内部表,元数据和实际数据都会被删除;如果是外部表,则只删除元数据,不删除实际数据:

hive> drop table if exists usr;
3. 删除视图

(1)删除视图 little_usr:

hive> drop view if exists little_usr;

三、alter:修改数据库、表、视图 1. 修改数据库

(1)为 hive 数据库设置 dbproperties 键值对属性值来描述数据库属性信息:

hive> alter database hive set dbproperties('edited-by'='1i1y');
2. 修改表

(1)重命名 usr 表为 user:

hive> alter table usr rename to user;

(2)为 usr 表增加新分区:

hive> alter table usr add if not exists partition(sex=true);
hive> alter table usr add if not exists partition(sex=false);

(3)删除 usr 表中分区:

hive> alter table usr drop if exists partition(sex=true);

(4)把 usr 表中列名 name 修改为 username,并把该列置于 age 列后:

hive> alter table usr change name username string after age;

(5)在对 usr 表分区字段之前,增加一个新列 sex:

hive> alter table usr add columns(sex boolean);

(6)删除 usr 表中所有字段并重新指定新字段 newid、newname 和 newage:

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

(7)为 usr 表设置 tblproperties 键值对属性值来描述表的属性信息:

hive> alter table usr set tblproperties('notes'='the columns in usr may be null except id');
3. 修改视图

(1)修改 little_usr 视图元数据中的 tblproperties 属性信息:

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

四、show:查看数据库、表、视图 1. 查看数据库

(1)查看 hive 中包含的所有数据库:

hive> show databases;

(2)查看 hive 中以 h 开头的所有数据库:

hive> show databases 1ike 'h.*';
2. 查看表和视图

(1)查看数据库 hive 中所有表和视图:

hive> use hive;
hive> show tables;

(2)查看数据库 hive 中以 u 开头的所有表和视图:

hive> show tables in hive like 'u.*';

五、describe:描述数据库、表、视图 1. 描述数据库

(1)查看数据库 hive 的基本信息,包括数据库中文件位置信息等:

hive> describe database hive;

(2)查看数据库 hive 的详细信息,包括数据库的基本信息及属性信息等:

hive> describe database extended hive;
2. 描述表和视图

(1)查看 usr 表和视图 little_usr 的基本信息,包括列信息等:

hive> describe hive.usr;
hive> describe hive.little_usr;

(2)查看 usr 表和视图 little_usr 的详细信息,包括列信息、位置信息、属性信息等:

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

(3)查看 usr 表中列 id 的信息:

hive> describe extended usr.id;

六、load:向表中装载数据

(1)把目录 /usr/local/data 下的数据文件中的数据装载进 usr 表并覆盖原有数据:

hive> load data lòcal inpath'/usr/local/data' overwrite into table usr;

(2)把目录 /usr/local/data 下的数据文件中的数据装载进 usr 表不覆盖原有数据:

hive> load data local inpath '/usr/local/data' into table usr;

(3)把分布式文件系统目录 hdfs://master_server/usr/local/data 下的数据文件数据装载进 usr 表并覆盖原有数据:

hive> load data inpath 'hdfs://master_server/usr/local/data' overwrite into table usr;

七、select:查询表中数据 1. 查询记录

(1)查询 usr 表中的所有数据:

hive> select * from usr;

(2)查询 usr 表中所有记录,只显示出 id,name,age 三个字段的值:

hive> select id,name,age from usr

(3)查询 usr 表中所有包含 age>20 的数据:

hive> select * from usr where age>20
2. 查询不重复的记录

(1)查询 usr 表中 name 不相同的数据:

hive> select distinct name from usr;

(2)查询 usr 表中 name、age 同时不同的数据:

hive> select distinct name,age from usr;
3. 排序和限制

(1)查询 usr 表并按 age 字段降序排列:

hive> select * from usr order by age desc;

(2)查询 usr 表并按 id 字段升序排列:

hive> select * from usr order by id asc;

(3)取出 usr 表中 age 排名前五的数据:

hive> select * from usr order by age desc limit 5;

八、insert:向表中插入数据或从表中导出数据

(1)向 usrl 表中插入来自 usr 表的数据并覆盖原有数据:

hive> insert overwrite table ursl
	> select * from usr where age=10;

(2)向 ursl 表中插入来自 usr 表的数据并追加在原有数据后:

hive> insert into table ursl
	> select * from usr where age=10;

总结
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604983.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号