9.Hive meta data 2中存储方式: 内置Derby 有目录缺陷和单会话缺陷 Mysql
10.HQL语句不识别TAB分隔
11.元数据 metadata 原数据 original data 源数据 source data
12.Hive常用命令:
1.执行Hadoop命令: dfs -ls /user/....
2.执行linux命令:! ls /user/...
3.Hive命令行中默认显示库名
cd ~
vi .hiverc
set hive.cli.print.current.db=true; -->显示库名
set mapred.job.name=hive-cli-01; -->显示job名
1.select形式输入:
select array(1,2,3);
select map("a",1,"b",2);
select struct(1,2)
2.建表字段为复杂类型
create table table_name (
info_1 array,
info_2 map
info_3 struct
info_4 struct
)
row format delimited
fields terminated by ','
collection items terminated by '::'
map keys terminated by '\003';
3.insert复杂类型
insert into emp01 select 'tom',array('english','chinese') from dual;
insert into userinfo select 'tom',map('type','chinese') from dual;
insert into userinfo select 'tom',struct('hebei','[email protected]','male') from dual;
(struct类型 不能轻易insert,必须字段名为:col1,col2,col3....)
4.load复杂类型
load data inpath '....' into table emp01;
load的时候注意原文件的分隔符等因素
5.访问复杂类型
select id, name[0], score['all'] , score['语文'], addr.province from demo;
17.Hive文件格式及压缩格式
1.默认文件格式:textfile 面向行:txt,seq,面向列:rc,orc
2.压缩格式:可切分:lzo,bz2 不可切分:snappy,gzip
3.查看hive/hadoop中内置编码,解码
set io.compression.codes;
18.压缩优缺点:
优点:减少占用磁盘的空间,减小磁盘或者网络i/o,从而提高吞吐量和i/o性能
缺点:增加cup开销
19.HQL DDL
1.输入规范:
CREATE, DROP, ALTER
DESC <表名>;(DESCRIBE <表名>; 的简写)
DESC FORMATTED <表名>;
SHOW CREATE TABLE <表名>;
2.修改表结构时,只是修改的结构,数据是不会变的,这里hive与MySQL不同,要注意
3.根据某表结构创建另外一张表
CREATE TABLE 表1名 LIKE 表2名
INSERT INTO 表2名 SELECT * FROM 表1名;
4.修改表的名称
ALTER TABLE 表旧名 RENAME TO 新名;
5.给表添加字段
ALTER TABLE 表名 ADD COLUMNS (字段名 字段类型);
6.修改表的字段
ALTER TABLE my_tbl_2 REPLACE COLUMNS (name string);
ALTER TABLE my_tbl_2 REPLACE COLUMNS (id int, name string);
ALTER TABLE my_tbl_2 REPLACE COLUMNS (id int, age int, salary double);
ALTER TABLE my_tbl_2 REPLACE COLUMNS (id int, name string, salary double);
ALTER TABLE my_tbl_2 CHANGE COLUMN id my_id int;
ALTER TABLE my_tbl_2 CHANGE COLUMN id id int AFTER age;
ALTER TABLE my_tbl_2 CHANGE COLUMN id id int FIRST;
7.字段名是乱码的问题:
因为Hive的元数据MySQL设置是latin。。。。
8.建表模板(默认参数):
create [external] table [if not exists] 表名 (
字段名 字段类型,
字段名 字段类型
)
comment 表注释
partitioned by 字段名,字段名
clustered by 字段名,字段名
sorted by 字段名,字段名 into 分桶个数 buckets
row format delimited
fields terminated by '\001'
collection items terminated by '\002'
map keys terminated by '\003'
lines terminated by '\n'
stored as textfile
location hdfs_path;
9.关键词解释
external: 创建内部表还是外部表,此为内外表的唯一区分关键字。
comment col_comment: 给字段添加注释
comment table_comment: 给表本身添加注释
partitioned by: 按哪些字段分区,可以是一个,也可以是多个
clustered by col_name... into num_buckets BUCKETS:按哪几个字段做hash后分桶存储
row format:用于设定行、列、集合的分隔符等设置
stored as : 用于指定存储的文件类型,如 text,rcfile 等
location : 设定该表存储的 hdfs 目录,如果不手动设定,则采用hive默认的存储路径
10.Hive表的默认配置
CREATE TABLE demo (
id INT,
name STRING,
salary DOUBLE,
nick_name ARRAY,
score MAP,
addr STRUCT
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\001'
COLLECTION ITEMS TERMINATED BY '\002'
MAP KEYS TERMINATED BY '\003'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
20.HQL DML
1.插入数据3种方法:
1.insert values:
insert into demo (name, id, salary) values ('Lucy', 6, 3.4);
2.将查询结果插入表中:
insert 表名
partition (col1=...,col2=...) + 查询语句
select * from (
select 1, 'abc', 5.6
union all
select 2, 'xyz', 7.8
) t; -->别名t必须有
3.加载数据
load data [local] inpath '文件路径' [overwrite] into table 表名 partition (col1=...);
2.多插入模式:
from 表名
insert overwrite table 表1名 partition(col1=11)
select id,name,cla where id = ...
insert overwrite table 表2名 partition()
select * where name = ...
3.将表数据写入到hdfs目录中:
insert overwrite [local] directory "目录路径"
row format delimited
fields terminated by '\t'
select * from 表名 where ....
4.语句规范
1.INSERT <表名> <查询语句>
2.LOAD DATA [LOCAL] INPATH '<绝对路径>' [OVERWRITE] INTO TABLE <表名>;
3.OVERWRITE是先删除原数据再添加新数据,否则就是直接追加
4.LOCAL代表从本地文件系统进行复制,不加LOCAL则从HDFS进行移动
5.绝对路径要用引号引起来,绝对路径可以是单个文件,也可以是一个目录,如果是一个目录,则会读取该目录下所有的文件内容(不递归,建议不要再有子目录)
21.Hive DQL
1.单表查询:
1.where :同MySQL差不多
2.distinct
select distinct teacher_id from student_score; -->一般是数据清洗了
数据量大的话推荐使用group by
3.group by:
group by id,name,score,teacher_id; -->group by后面的字段得写全
4.limit
select id,name,score,teacher_id from student limit 1;
5.统计函数/聚合函数/分组查询count,sum,avg,max,min,group by,having
2.order by 与 sort by
1.概念
order by是全局排序,它会只使用一个reducer;
sort by是部分排序,每个reducer的输出均有序,但是全局上不一定有序
2.order by
partition (dt='20181105')
insert overwrite table mb1_sort
select uid, name, url from mb1 order by uid;
3.sort by
insert overwrite table mb1_sort
partition (dt='20181104')
select uid, name, url from mb1 sort by uid;
4.Hive中设置reduce个数:
set mapred.reduce.tasks=3;
3.distribute by 和 cluster by
1.distribute by 概念:
控制mapper的输出在reducer中是如何划分的,解决的问题和分桶一样
分桶机制是在与建表语句结合使用的
如果我们只是进行select操作,并将结果输出到一张未分桶的表中时,
为了实现和分桶一样的效果,就要在select语句中使用distribute by
2.必需事项:
2.1.同时需要设置和分桶个数相匹配的reducer数目
set mapred.reduce.tasks=3
2.2.示例:
insert overwrite table mb1_cluster
partition (dt='20181105')
select uid, id, name, url
from mb1
distribute by uid; -->相同的uid会放到一个桶中
2.3.之后可以直接sort by
insert overwrite table mb1_cluster
partition (dt='20181104')
select uid, id, name, url
from mb1
distribute by uid
sort by id;
3.cluster by
等价于 distribute by uid sort by uid
4.多表查询-union/union all
union 去重,相对效率低
union all 不去重
Hive要求:连接的查询字段数量要相同,对应字段类型也要相同
5.多表查询-子查询/连接查询
1.在select之后
不支持子查询,改为连接查询
2.在from之后
类似与MySQL
3.在where之后
类似与MySQL
4.Hive查询特点
4.1.Hive会对每一个join操作触发一个MR Job
(如a join b join c:针对a join b触发一个Job,计算出a join b的结果后,
再使用这个结果join c,这又会触发一个Job)
4.2.目前,Hive只支持等值连接。
5.join查询
inner join:
left outer join:左表为主,左表数据全部保留,没有关联上的数据设置为NULL
right outer join:
full outer join:么有关联上数据字段全部设置为NULL
6.注意事项:
1.对分号敏感:
rdb sql分号使用:
select concat(key,concat(';',key)) from table;
hive 分号使用:
select concat(key,concat('\;',key)) from table;
2.NULL值判断:
is null 或者 is not null
22.分区
1.分区概念:
1.Hive在查询时通常是做全表扫描的,而一个好的分区设计可以避免全表扫描
并且可以大大减少Hive的扫描数据量
2.最常见的分区表是按天建立分
(当然也有使用时间空间两个维度进行分区的,第一个分区是按天建立的,
第二个则按地区建立)
3.分区不能过多,否则就会大大增加NameNode的压力
(Hadoop HDFS更适合存储与处理少量的大文件而不是大量的小文件)
这时候一般就会结合使用分桶机制
4.分区是一种特殊的列,这种列的值不在数据文件中,而是通过目录名称读取的,
分区实际上正对应了HDFS中的目录
2.分区:
1.表结构:
create table my_access_history (
id int,
name string,
url string
)
partitioned by (dt string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t' ;
2.load数据:
vi my_access_history
2 小明 http://www.qq.com/
LOAD DATA LOCAL INPATH 'my_access_history'
INTO TABLE my_access_history
PARTITION (dt='20181017');
3.insert数据:
INSERT INTO my_access_history
PARTITION (dt='20181019')
SELECT * FROM (
SELECT 2, 'James', 'http://www.google.com/'
)t;
4.查看表有哪些分区:
show partitions my_access_history;
5.规范查看分区:
select * from 表名 where dt=2222 limit 10;
6.动态分区(默认关闭,使用静态分区)
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
有2个分区字段的时候,第一个必须是静态的,第二个是动态的
INSERT INTO my_access_history
PARTITION (dt)
select * from (
SELECT 1, '小红', 'http://www.baidu.com/', '20181020'
)t;
INSERT INTO my_access_history
PARTITION (dt)
select * from (
SELECT 1, '小红', 'http://www.baidu.com/', '20181021'
union all
SELECT 3, '小强', 'http://www.google.com/', '20181022'
)t;
INSERT OVERWRITE TABLE my_access_history
PARTITION (dt)
select * from (
SELECT 1, '小红', 'http://www.baidu.com/', '20181018'
union all
SELECT 3, '小强', 'http://www.google.com/', '20181018'
)t;
23.分桶
1.概念:分桶可以使数据均匀分布,提高查询效率
(尤其是连接查询map side join),特别适用于抽样查询场景
2.桶的数量一般对应reducer的数量
3.建表位置:
create table my_access_history_buckets (
user_id int,
id int,
name string,
url string
)
partitioned by (dt string)
clustered by (user_id) into 10 buckets
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t' ;
4.设置分桶:
set hive.enforce.bucketing=true;
5.正确分桶:
5.1.使用load方式导入的数据肯定不会自动分桶
5.2.使用select方式导入的数据也不一定会自动分桶
5.3.我们应该使用cluster by来保证分桶
insert overwrite table mb
partition(dt=20181105)
select uid, id, name, url
from mb
where dt=20181107
cluster by uid;
5.4.如果我们不使用cluster by,就必须保证select的数据的来源必须是经过真正分桶的,
或者像下面这样,数据是来源于一系列计算的最终结果的
insert overwrite table mb
partition(dt=20181103)
select * from (
SELECT 10010, 1, '小红', 'http://www.baidu.com/'
union all
SELECT 10011, 3, '小强', 'http://www.google.com/'
union all
SELECT 10012, 2, '小明', 'http://www.sogou.com/'
)t;
6.load的正确分桶姿势:
1.数据准备:
10000 1 刘十三 http://www.baidu.com/
10001 2 张三 http://www.le.com/
10002 3 李四 http://www.google.com/
10003 4 王五 http://www.ip138.com/
10004 5 赵六 http://www.qq.com/
10005 6 小明 http://www.qq.com/
10006 7 小红 http://www.qq.com/
10007 8 小强 http://www.qq.com/
10008 9 小刚 http://www.qq.com/
10009 10 小小 http://www.qq.com/
2.进行Load操作到一张新表中:
LOAD DATA LOCAL INPATH 'my_access_history_buckets'
INTO TABLE my_access_history_buckets
PARTITION (dt='20181017');
3.从新表查询插到需求的表中:-->注意最后的cluster by uid
FROM my_access_history_buckets mahb
INSERT OVERWRITE TABLE my_access_history_buckets
PARTITION(dt=20181016)
SELECT user_id, id, name, url WHERE mahb.dt in (20181017, 20181021)
cluster by uid;
继承 extends 多态
继承是面向对象最经常使用的特征之一:继承语法是通过继承发、基类的域和方法 //继承就是从现有的类中生成一个新的类,这个新类拥有现有类的所有extends是使用继承的关键字:
在A类中定义属性和方法;
class A{
//定义属性
int age;
//定义方法
public void go
hive DDL语法汇总
1、对表重命名
hive> ALTER TABLE table_name RENAME TO new_table_name;
2、修改表备注
hive> ALTER TABLE table_name SET TBLPROPERTIES ('comment' = new_comm