关于大数据:Impala详解

9次阅读

共计 9003 个字符,预计需要花费 23 分钟才能阅读完成。

第 1 章 Impala 的基本概念

1.1 什么是 Impala

Cloudera 公司推出,提供对 HDFS、Hbase 数据的高性能、低提早的交互式 SQL 查问性能。

基于 Hive,应用内存计算,兼顾数据仓库、具备实时、批处理、多并发等长处。

是 CDH 平台首选的 PB 级大数据实时查问剖析引擎。

1.2 Impala 的优缺点

1.3 Impala 的组成

1.4 Impala 的运行原理

Impala 执行查问的具体过程:

1)当用户提交查问前,Impala 先创立一个负责协调客户端提交的查问的 Impalad 过程,该过程会向 Impala State Store 提交注册订阅信息,State Store 会创立一个 statestored 过程,statestored 过程通过创立多个线程来解决 Impalad 的注册订阅信息。

2)用户通过 CLI 客户端提交一个查问到 impalad 过程,Impalad 的 Query Planner 对 SQL 语句进行解析,生成解析树;而后,Planner 把这个查问的解析树变成若干 PlanFragment,发送到 Query Coordinator.

3)Coordinator 通过从元数据库中获取元数据,从 HDFS 的名称节点中获取数据地址,以失去存储这个查问相干数据的所有数据节点。

4)Coordinator 初始化相应 impalad 上的工作执行,即把查问任务分配给所有存储这个查问相干数据的数据节点。

5)Query Executor 通过流式替换两头输入,并由 Query Coordinator 汇聚来自各个 impalad 的后果。

6)Coordinator 把汇总后的后果返回给 CLI 客户端。

第 2 章 Impala 的装置

2.1 Impala 的地址

  1. Impala 的官网

http://impala.apache.org/

  1. Impala 文档查看

http://impala.apache.org/impala-docs.html

  1. 下载地址

http://impala.apache.org/downloads.html

2.2 Impala 的装置形式

Impala 有两种装置形式:

  1. 手动装置。
  2. CDH 装置(举荐)。

上面咱们应用 Cloudera Manager 装置 Impala:

1)在主页中点击增加服务

2)抉择 Impala 服务

3)进行角色调配

留神:最好将 StateStore 和 CataLog Sever 独自部署在同一节点上。

4)配置 Impala

5)启动 Impala

6)装置胜利


2.3 Impala 的监护治理

能够通过上面的链接来拜访 Impala 的监护治理页面:

1.查看 StateStore
http://hadoop102:25020/

2.查看 Catalog

http://hadoop102:25010/

2.4 Impala 的初体验

1. 启动 Impala

[root@hadoop102 ~]# impala-shell

2. 查看数据库

[hadoop102:21000] > show databases;

3. 关上默认数据库

[hadoop102:21000] > use default;

4. 显示数据库中的表

[hadoop102:21000] > show tables;

5. 创立一张 student 表

[hadoop102:21000] > create table student(id int, name string)

row format delimited

fields terminated by ‘t’;

6. 向表中导入数据

[hadoop103:21000] > load data inpath ‘/student.txt’ into table student;

留神:

1) 敞开(批改 hdfs 的配置 dfs.permissions 为 false)或批改 hdfs 的权限,否则 impala 没有写的权限

[hdfs@hadoop103 ~]$ hadoop fs -chmod 777 /

2) Impala 不反对将本地文件导入到表中

7. 查问

[hadoop103:21000] > select * from student;

8. 退出 impala

[hadoop103:21000] > quit;

第 3 章 Impala 的操作命令

3.1 Impala 的内部 shell

选项

形容

-h, –help

显示帮忙信息

-v or –version

显示版本信息

-i hostname, –impalad=hostname

指定连贯运行 impalad 守护过程的主机。默认端口是 21000。

-q query, –query=query

从命令行中传递一个 shell 命令。执行完这一语句后 shell 会立刻退出。

-f query_file, –query_file= query_file

传递一个文件中的 SQL 查问。文件内容必须以分号分隔

-o filename or –output_file filename

保留所有查问后果到指定的文件。通常用于保留在命令行应用 -q 选项执行单个查问时的查问后果。

-c

查问执行失败时继续执行

-d default_db or –database=default_db

指定启动后应用的数据库,与建设连贯后应用 use 语句抉择数据库作用雷同,如果没有指定,那么应用 default 数据库

-r or –refresh_after_connect

建设连贯后刷新 Impala 元数据

-p, –show_profiles

对 shell 中执行的每一个查问,显示其查问执行打算

-B(–delimited)

去格式化输入

–output_delimiter=character

指定分隔符

–print_header

打印列名

  1. 连贯指定 hadoop103 的 impala 主机

[root@hadoop102 datas]# impala-shell -i hadoop103

  1. 应用 - q 查问表中数据,并将数据写入文件中

[hdfs@hadoop103 ~]$ impala-shell -q ‘select * from student’ -o output.txt

  1. 查问执行失败时继续执行

[hdfs@hadoop103 ~]$ vim impala.sql

select * from student;

select * from stu;

select * from student;

[hdfs@hadoop103 ~]$ impala-shell -f impala.sql;

[hdfs@hadoop103 ~]$ impala-shell -c -f impala.sql;

  1. 在 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 |

  1. 显示查问执行打算

[hdfs@hadoop103 ~]$ impala-shell -p

[hadoop103:21000] > select * from student;

所谓执行打算,即在查问 sql 执行之前,先对该 sql 做一个剖析,列出须要实现这一项查问的具体计划:

  1. 去格式化输入

[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

选项

形容

help

显示帮忙信息

explain <sql>

显示执行打算

profile

(查问实现后执行)查问最近一次查问的底层信息

shell <shell>

不退出 impala-shell 执行 shell 命令

version

显示版本信息(同于 impala-shell -v)

connect

连贯 impalad 主机,默认端口 21000(同于 impala-shell -i)

refresh <tablename>

增量刷新元数据库

invalidate metadata

全量刷新元数据库(慎用)(同于 impala-shell -r)

history

历史命令

  1. 查看执行打算

explain select * from student;

  1. 查问最近一次查问的底层信息

[hadoop103:21000] > select count(*) from student;

[hadoop103:21000] > profile;

  1. 查看 hdfs 及 linux 文件系统

[hadoop103:21000] > shell hadoop fs -ls /;

[hadoop103:21000] > shell ls -al ./;

  1. 刷新指定表的元数据

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;

  1. 查看历史命令

[hadoop103:21000] > history;

第 4 章 Impala 的数据类型

Hive 数据类型

Impala 数据类型

长度

TINYINT

TINYINT

1byte 有符号整数

SMALINT

SMALINT

2byte 有符号整数

INT

INT

4byte 有符号整数

BIGINT

BIGINT

8byte 有符号整数

BOOLEAN

BOOLEAN

布尔类型,true 或者 false

FLOAT

FLOAT

单精度浮点数

DOUBLE

DOUBLE

双精度浮点数

STRING

STRING

字符系列。能够指定字符集。能够应用单引号或者双引号。

TIMESTAMP

TIMESTAMP

工夫类型

BINARY

不反对

字节数组

留神:Impala 尽管反对 array,map,struct 简单数据类型,然而反对并不齐全,个别解决办法,将简单类型转化为根本类型,通过 hive 创立表。

第 5 章 DDL 数据定义

5.1 创立数据库

1. 创立命令

CREATE DATABASE [IF NOT EXISTS] database_name

[COMMENT database_comment]

[LOCATION hdfs_path];

注:Impala 不反对 WITH DBPROPERTIE…语法

  1. 谬误演示

[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 章 查问

  1. 根本的语法跟 hive 的查问语句大体一样
  2. Impala 不反对 CLUSTER BY, DISTRIBUTE BY, SORT BY
  3. Impala 中不反对分桶表
  4. Impala 不反对 COLLECT_SET(col) 和 explode(col)函数
  5. Impala 反对开窗函数

[hadoop103:21000] > select name,orderdate,cost,sum(cost) over(partition by month(orderdate)) from business;

第 8 章 函数

8.1 自定义函数

1.创立一个 Maven 工程 Hive

2.导入依赖

<dependencies>

<!– https://mvnrepository.com/art… –>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-exec</artifactId>

<version>1.2.1</version>

</dependency>

</dependencies>

3.创立一个类

package com.atguigu.hive;

import org.apache.hadoop.hive.ql.exec.UDF;

public class Lower extends UDF {

public String evaluate (final String s) {

if (s == null) {

return null;

}

return s.toLowerCase();

}

}

4.打成 jar 包上传到服务器 /opt/module/jars/udf.jar

  1. 将 jar 包上传到 hdfs 的指定目录

hadoop fs -put hive_udf-0.0.1-SNAPSHOT.jar /

  1. 创立函数

[hadoop103:21000] > create function mylower(string) returns string location ‘/hive_udf-0.0.1-SNAPSHOT.jar’ symbol=’com.atguigu.hive_udf.Hive_UDF’;

  1. 应用自定义函数

[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 是否可间接创立

是否可直接插入

Parquet

Snappy(默认), GZIP;

Yes

反对:CREATE TABLE, INSERT, 查问

Text

LZO,gzip,bzip2,snappy

Yes. 不指定 STORED AS 子句的 CREATE TABLE 语句,默认的文件格式就是未压缩文本

反对:CREATE TABLE, INSERT, 查问。如果应用 LZO 压缩,则必须在 Hive 中创立表和加载数据

RCFile

Snappy, GZIP, deflate, BZIP2

Yes.

仅反对查问,在 Hive 中加载数据

SequenceFile

Snappy, GZIP, deflate, BZIP2

Yes.

仅反对查问,在 Hive 中加载数据

注: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. 尽可能的做条件过滤

–c. 应用 limit 字句

–d. 输入文件时,防止应用丑化输入

–e. 尽量少用全量元数据的刷新

9、应用 profile 输入底层信息打算,在做相应环境优化

正文完
 0