hive综述

1.hive的作用
用类SQL的语言HQL来计算HDFS中的数据,主要做海量数据的离线分析,没有实物的概念.

2.Hive更加侧重OLAP( 联机分析处理)的操作
tips: 
set hive.cli.print.current.db=true//显示数据库实例名
set hive.cli.print.header=true     //显示列名

3.基础建表语句
Create [external] table [if not exists]  table_name
[(col_name data_type[comment],...)]
[comment tablencomment]
[partitioned by (col_name data_type)]
[cluster by (col_name...)]
[sorted by (column_name [asc|desc],...)] into num_buckets buckets
[row format row_format]
[stored as file_format]
[location   hdfs_path]

4.默认数据库的位置
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/usr/hive/warehouse</value>
</property>

也可以通过set hive.metastore.warehouse.dir; 来获取

5.列操作
alter table ... add columns (col_name data_type);
alter table ... replace columns (col_name datatype);
alter table ... change col_name col_name data_type first ; 移动某列到最前
alter table ... change col_name col_name data_type after col_name 1;在某列后新增字段

6.数据的加载方式
读模式:数据被加载到数据库的时候,不对其合法性进行检验,只在查询等操作的时候检验  特点是加载速度快
写模式:加载时,对数据合法性进行检验,数据库中的都是合法数据。  特点:数据加载慢,但查询速度快
hive采用的是度模式,对于非法数据的显示为null

7.hive表中的数据加载
load data [local inpath] linux_fs_path into table tblName;

8.分区操作:
加载分区数据
load data [local path] hdfs_path into table tabName partition (par='');
手工创建分区:alter table tblName add partition(par='');
查看表分区:show partitions tblName
按分区查数据:需开启动态分区 insert into tblName partition (hdfs_par)  select ...;
动静分区同时使用时,需指定静态分区
开启动态分区:
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nostrict;
set hive.exec.max.dynamic.parititons.pernde=1000;
使用 set hive.exec.max.dynamic.partitions 获取hive支持的最大分区数

数据导出: insert overwrite [local] directory  --select * from ..;

有些hql语句的执行会转化为mr,要提高效率需使用本地模式
set hive.exec.mode.local.auto=true;

9.hive的文件格式
textfile
hive默认格式,数据不做压缩,磁盘开销大,数据解析开销大
使用这种方式hive不会对数据进行切分,从而无法对数据进行并行操作。
sequence file
是hadoop api提供的一种二进制文件支持,具有使用翻遍,可压缩,可分割的特点
rcfile
一种行列存储相结合的存储方式
首先将数据按行分块,保证同一个record在一个块上,避免读取一个记录需要读多个bloce,
其次块数据列式存储,有利于数据压缩和快速的列存取
rcfile存储空间小,查询效率高,需要通过text文件转化来加载,加载的速度最低。
目前用的最多的是parquet,其特点是二进制存储,列式存储,映射下推,谓词下推

10.hive函数 show functions
hive数据行列转换
第一步,做表关联,分析结果
select u.id,u.name,a.address from tll_user u join tll_address a on u.id=a.uid;
第二步,对多个出现的结果按照id来拼接字符串
select u.id,max(u.name),concat_ws(",",collect(a.address)) as addr from tll_user u join tll_address a on u.id=a.id group by u.id;

使用hive完成单词统计
第一步 对文本切割
select split(word,'') from tblName;
第二步 集合数据转多行
select explode(split(word,'')) from tblName;
第三步 使用聚合函数统计
select word,count(1) as count from (select explode(split(word,'') from tblName w group by word));














猜你喜欢

转载自blog.csdn.net/wx740851326/article/details/80915315
今日推荐