Hive
-
Hive描述
- 以分布式的形式,执行SQL语句,进行数据统计分析,将SQL语句 翻译成MapReduce程序运行
-
Hive核心架构
- 元数据管理,称之为Metastore服务
- SQL解析器(Driver驱动程序),完成SQL解析、执行优化、代码提交等功能
- 用户接口:提供用户和Hive交互的功能

-
Hive创建表
- 设置字段分隔符 ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘|’
![[图片]](/https://i-blog.csdnimg.cn/blog_migrate/21f20ef1151fb69edab15b2004f37a62.png)

- 设置字段分隔符 ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘|’
-
Hive分区
- 创建分区表
create table tablename(...) partitioned by (分区列 列类型, ......) row format delimited fields terminated by ' '- 创建分区表语法
create table score(s_id string,s_score string) partitioned by (month string) row format delimited fields terminated by '\t' - 创建一个表带多个分区
create table score(s_id string,s_score string) partitioned by (year string,month string,day string) row format delimited fields terminated by '\t' - 加载数据到分区表中
load data local inpath '/score.txt' into table score partition(month='202006') - 加载数据到一个多分区的表中
load data local inpath '/score.txt' into table score partition(year='2020',month='202006',day='01')
- 创建分区表语法
- 查看分区
show partitions 表名 - 添加一个分区
alter table 表名 add partition(month='202005'); - 同时添加多个分区
alter table 表名 add partition(month='202005') partition(month='202006') - 删除分区
alter table 表名 drop partition(month='202006')
- 创建分区表
-
Hive分桶
- 开启分桶的自动优化(自动匹配reduce task数量和桶数量一致)
set hive.enforce.bucketing=true; - 创建分桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t'; - 分桶表数据加载
- 创建一个临时表(外部表或内部表均可),通过load data加载数据进入表
- 创建普通表
create table course_common(c_id string,c_name string,t_id string) row format delimited fields terminated by '\t' - 普通表加载数据
load data local inpath '/course.txt' into table course_common
- 然后通过insert select 从临时表向桶表插入数据
- 通过insert overwrite给桶表中加载数据
insert overwrite table course select * from course_common cluster by(c_id)
- 开启分桶的自动优化(自动匹配reduce task数量和桶数量一致)
-
Hive修改表
- 表重命名
alter table old_table_name rename to new_table_name; - 修改表属性
ALTER TABLE table_name SET TBLPROPERTIES table_properties;- table_properties: (property_name = property_value, property_name = property_value, … )
- 修改内外部表属性
ALTER TABLE table_name SET TBLPROPERTIES("EXTERNAL"="TRUE") - 修改表注释
ALTER TABLE table_name SET TBLPROPERTIES ('comment' = new_comment)
- 修改分区
- 添加分区
ALTER TABLE tablename ADD PARTITION (month='201101') - 修改分区
ALTER TABLE tablename PARTITION (month='202005') RENAME TO PARTITION (month='201105') - 删除分区
ALTER TABLE tablename DROP PARTITION (month='201105')
- 添加分区
- 修改列
- 添加列
ALTER TABLE table_name ADD COLUMNS (v1 int, v2 string) - 修改列名
ALTER TABLE test_change CHANGE v1 v1new INT
- 添加列
- 删除表
DROP TABLE tablename - 清空表(只可以清空内部表)
TRUNCATE TABLE tablename
- 表重命名
-
Hive复杂类型操作

-
Hive抽样
- 桶抽样方式,TABLESAMPLE(BUCKET x OUT OF y ON(colname | rand())),推荐,完全随机,速度略慢块抽样,使用分桶表可以加速
- SELECT … FROM tbl TABLESAMPLE(BUCKET x OUT OF y ON(colname | rand()))
- y表示将表数据随机划分成y份(y个桶)
- x表示从y里面随机抽取x份数据作为取样
- colname表示随机的依据基于某个列的值
- rand()表示随机的依据基于整行
- 示例:
SELECT username, orderId, totalmoney FROM itheima.orders TABLESAMPLE(BUCKET 1 OUT OF 10 ON username); - 示例:
SELECT * FROM itheima.orders TABLESAMPLE(BUCKET 1 OUT OF 10 ON rand()); - 使用colname作为随机依据,则其它条件不变下,每次抽样结果一致
- 使用rand()作为随机依据,每次抽样结果都不同
- SELECT … FROM tbl TABLESAMPLE(BUCKET x OUT OF y ON(colname | rand()))
- 块抽样方式,TABLESAMPLE(num ROWS | num PERCENT | num(K|M|G)),速度快于桶抽样方式,但不随机,只是按照数据顺序从前向后取。
- SELECT … FROM tbl TABLESAMPLE(num ROWS | num PERCENT | num(K|M|G));
- num ROWS 表示抽样num条数据
- num PERCENT 表示抽样num百分百比例的数据
- num(K|M|G) 表示抽取num大小的数据,单位可以是K、M、G表示KB、MB、GB
- 使用这种语法抽样,条件不变的话,每一次抽样的结果都一致,即无法做到随机,只是按照数据顺序从前向后取
- SELECT … FROM tbl TABLESAMPLE(num ROWS | num PERCENT | num(K|M|G));
- 桶抽样方式,TABLESAMPLE(BUCKET x OUT OF y ON(colname | rand())),推荐,完全随机,速度略慢块抽样,使用分桶表可以加速
-
Hive函数
-


3万+

被折叠的 条评论
为什么被折叠?



