码迷,mamicode.com
首页 > 其他好文 > 详细

Hive(二)---- Hive基本使用

时间:2018-05-06 20:08:07      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:通过   like   AC   change   ati   使用场景   for   exist   中间   

Hive常用命令

创建数据库

创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db

create database 库名;

避免要创建的数据库已经存在错误,增加if not exists判断。(标准写法)

create database if not exists 库名;

创建一个数据库,指定数据库在HDFS上存放的位置

create database db_hive2 location ‘/db_hive2.db‘;

修改数据库

用户可以使用ALTER DATABASE命令为某个数据库的DBPROPERTIES设置键-值对属性值,来描述这个数据库的属性信息。数据库的其他元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置。

alter database db_hive set dbproperties(‘createtime‘=‘20180830‘);

在mysql中查看修改结果

desc database extended db_hive;

查询数据库

显示数据库

显示数据库

show databases;

过滤显示查询的数据库

show databases like ‘db_hive*‘;

查看数据库详情

显示数据库信息

desc database db_hive;

显示数据库详细信息,extended

desc database extended db_hive;

使用数据库

use db_hive;

删除数据库

删除空数据库

drop database db_hive2;

如果删除的数据库不存在

drop database if exists db_hive2;

如果数据库不为空

drop database db_hive cascade;

创建表

1.提示:如果将sql语句写到文件中是,可以用:hive -f 文件名的方式来执行文件中的sql语句

2.默认创建的表都为内部表

创建内部表

create table if not exists 表名(eid int, name string, sex string) row format delimited fields terminated by \t;

查询表类型

desc formatted 表名;

创建外部表

因为表是外部表,所有Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉

建表语句:

create external table if not exists 表名(eid int, name string, sex string) row format delimited fields terminated by \t;

外部表使用场景

每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表

向外部表导入数据

load data local inpath ‘本地文件路径‘ into table 库名.表名;

分区表

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成小的数据集。在查询时通过WHERE子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多

创建分区表

create table dept_partition(
               deptno int, dname string, loc string
               )
               partitioned by (month string)
               row format delimited fields terminated by \t;

加载数据到分区表中

load data local inpath ‘/opt/module/datas/dept.txt‘ into table default.dept_partition partition(month=‘201709‘);

查询分区表中数据

单分区查询

select * from dept_partition where month=‘201709‘;

多分区查询

select * from dept_partition where month=‘201709‘
              union
select * from dept_partition where month=‘201708‘
              union
select * from dept_partition where month=‘201707‘;

增加分区

创建当个分区

alter table dept_partition add partition(month=‘201706‘) ;

同时创建多个分区

alter table dept_partition add partition(month=‘201705‘) partition(month=‘201704‘);

删除分区

删除单个分区

alter table dept_partition drop partition (month=‘201704‘);

同时删除多个分区

alter table dept_partition drop partition (month=‘201705‘), partition (month=‘201706‘);

查看分区表有多少分区

show partitions dept_partition;

查看分区表结构

desc formatted dept_partition;

创建二级分区表

create table dept_partition2(
               deptno int, dname string, loc string
               )
               partitioned by (month string, day string)
               row format delimited fields terminated by \t;

加载数据到二级表中

load data local inpath ‘/opt/module/datas/dept.txt‘ into table default.dept_partition2 partition(month=‘201709‘, day=‘13‘);

查询分区数据

select * from dept_partition2 where month=‘201709‘ and day=‘13‘;

修改表

alter table dept_partition2 rename to dept_partition3;

增加/修改/替换列信息

添加列

alter table dept_partition add columns(deptdesc string);

更新列

alter table dept_partition change column deptdesc desc int;

替换列

alter table dept_partition replace columns(deptno string, dname string, loc string);

删除表

drop table dept_partition;

Hive(二)---- Hive基本使用

标签:通过   like   AC   change   ati   使用场景   for   exist   中间   

原文地址:https://www.cnblogs.com/screen/p/8999057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!