弊端:
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
create table t_2(id int,name string,age int) row format delimited fields terminated by ','; --指定分隔符为逗号
复制代码
猜想3:建表的时间字段范例要不要和文件中数据保持一致? 肯定要保持一致
如果不一致,hive会尝试举行转换,但是不保证成功,如果不成功显示null。
create table t_3(id int,name int,age string) row format delimited fields terminated by ',';
+---------+-----------+----------+--+
| t_3.id | t_3.name | t_3.age |
+---------+-----------+----------+--+
| 1 | NULL | 18 |
| 2 | NULL | 24 |
| 3 | NULL | 45 |
+---------+-----------+----------+--+
复制代码
当我们映射成功之后,会如何?
就可以基于表写Hive SQL 开展数据分析,不用写MapReduce。
0: jdbc:hive2://node1:10000> select * from t_2 where age >18;
+---------+-----------+----------+--+
| t_2.id | t_2.name | t_2.age |
+---------+-----------+----------+--+
| 2 | james | 24 |
| 3 | anna | 45 |
+---------+-----------+----------+--+
2 rows selected (0.722 seconds)
0: jdbc:hive2://node1:10000>
0: jdbc:hive2://node1:10000> select count(*) from t_2 where age >18;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Hive SQL中常见的show语法 show databases show tables show partitions desc formatted table_name; 检察表的元数据信息 show create table table_name; 获取表的DDL建表语句 show functions;
--1、显示所有数据库 SCHEMAS和DATABASES的用法 功能一样
show databases;
--2、显示当前数据库所有表/视图/物化视图/分区/索引
show tables;
SHOW TABLES [IN database_name]; --指定某个数据库
--3、显示表分区信息,分区按字母顺序列出,不是分区表执行该语句会报错
show partitions table_name;
show partitions itheima.student_partition;
--4、显示表、视图的创建语句
SHOW CREATE TABLE ([db_name.]table_name|view_name);
show create table student;
--5、显示当前支持的所有自定义和内置的函数
show functions;
--6、Describe desc
--查看表信息(格式化美观)
desc formatted table_name;
-- 视图的展示
create view student_view as select * from itheima.student_plus;
The total input size of the job is lower than: hive.exec.mode.local.auto.inputbytes.max (128MB by default) --数据量小于128M
The total number of map-tasks is less than: hive.exec.mode.local.auto.tasks.max (4 by default) --maptask个数少于4个
The total number of reduce tasks required is 1 or 0. --reducetask个数是0 或者 1
复制代码
切换Hive的实行引擎
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
如果针对Hive的调优依然无法满足你的需求 还是效率低, 尝试使用spark计算引擎 或者Tez.
复制代码
13.3、join优化
开启map端jion 不须要实行shuffle
先对于毗连字段举行null值过滤
对于毗连字段分桶,并且两个表的分桶数目最好是雷同大概是倍数关系
底层还是MapReduce的join优化。
MapReduce中有两种join方式。指的是join的行为发生什么阶段。
map端join
reduce端join
优化1:Hive自动尝试选择map端join提高join的服从 省去shuffle的过程。
开启 mapjoin 参数设置:
(1)设置自动选择 mapjoin
set hive.auto.convert.join = true; --默认为 true
(2)大表小表的阈值设置:
set hive.mapjoin.smalltable.filesize= 25000000; -- 此处为小于25M就是一个小表可以走map端join,否则不行
复制代码
不使用map端join
使用map端join
优化2:大表join大表
可以对于毗连字段举行分桶
join 数据的毗连如果毗连字段为null 毗连肯定是无意义的, 但是会占用内存和资源
在join之前把空值替换或删除,则会淘汰匹配次数,提高服从
可以先对于被毗连表举行过滤,再毗连,不要先毗连再过滤
--背景:
大表join大表本身数据就十分具体,如果join字段存在null空值 如何处理它?
--方式1:空key的过滤 此行数据不重要
参与join之前 先把空key的数据过滤掉
SELECT a.* FROM (SELECT * FROM nullidtable WHERE id IS NOT NULL ) a JOIN ori b ON a.id =b.id;
--方式2:空Key转换
CASE WHEN a.id IS NULL THEN 'xxx任意字符串' ELSE a.id END
CASE WHEN a.id IS NULL THEN concat('hive', rand()) ELSE a.id --避免转换之后数据倾斜 随机分布打散