CTL模式
待补充
web模式
hive –servece hwi
待补充
开启远程连接
hive –servece hiveserver
Hive数据类型
- 基本数据类型
- tinyint/smallint/int/bigint:整数类型
- float/double:付点类型
- boolean:布尔类型
- string:字符串类型
- 复杂数据类型
- Array:数组类型
- Map:集合类型 key -> value键值对
- Struct:结构类型,可以包含不同数据类型的元素,这些元素可以通过”点语法”的方式来得到所需的元素
- 时间数据类型
- Date:从Hive0.12.0开始支持:DATE ‘2013-01-01’
- Timestamp:从Hive0.8.0开始支持:时间戳:hive:select unix_timestamp();
-> cast()函数可以相互转换这两个时间的数据类型
Hive的数据存储
- 基于HDFS
- 没有专门的数据存储格式:txt,csv,巴拉巴拉都可以
- 主要存储结构:数据库,文件,表,视图。
- 可以直接加载文本文件(.txt文件等)
- 创建hive表时候,指定Hive数据的列分隔符与行分隔符。
表
1.Table 内部表:
- 与数据库中的Tabled在概念上是类似的
- 每个Table在Hive中都有一个相应的目录存储数据
- 所有的Table数据(不包括External Table)都保存在这个目录中
- 删除表的时候,元数据和数据都会被删除
- 指定表中的数据(查询创建表):create table test as select * from another_table
- 指定存储地址:create table test (tid int,tname string) location ‘/mytable/hive/test’
- 指定hive数据列分隔符:create table test (tid int,tname string) row format delimited fields - terminated by ‘,’
- 给表添加新的列:alter table test add columns(newCloumnsname datatype);
- 删除表:drop table tablename;
- 插入数据:insert into table test value (…),(…);
2.Partition 分区表
- Partition 对应数据库的Partition列的密集索引。
- 在Hive中,表中的一个Partition对应表下的一个目录,所有的Partition的数据都存储在对应的目录中。
- 通过条件分区来提高查询效率。
- 创建分区表:
create table partition_table (sid int, sname string) partitioned by (gender string) row format delimited fields terminated by ‘,’; - 插入数据并分区:表示通过gender=’M’这个条件来分区。
insert into table partition_table partition(gender=’M’) value (…),(…);
3.External Table 外部表
- 指向已经在HDFS中存在的数据,可以创建Partition。
- 它和内部表在元数据的组织上是相同的,而实际数据存储则有较大的差异。
- 外部表只是一个过程,加载数据和创建表同时完成,并不会移动数据到数据仓库的目录中,知识与数据建立一个链接。当删除一个外部表的时候,仅删除该链接。
- 创建一个外部表并指向hdfs的/input目录:
create table external_table (sid int, sname string) row format delimited fields terminated by ‘,’ location ‘/input’;
4.Bucket Table 桶表
- 桶表是对数据进行哈希取值,然后放到不同的文件中存储。为了达到均衡存储打散数据分开存储的目的,避免造成热块现象。
- 创建桶表:根据sname这个字段做哈希运算分开放入5个桶中存储。
create table bucket_table (sid int,sname string,age int) clustered by (sname) into 5 bucket;
视图
- 视图是一个虚表,是一个逻辑概念;可以跨越多张表。
- 视图建立在已有的表的基础上,视图依赖已经建立的表,成为基表。
- 视图可以简化复杂的查询。
例子:
两张表dept,emp, 查询员工信息:员工号,姓名,月薪,年薪,不部门名称
hive> desc dept;
deptno int
dname string
hive> select * from dept;
OK
10 SALES
20 RESEARCH
30 ACCOUNTING
hive> desc emp;
OK
empno int
ename string
job string
mgr string
hiredate date
sal int
comm int
deptno int
hive> select * from emp;
OK
7369 SMITH CLERK 7902 1980-12-17 800 NULL 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 NULL 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 NULL 30
7782 CLARK MANAGER 7839 1981-06-09 2450 NULL 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 NULL 20
7839 KING PRESIDENT 1981-11-17 5000 NULL 10
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 NULL 20
7922 JAMES CLERK 7698 1981-12-03 950 NULL 30
7902 FORD ANALYST 7566 1981-12-03 3000 NULL 20
7934 MILLER CLERK 7782 1982-01-23 1300 NULL 10
查询员工信息:员工号,姓名,月薪,年薪,不部门名称
hive> create view empinfo
> as
> select e.empno,e.ename,e.sal,e.sal*12 annlsal,d.dname
> from emp e,dept d
> where e.deptno=d.deptno;
OK
Time taken: 5.238 seconds
hive> desc empinfo;
OK
empno int
ename string
sal int
annlsal int
dname string
hive> select * from empinfo;
OK
7369 SMITH 800 9600 RESEARCH
7499 ALLEN 1600 19200 ACCOUNTING
7521 WARD 1250 15000 ACCOUNTING
7566 JONES 2975 35700 RESEARCH
7654 MARTIN 1250 15000 ACCOUNTING
7698 BLAKE 2850 34200 ACCOUNTING
7782 CLARK 2450 29400 SALES
7788 SCOTT 3000 36000 RESEARCH
7839 KING 5000 60000 SALES
7844 TURNER 1500 18000 ACCOUNTING
7876 ADAMS 1100 13200 RESEARCH
7922 JAMES 950 11400 ACCOUNTING
7902 FORD 3000 36000 RESEARCH
7934 MILLER 1300 15600 SALES