第1章
Impala
的基本概念
1.1
什么是
Impala
Cloudera
公司推出,提供对
HDFS
、
HBase
数据的高性能、
低延迟的交互式
SQL
查询功能。
基于Hive,
使用内存计算
,兼顾数据仓库、具有实时、批处理、多并发等优点
。
是CDH
平台首选的
PB
级大数据实时查询分析引擎
。
1.2
Impala
的优缺点
1.2.1
优点
1
)基于内存运算,
不需要把中间结果写入磁盘,省掉了大量的
I/O
开销。
2
)无需转换为
Mapreduce
,
直接访问存储在
HDFS
,
HBase
中的数据进行作业调度
,速度快
。
3)
使用了支持
Data locality
的
I/O
调度机制,尽可能地将数据和计算分配在同一台机器上进行,减少了网络开销
。
4)
支持各种文件格式,如
TEXTFILE
、
SEQUENCEFILE
、RCFile、Parquet
。
5
)可以访问
hive
的
metastore
,对
hive
数据直接做数据分析
。
1.2.2
缺点
1
)对内存的依赖大,且完全依赖于
hive。
2
)实践中,分区超过
1
万,性能严重下降。
3)
只能读取文本文件,而不能
直接
读取自定义二进制文件。
4)
每当新的记录
/
文件被添加到
HDFS
中的数据目录时,该表需要被刷新。
1.3
Impala
的架构
从上图可以看出,
Impala
自身包含三个模块:
Impalad
、
Statestore
(
存放
Hive
的元数据
)
和
Catalog(
拉取真实数据
)
,除此之外它还依赖
Hive Metastore
和
HDFS
。
1
)
impalad
:
接收client的
请求、
Query
执行并返回给中心协调节点
;
子节点上的守护进程,负责向statestore
保持通信,汇报工作
。
2)Catalog:
分发表的元数据信息到各个impalad
中
;
接收来自statestore
的所有请求
。
3)Statestore:
负责收集分布在集群中各个impalad
进程的资源信息、各节点健康状况,同步节点信息
;
第1章
Impala
的安装
2.1
Impala
的地址
1
)
Impala
的官网
http://impala.apache.org/
2
)
Impala
文档查看
http://impala.apache.org/impala-docs.html
3
)下载地址
http://impala.apache.org/downloads.html
2.2
Impala
的安装方式
Cloudera Manager
(
CDH
首推)
下面我们使用
Cloudera Manager
安装
Impala
1.
在主页中点击添加服务
[hadoop102:21000] > show tables;
5.
创建一张
student
表
[hadoop102:21000] > create table student(id int, name string)
> row format delimited
> fields terminated by '\t';
6.
向表中导入数据
[hadoop102:21000] > load data inpath '/student.txt' into table student;
1) 关闭(修改hdfs的配置dfs.permissions
为
false)或修改hdfs的权限,否则impala没有写的权限
[hdfs@hadoop102 ~]$ hadoop fs -chmod 777 /
2
)
Impala不支持将本地文件导入到表中
7.
查询
[hadoop102:21000] > select * from student;
8.
退出
impala
[hadoop102:21000] > quit;
第3章
Impala的操作命令
3.1
Impala
的外部
shell
使用
-q
查询表中数据,并将数据写入文件中
[hdfs@hadoop103 ~]$ impala-shell -q 'select * from student' -o output.txt
查询执行失败时继续执行
[hdfs@hadoop103 ~]$ vim impala.sql
select * from student;
select * from stu;
select * from student;
[hdfs@hadoop103 ~]$ impala-shell -f impala.sql;
(
加上
-c
失败会继续执行
)
[hdfs@hadoop103 ~]$ impala-shell -c -f impala.sql;
在
hive中创建表后,使用-r刷新元数据
hive> create table stu(id int, name string);
[hadoop103:21000] > show tables;
Query: show tables
+---------+
| name |
+---------+
| student |
+---------+
[hdfs@hadoop103 ~]$ impala-shell -r
[hadoop103:21000] > show tables;
Query: show tables
+---------+
| name |
+---------+
| stu |
| student |
+---------+
显示查询执行计划
[hdfs@hadoop103 ~]$ impala-shell -p
[hadoop103:21000] > select * from student;
去格式化输出
[root@hadoop103 ~]# impala-shell -q 'select * from student' -B --output_delimiter="\t" -o output.txt
[root@hadoop103 ~]# cat output.txt
1001 tignitgn
1002 yuanyuan
1003 haohao
1004 yunyun
3.2
Impala
的内部
shell
hive> load data local inpath '/opt/module/datas/student.txt' into table student;
[hadoop103:21000] > select * from student;
[hadoop103:21000] > refresh student;
[hadoop103:21000] > select * from student;
查看历史命令
[hadoop103:21000] > history;
第4章
Impala的数据类型
CREATE DATABASE [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path];
注:
Impala
不支持
WITH DBPROPERTIE…语法
[hadoop103:21000] > create database db_hive
> WITH DBPROPERTIES('name' = 'ttt');
Query: create database db_hive
WITH DBPROPERTIES('name' = 'ttt')
ERROR: AnalysisException: Syntax error in line 2:
WITH DBPROPERTIES('name' = 'ttt')
Encountered: WITH
Expected: COMMENT, LOCATION
5.2
查询数据库
5.2.1
显示数据库
[hadoop103:21000] > show databases;
[hadoop103:21000] > show databases like 'hive*';
Query: show databases like 'hive*'
+---------+---------+
| name | comment |
+---------+---------+
| hive_db | |
+---------+---------+
[hadoop103:21000] > desc database hive_db;
Query: describe database hive_db
+---------+----------+---------+
| name | location | comment |
+---------+----------+---------+
| hive_db | | |
+---------+----------+---------+
5.2.2
删除数据库
[hadoop103:21000] > drop database hive_db;
[hadoop103:21000] > drop database hive_db cascade;
注:
Impala
不支持
alter
database
语法,
当数据库被
USE
语句选中时,无法删除
======
5.3
创建表
5.3.1
管理表
[hadoop103:21000] > create table if not exists student2(
> id int, name string
> row format delimited fields terminated by '\t'
> stored as textfile
> location '/user/hive/warehouse/student2';
[hadoop103:21000] > desc formatted student2;
5.3.2
外部表
[hadoop103:21000] > create external table stu_external(
> id int,
> name string)
> row format delimited fields terminated by '\t' ;
5.4
分区表
5.4.1
创建分区表
[hadoop103:21000] > create table stu_par(id int, name string)
> partitioned by (month string)
> row format delimited
> fields terminated by '\t';
5.4.2
向表中导入数据
[hadoop103:21000] > alter table stu_par add partition (month='201810');
[hadoop103:21000] > load data inpath '/student.txt' into table stu_par partition(month='201810');
[hadoop103:21000] > insert into table stu_par partition (month = '201811')
> select * from student;
如果分区没有,
load data导入数据时,不能自动创建分区。
5.4.3
查询分区表中的数据
[hadoop103:21000] > select * from stu_par where month = '201811';
5.4.4
增加多个分区
[hadoop103:21000] > alter table stu_par add partition (month='201812') partition (month='201813');
5.4.5
删除分区
[hadoop103:21000] > alter table stu_par drop partition (month='201812');
5.4.5
查看分区
[hadoop103:21000] > show partitions stu_par;
第6章 DML数据操作
6.1
数据导入(基本同
hive
类似)
注意:
impala
不支持
load
data local inpath…
6.2
数据的导出
1.impala
不支持
insert
overwrite…语法导出数据
2.impala
数据导出一般使用
impala -o
[root@hadoop103 ~]# impala-shell -q 'select * from student' -B --output_delimiter="\t" -o output.txt
[root@hadoop103 ~]# cat output.txt
1001 tignitgn
1002 yuanyuan
1003 haohao
1004 yunyun
Impala
不支持
export
和
import
命令
第7章 查询
基本的语法跟
hive
的查询语句大体一样
Impala
不支持
CLUSTER BY, DISTRIBUTE BY, SORT BY
Impala
中不支持分桶表
Impala
不支持
COLLECT_SET(col)
和
explode
(
col
)函数
Impala
支持开窗函数
[hadoop103:21000] > select name,orderdate,cost,sum(cost) over(partition by month(orderdate)) from business;
第3章
函数
第8章 函数
8.1
自定义函数
1.
创建一个
Maven
工程
Hive
2.导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
4.打成jar包上传到服务器/opt/module/jars/udf.jar
5.
将
jar
包上传到
hdfs
的指定目录
hadoop fs -put hive_udf-0.0.1-SNAPSHOT.jar /
6. 创建函数
[hadoop103:21000] > create function mylower(string) returns string location '/hive_udf-0.0.1-SNAPSHOT.jar' symbol='com.atguigu.hive_udf.Hive_UDF';
7. 使用自定义函数
[hadoop103:21000] > select ename, mylower(ename) from emp;
8.
通过
show functions查看自定义的函数
[hadoop103:21000] > show functions;
Query: show functions
+-------------+-----------------+-------------+---------------+
| return type | signature | binary type | is persistent |
+-------------+-----------------+-------------+---------------+
| STRING | mylower(STRING) | JAVA | false |
+-------------+-----------------+-------------+---------------+
第9章 存储和压缩
注:
impala
不支持
ORC
格式
1.
创建
parquet
格式的表并插入数据进行查询
[hadoop104:21000] > create table student2(id int, name string)
row format delimited
fields terminated by '\t'
stored as PARQUET;
[hadoop104:21000] > insert into table student2 values(1001,'zhangsan');
[hadoop104:21000] > select * from student2;
2.
创建
sequenceFile
格式的表,插入数据时报错
[hadoop104:21000] > insert into table student3 values(1001,'zhangsan');
Query: insert into table student3 values(1001,'zhangsan')
Query submitted at: 2018-10-25 20:59:31 (Coordinator: http://hadoop104:25000)
Query progress can be monitored at: http://hadoop104:25000/query_plan?query_id=da4c59eb23481bdc:26f012ca00000000
WARNINGS: Writing to table format SEQUENCE_FILE is not supported. Use query option ALLOW_UNSUPPORTED_FORMATS to override.
第10章 优化
1、 尽量将StateStore和Catalog部署到同一个节点,保证他们正常通行。
2、
通过对
Impala Daemon
内存限制(默认
256M
)及
StateStore
工作线程数,来提高
Impala
的执行效率。
3、 SQL
优化,使用之前调用执行计划
。
4、
选择合适的文件格式进行存储
,提高查询效率。
5、
避免产生很多小文件(如果有其他程序产生的小文件,可以使用中间表,将小文件
数据
存放到中间表
。
然后
通过
insert…select…方式中间表的数据插入到最终表中
)
6、
使用合适的分区技术,根据分区粒度测算
7、
使用
compute stats进行表信息搜集,
当一个内容表或分区明显变化,重新计算统计相关数据表或分区。因为行和不同值的数量差异可能导致
impala选择不同的连接顺序时,表中使用的查询。
[hadoop104:21000] > compute stats student;
Query: compute stats student
+-----------------------------------------+
| summary |
+-----------------------------------------+
| Updated 1 partition(s) and 2 column(s). |
+-----------------------------------------+
8、
网络
io
的优化:
a.
尽可能的做条件过滤
b.
使用
limit
字句
c.
输出文件时,避免使用美化输出
d.尽量少用全量元数据的刷新