Hive学习笔记简版

一.概述

1. Hive是Apache提供的基于Hadoop的数据仓库管理工具
2. Hive提供了类SQL语言来操作Hadoop,底层会将SQL转化为MapReduce来执行,所以效率会比较低
3. Hive适应于离线处理
4. 要求安装Hive的节点要先安装Hadoop,解压完成之后,在启动Hive的时候自动去找环境变量中的HADOOP_HOME

二.数据仓库和数据库比较

        数据库          数据仓库
数据量      <=GB          >=TB
数据种类  单一 - 结构化        多样 - 结构化、半结构化、非结构化
数据来源  相对单一          数据库、日志、爬虫、埋点…
事务    提供了完整的事务(ACID)   弱/无事务
冗余    精简冗余          人为制造冗余 - 副本
场景    为线上系统实时捕获数据    一般存储的是历史数据
系统    OLTP - 联机事务处理    OLAP - 联机分析处理
面向对象  最终面向程序员、DBA    最终面向市场、领导、客户等人员

三.Hive的特点

1. 在Hive中,每一个database/table在HDFS都会对应一个目录
2. Hive中没有主键
3.Hive中如果需要指定字段之间的间隔符号,需要在建表的时候就指定,而一个表一旦建立好,那么间隔符号不能改
4.三个查数据:
从表t1查数据,将指定数据插入到表t2和表t3内;

from t1 insert into t2 select * where id <= 4 insert into t3 select * where gender = 'male';

从表t1查数据,将指定数据写入到本地目录下;

insert overwrite local directory '/opt/hivedata' select * from t1 where id <= 2;

从表t1查数据,将指定数据放入hdfs的指定目录下;

insert overwrite directory '/table' select * from t1;

5.向本地目录或者HDFS中的目录写文件的时候只能用overwrite

四.表结构

1.内部表和外部表
a.内部表:自己建表管理原本在HDFS上不存在的数据
b.外部表:需要建表管理在HDFS上已经存在的数据
c.内部表删除对应的目录一起删除,但是外部表被删除不改变原文件
2.分区表
a.分区字段在原文件中是不存在,需要在添加数据的时候手动指定
b.分区的作用是将数据进行分类

create table cities(id int, name string) partitioned by(country string) row format delimited fields terminated by ' ';
load data local inpath '/opt/hivedata/cn.txt' into table cities partition(country='china');

c.每一个分区对应一个目录
d.如果在查询的时候加入分区条件,效率会大幅度提高;如果产生了跨分区查询,效率反而会下降
e.自己手动创建的目录并不会被认为称分区,需要手动来添加分区

alter table cities add partition(country='japan') location '/user/hive/warehouse/hivedemo.db/cities/country=japan';

或(可能会失败)

msck repair table cities;

f.如果从未分区表中查询数据向已分区表中插入,那么需要开启动态分区机制
# 开启动态分区机制

set hive.exec.dynamic.partition = true;

# 关闭严格模式

set hive.exec.dynamic.partition.mode = nostrict;

# 进行动态分区

insert into table t1 partition(class) select sid, sname, sclass from t1_tmp distribute by sclass;

g.分区的时候可以指定多个字段,在前的字段会自动包含在后的字段
3.分桶表
a.分桶表的作用是用于进行数据抽样的
b.分桶机制默认是不开启的

set hive.enforce.bucketing = true;
create table t1_sam(id int, name string) clustered by(name) into 6 buckets row format delimited fields terminated by ' ';
insert into table t1_sam select * from t1;
select * from t1_sam tablesample(bucket 2 out of 3 on name);

c.分桶表在进行分桶的时候不能从本地文件加载数据,也不能是外部表,只能从其他表中查询然后插入分桶表中
d.允许一个表既分区又分桶

五.其他

1.SerDe
a.通过正则表达式来应对实际过程中不规则数据
b.在正则表达式中设置捕获组来应对不规则数据,在使用的时候,每一个捕获组对应表中的一个字段,意味着捕获组的个数和字段的个数是一致的 --- 确定捕获组之间的间隔符号

create table t4(ip string, datetime string, timezone string, request string,resource string, protocol string, stateid int)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' with serdeproperties("input.regex" = "(.*) \-\- \\[(.*) (.*)\\] \"(.*) (.*) (.*)\" ([0-9]*) \-") stored as textfile;

2.索引
a.索引的作用是能够提高查询速率
b.数据库中的索引是针对主键自动建立索引,Hive没有主键,所以Hive中默认也不自动建立索引
c.在Hive中,数据可以建立索引,但是需要指定字段建立索引
# 建立索引表

create index s_index on table t1(id) as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild in table t1_index;

# 针对表t1产生索引

alter index s_index on t1 rebuild;

# 删除索引

drop index s_index on t1;

3.视图
a.
b.视图分为:物化视图(视图是维系在磁盘上)和虚拟视图(视图是维系在内存中)
c.Hive中只支持虚拟视图

create view score_view as select name, math from score;

d.视图中要跟随一个查询语句,但是在Hive中,创建视图的时候,这个查询语句并没有被触发 - 即意味着,这个视图创建好之后,只要没有操作这个视图,那么这个视图中此时是没有数据的
4.元数据
a.库名、表名、字段名、索引、视图、分区字段、抽样字段等称之为Hive的元数据
b.Hive的元数据是存储在关系型数据库中。关系型数据库目前只支持两种:Derby和MySQL。默认情况下,Hive的元数据是存储在Derby中
5.beeline
#远程连接方式:

sh beeline -u
jdbc:hive2://hadoop01:10000/hivedemo -n root

# 其中,-u表示连接地址,-n表示用户名

六.数据类型

1.将数据类型拆分为基本类型和复杂类型
2.复杂类型:
a.array--数组类型,对应Java中的数组和集合
# 例句

create table nums(num1 array<int>, num2 array<int>) row format delimited fields terminated by ' ' collection items terminated by ',';

# 非空查询

select info['alex'] from infos where info['alex'] is not null;

b.map--映射类型,对应Java中的Map类型
# 例句

create table infos(id int, info map<string,int>) row format delimited fields terminated by ' ' map keys terminated by ':';

# 非空查询

select info['alex'] from infos where info['alex'] is not null;

c.struct--结构体类型,对应Java中的对象
# 例句

create external table score(info struct<name:string, chinese:int, math:int, english:int>) row format delimited collection items terminated by ' ' location '/score';

#获取指定属性值

select info.chinese from score;

七.主要函数

1.concat_ws:用指定符号拼接多个字符串
例:
# txt文档内容为

mail 163 com
news baidu com
hive apache org

# 建表

create table web(app string, name string, type string) row format delimited fields terminated by ' ';

# 拼接

select concat_ws('.', app, name, type) from web;

2.explode:将数组中元素提取出来形成单独的一行
例:
# 建表管理原始数据

create external table words(word string) row format delimited fields terminated by ',' location '/words';

# 以空格为单位将单词给拆分成数组

select split(word, ' ') from words;

# 将数组中的每一个单词拆分成单独的一行以便于统计

select explode(split(word, ' ')) from words;

# 统计个数

select w , count(w) from (select explode(split(word, ' ')) w from words)ws group by w;

3.自定义函数 - UDF - User Define Function

猜你喜欢

转载自www.cnblogs.com/fanqinglu/p/11837548.html