大数据开发工程师-数据分析引擎之Impala-2


数据分析引擎之Impala-2

3 Impala 高级内容

Impala之refresh命令

1
2
3
4
5
6
7
refresh的主要功能是负责重新加载指定表的最新元数据信息,以及增量加载表中新的数据文件的位置信息。

为什么要刷新表的最新元数据信息呢?

如果Impala中获取到的表的元数据不是最新的,那么在执行查询时得到的结果就不准确了。

正常情况下,我们在Impala中对表执行的操作都是通过Catalog服务管理的,表的元数据信息都是会实时更新的,不会导致元数据信息不准确。
1
2
3
4
5
6
7
但是针对一些特殊情况,会导致Impala无法自动感知到最新的元数据变化:

1:当Impala中的表对应的HDFS目录中的数据文件发生了变化的时候,也就是我们使用hdfs的put命令向表的数据目录中添加了新的文件,或者删除了老的文件,此时Impala中维护的元数据信息是感知不到这些数据文件变化的,这样会导致在impala中查询时获取的还是之前的老数据。

2:当我们在Hive中对Impala的表或者是Hive自己的表执行了alter table、insert、load data以及其他对表产生修改的SQL语句时,也会出现这种问题,这也就意味着Impala是无法自动识别到Hive中元数据的变化情况。

下面我们来演示一下这个问题:

第一种情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
首先查询一下之前创建的外部表im_external_table中的数据:
[cdh02:21000] default> select * from im_external_table;
Query: select * from im_external_table
Query submitted at: 2022-08-17 17:05:33 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=9e401c30460cf400:430872e00000000
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
+----+------+-----+---------------------+
Fetched 3 row(s) in 0.57s

然后在这个表对应的数据目录中再添加一个数据文件:
[root@cdh01 ~]# hdfs dfs -put im_external_table.dat /data/im_external_table/im_external_table.dat.2

接下来再查询一下这个表中的数据:
[cdh02:21000] default> select * from im_external_table;
Query: select * from im_external_table
Query submitted at: 2022-08-17 17:05:33 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=9e401c30460cf400:430872e00000000
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
+----+------+-----+---------------------+
Fetched 3 row(s) in 0.57s

此时发现查询出的数据还是之前的3条,现在其实应该是6条数据的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
我们尝试连到cdh03这个节点上执行查询,看看结果是什么样的:
[root@cdh01 ~]# impala-shell -i cdh03:21000
[cdh03:21000] default> select * from im_external_table;
Query: select * from im_external_table
Query submitted at: 2022-08-17 16:50:54 (Coordinator: http://cdh03:25000)
Query progress can be monitored at: http://cdh03:25000/query_plan?query_id=7f468abf085ce6c9:39a269c000000000
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
+----+------+-----+---------------------+
Fetched 3 row(s) in 0.45s

结果发现是一样的,都是返回3条数据。
1
2
3
4
5
6
7
8
9
10
11
12
那我们到Hive中查询一下看看:
hive> select * from im_external_table;
OK
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
Time taken: 7.494 seconds, Fetched: 6 row(s)

在Hive中发现是没有问题的,可以正常识别到最新的6条数据,从这一点上来看,还是Hive比较好用。
1
2
3
4
5
6
7
想要让Impala识别到这个表中数据文件的变化情况,就需要使用refresh刷新这个表的元数据了。
在任意一个Impalad节点中执行就行了,不需要在所有节点中都执行:
[cdh02:21000] default> refresh im_external_table;
Query: refresh im_external_table
Query submitted at: 2022-08-17 17:17:17 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=544612cf1ae43866:49d3db5500000000
Fetched 0 row(s) in 0.63s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
此时在cdh02节点中查询这个表中的数据,发现可以获取到最新的6条数据了。
[cdh02:21000] default> select * from im_external_table;
Query: select * from im_external_table
Query submitted at: 2022-08-17 17:18:16 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=1d4f432a45dce8d2:818cde9700000000
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
+----+------+-----+---------------------+
Fetched 6 row(s) in 0.69s

到cdh03中查询也是一样的

第二种情况

1
2
3
4
5
6
7
8
9
接下来我们演示一下第二种情况:
hive> load data local inpath '/root/im_external_table.dat' into table im_external_table;
Loading data to table default.im_external_table
OK
Time taken: 4.09 seconds

在Hive中使用load data向外部表im_external_table中继续添加数据文件:

其实这个操作就是将数据文件加载到了表对应的数据目录中,和我们前面使用hdfs的put命令达到的效果是一样的,所以这种方式加载的数据Impala也是识别不到的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
我们先到Hive中查询验证一下:
hive> select * from im_external_table;
OK
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
Time taken: 0.323 seconds, Fetched: 9 row(s)

此时可以查到9条数据,这是对的。

然后我们通过impala进行查询
结果发现查询到的还是6条数据,Impala没有识别到刚才Hive最新加载的数据。

所以还需要使用refresh刷新这个表的元数据了:
[cdh02:21000] default> refresh im_external_table;
Query: refresh im_external_table
Query submitted at: 2022-08-17 17:27:13 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=a4accef838a1907:af9a27d400000000
Fetched 0 row(s) in 0.57s

刷新之后,再查询表中的最新数据

这样就可以了

第三种情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
当你使用HDFS命令从表的数据目录中删除一个文件,在没有使用refresh 刷新表的时候,还是可以从表中查询到对应的数据:
使用hdfs命令从外部表im_external_table对应的数据目录中删除一个文件:

[root@cdh01 ~]# hdfs dfs -ls /data/im_external_table
Found 3 items
-rw-r--r-- 2 root supergroup 86 2022-08-17 15:06 /data/im_external_table/im_external_table.dat
-rw-r--r-- 2 root supergroup 86 2022-08-17 16:51 /data/im_external_table/im_external_table.dat.2
-rwxrwxrwx 2 root supergroup 86 2022-08-17 17:21 /data/im_external_table/im_external_table_copy_1.dat

[root@cdh01 ~]# hdfs dfs -rm -r /data/im_external_table/im_external_table.dat.2
22/08/17 17:31:14 INFO fs.TrashPolicyDefault: Moved: 'hdfs://cdh01:8020/data/im_external_table/im_external_table.dat.2' to trash at: hdfs://cdh01:8020/user/root/.Trash/Current/data/im_external_table/im_external_table.dat.2

[root@cdh01 ~]# hdfs dfs -ls /data/im_external_table
Found 2 items
-rw-r--r-- 2 root supergroup 86 2022-08-17 15:06 /data/im_external_table/im_external_table.dat
-rwxrwxrwx 2 root supergroup 86 2022-08-17 17:21 /data/im_external_table/im_external_table_copy_1.dat
1
2
3
4
5
6
7
8
9
10
11
12
本来这个表中有3个数据文件,现在删除1个数据文件之后,还剩下2个。

到Hive中查询一下这个表最新的数据:
hive> select * from im_external_table;
OK
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
1 zs 18 2001-01-01 10:10:10
2 ls 17 2002-02-02 12:12:12
3 jack 19 2000-09-21 18:10:10
Time taken: 0.264 seconds, Fetched: 6 row(s)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
此时发现只剩下6条数据了,这是正确的。

然后到Impala中查询一下:
[cdh02:21000] default> select * from im_external_table;
Query: select * from im_external_table
Query submitted at: 2022-08-17 17:32:43 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=c84a66bdb914d9f8:881769fb00000000
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
+----+------+-----+---------------------+
Fetched 9 row(s) in 0.46s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
此时发现还是可以查到已经删除的数据文件中的数据,这是因为那些数据被缓存到了内存中,此时需要重新使用refresh刷新元数据才能获取到最新的:
[cdh02:21000] default> refresh im_external_table;
Query: refresh im_external_table
Query submitted at: 2022-08-17 17:33:57 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=f243b446e42c15d3:8c2d24c000000000
Fetched 0 row(s) in 0.62s
[cdh02:21000] default> select * from im_external_table;
Query: select * from im_external_table
Query submitted at: 2022-08-17 17:33:59 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=e34d92b7bde5d898:ed17982000000000
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
| 1 | zs | 18 | 2001-01-01 10:10:10 |
| 2 | ls | 17 | 2002-02-02 12:12:12 |
| 3 | jack | 19 | 2000-09-21 18:10:10 |
+----+------+-----+---------------------+
Fetched 6 row(s) in 1.03s

使用refresh刷新后,就可以查询到最新的数据了。

invalidate metadata命令

1
2
3
我们前面讲的refresh,是负责重新加载指定表的最新元数据信息,以及增量加载表中新的数据文件的位置信息,这个操作可以认为是增量更新,所以说他是轻量级的,性能开销较小。

而现在要讲的这个invalidate metadata,他可以将表的元数据标记为过期,属于全量更新,性能开销较大。
1
2
Impala在对一个元数据标记为过期的表进行查询的时候,会自动重新加载这个表最新的元数据。
但是针对一个拥有很多分区的大表来说,执行invalidate metadata这个操作,成本相当高,因为它会重新加载这个表的所有元数据信息。此时使用refresh的话会比较好,因为它只需要增量加载新增加的数据文件的位置数据即可,成本比较低。
1
2
3
4
5
6
7
8
9
10
11
invalidate metadata命令的语法格式的这样的:
invalidate metadata [ table_name ]

他的后面可以指定表名,也可以不指定表名。

如果不指定表名,则Impala中缓存的所有表的元数据信息都会被标记为过期。
如果指定了表名,则只会将指定的表的元数据信息标记为过期。

针对单张表而言,invalidate metadata的性能开销也比refresh要高。在向表中添加了数据文件之后,建议优先使用refresh。

如果不确定哪个表的元数据发送了变化,想要全量更新表的元数据信息,建议使用invalidate metadata,这样比较方便,此时就不考虑性能开销了。
1
2
3
4
5
6
7
下面我们来演示一个invalidate metadata的典型使用场景

假设我在Hive中创建了一个表:h_t1
create table h_t1 (
id int,
name string
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
在Hive中可以查到这个表,也可以正常操作这个表(此时表中没有数据):
hive> show tables;
OK
h_t1
im_external_partition_table
im_external_table
im_inner_table
im_t1
Time taken: 0.167 seconds, Fetched: 5 row(s)
hive> select * from h_t1;
OK
Time taken: 0.279 seconds

但是在Impala中却找不到这个表,并且也无法使用。
[cdh02:21000] default> show tables;
Query: show tables
+-----------------------------+
| name |
+-----------------------------+
| im_external_partition_table |
| im_external_table |
| im_inner_table |
| im_t1 |
+-----------------------------+
Fetched 4 row(s) in 0.02s
[cdh02:21000] default> select * from h_t1;
Query: select * from h_t1
Query submitted at: 2022-08-17 18:10:45 (Coordinator: http://cdh02:25000)
ERROR: AnalysisException: Could not resolve table reference: 'h_t1'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
此时使用refresh就不好使了,因为impala中现在没有识别到这个表,使用refresh也会提示表不存在。
[cdh02:21000] default> refresh h_t1;
Query: refresh h_t1
Query submitted at: 2022-08-17 18:11:19 (Coordinator: http://cdh02:25000)
ERROR: AnalysisException: Table does not exist: default.h_t1

想要解决这个问题,就只能使用invalidate metadata了。
[cdh02:21000] default> invalidate metadata;
Query: invalidate metadata
Query submitted at: 2022-08-17 18:13:21 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=a04e1b083fdf92b0:a33f98fa00000000
Fetched 0 row(s) in 2.78s

此时再获取最新的表信息就可以看到了。
[cdh02:21000] default> show tables;
Query: show tables
+-----------------------------+
| name |
+-----------------------------+
| h_t1 |
| im_external_partition_table |
| im_external_table |
| im_inner_table |
| im_t1 |
+-----------------------------+
Fetched 5 row(s) in 0.02s
1
2
3
4
5
6
7
8
9
10
这样就可以正常使用了。
[cdh02:21000] default> select * from h_t1;
Query: select * from h_t1
Query submitted at: 2022-08-17 18:14:44 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=da4aa121cbb177bc:145a901300000000
Fetched 0 row(s) in 5.24s

这个操作对Impala共享使用Hive中的元数据提供了技术支持。

所以我们在工作中,通过Hive创建的表,在Impala中都是可以使用的,只是需要先同步一下元数据信息。

Impala的数据存储和数据压缩

1
2
3
前面我们在学习Hive的时候,详细分析了数据存储格式和数据压缩格式,那么这些数据存储格式和数据压缩格式在Impala中也是支持的。

Impala支持对TextFile、SequenceFile、Avro、RCFile、ORC、Parquet等数据存储格式进行读取,但是对于某些Impala支持的不太好的数据存储格式,Impala只能对其进行查询操作,无法写入数据。

image-20230620144954392

1
2
3
-存储格式:常见的数据存储格式,Hive中都支持,Impala也是可以都读取的。
-Impala是否可以直接创建表:意思是说在Impala中是否可以在创建表的时候指定这些存储格式,目前我们使用的Impala3.2.0版本,这些都是支持的。
-Impala是否可以直接插入数据:意思是说在Impala中是否可以直接使用insert into语句向这些数据存储格式的表中插入数据。目前来看只有TextFile和PARQUET格式支持,其他的数据格式需要在Hive中使用insert into语句插入数据。或者在Impala中使用load data命令也可以,但是前提是需要先把数据整理成对应的数据格式。
1
2
3
4
5
这是数据存储相关的内容。

针对数据压缩,建议参考前面在Hive扩展内容中讲的数据压缩步骤,在Hive中进行设置。

其实在实际工作中还是建议在Hive中创建和管理表,然后在Impala中只查询,这样是比较方便的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
下面演示一下Impala中RCFile格式的使用。

在impala中创建表:im_t1_rcfile
create table im_t1_rcfile (
id int,
name string
)stored as rcfile;

从表im_t1中查询数据,然后使用insert into向表im_t1_rcfile中插入数据:
[cdh02:21000] default> insert into im_t1_rcfile select id,name from im_t1;
Query: insert into im_t1_rcfile select id,name from im_t1
Query submitted at: 2022-08-22 16:28:18 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=3d40a3f6a8fcede7:6cb6af9500000000
ERROR: Cannot write to table with format RC_FILE. Impala only supports writing to TEXT and PARQUET.
1
2
3
4
此时发现insert语句执行报错,错误日志提示说不能向RCFILE格式的表中写入数据,目前Impala中只能支持向Text和PARQUET格式的表中写入数据。

所以这种情况下就需要在Hive中使用insert语句向这个表中插入数据了。
hive> insert into im_t1_rcfile select id,name from im_t1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
上面的SQL执行成功之后就可以在Hive中或者Impala中查询表im_t1_rcfile中的数据了。
在Hive中查询。
hive> select * from im_t1_rcfile;
OK
2 ls
1 zs

在Impala中查询。

注意:需要先刷新表,因为我们是在Hive中向这个表里面添加了数据,Impala中默认是无法识别数据变化的。
[cdh02:21000] default> select * from im_t1_rcfile;
Query: select * from im_t1_rcfile
Query submitted at: 2022-08-22 16:41:53 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=5e477ef6db24bd4e:41cc43b100000000
Fetched 0 row(s) in 0.15s
[cdh02:21000] default> refresh im_t1_rcfile;
Query: refresh im_t1_rcfile
Query submitted at: 2022-08-22 16:42:08 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=a34752b078041d31:c508e9f600000000
Fetched 0 row(s) in 0.29s
[cdh02:21000] default> select * from im_t1_rcfile;
Query: select * from im_t1_rcfile
Query submitted at: 2022-08-22 16:42:10 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=8c45b2e0d93bc9c4:b6abd30100000000
+----+------+
| id | name |
+----+------+
| 2 | ls |
| 1 | zs |
+----+------+
Fetched 2 row(s) in 0.14s
1
所以为了避免在Impala中操作表遇到问题,建议在Hive中统一创建、管理表,只在Impala中提供快速查询服务,并且最好在每次查询之前先刷新一下表,这样可以保证每次查询的都是最新的数据。

Impala SQL VS Hive SQL

Impala SQL中不支持的特性

1
2
3
4
5
6
下面总结一些在Hive SQL中支持,但是在Impala SQL中不支持的特性,大家以后在使用Impala的时候需要留意:
-不支持Date数据类型。(前面已演示过。)
-load data时不支持local。(前面已演示过。)
-SQL中不支持多个distinct。

简单来说就是在一个SQL的select语句中不能使用多个distinct。
SQL中不支持多个distinct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
举个例子:
在impala中,select语句中使用1个distinct是没有问题的。
[cdh02:21000] default> select count(distinct id) from im_t1;
Query: select count(distinct id) from im_t1
Query submitted at: 2022-08-22 16:44:40 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=774f8823c028e167:32b025a400000000
+--------------------+
| count(distinct id) |
+--------------------+
| 2 |
+--------------------+
Fetched 1 row(s) in 1.19s
[cdh02:21000] default> select count(distinct name) from im_t1;
Query: select count(distinct name) from im_t1
Query submitted at: 2022-08-22 16:45:36 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=945efae0a14826a:eac4102400000000
+----------------------+
| count(distinct name) |
+----------------------+
| 2 |
+----------------------+
Fetched 1 row(s) in 0.57s
1
2
3
4
5
6
7
8
9
10
11
12
13
在impala中,select语句中使用多个distinct语句是会报错的:
[cdh02:21000] default> select count(distinct id),count(distinct name) from im_t1;
Query: select count(distinct id),count(distinct name) from im_t1
Query submitted at: 2022-08-22 16:45:51 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=9647a6c4a515b813:94d79b5200000000
ERROR: ExecQueryFInstances rpc query_id=9647a6c4a515b813:94d79b5200000000 failed: Failed to get minimum memory reservation of 136.01 MB on daemon cdh02:22000 for query 9647a6c4a515b813:94d79b5200000000 due to following error: Memory limit exceeded: Could not allocate memory while trying to increase reservation.
Query(9647a6c4a515b813:94d79b5200000000) could not allocate 136.01 MB without exceeding limit.
Error occurred on backend cdh02:22000
Memory left in process limit: 143.38 MB
Query(9647a6c4a515b813:94d79b5200000000): Reservation=0 ReservationLimit=204.80 MB OtherMemory=0 Total=0 Peak=0
Memory is likely oversubscribed. Reducing query concurrency or configuring admission control may help avoid this error.

提示的是内存相关的错误,但是这个表里面就只有2条数据,其实还是因为它不支持这种语法。
1
2
3
4
5
在Hive中是可以支持这种语法的:
hive> select count(distinct id),count(distinct name) from im_t1;
OK
2 2
Time taken: 66.222 seconds, Fetched: 1 row(s)
不支持常见的复合数据类型
1
-不支持常见的复合数据类型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
例如在Hive中支持的array、map、struct这些复合数据类型,Impala中都不支持。
验证一下:
在Impala中执行下面这几个SQL建表语句(Hive中复合数据类型的建表语句)都是会报错的:
[cdh02:21000] default> create table stu(
> id int,
> name string,
> favors array<string>
> )row format delimited
> fields terminated by '\t'
> collection items terminated by ','
> lines terminated by '\n';
Query: create table stu(
id int,
name string,
favors array<string>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
lines terminated by '\n'
ERROR: ParseException: Syntax error in line 7:
collection items terminated by ','
^
Encountered: IDENTIFIER
Expected: AS, CACHED, ESCAPED, LINES, LOCATION, STORED, TBLPROPERTIES, UNCACHED, WITH

CAUSED BY: Exception: Syntax error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[cdh02:21000] default> create table stu2(
> id int,
> name string,
> scores map<string,int>
> )row format delimited
> fields terminated by '\t'
> collection items terminated by ','
> map keys terminated by ':'
> lines terminated by '\n';
Query: create table stu2(
id int,
name string,
scores map<string,int>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
ERROR: ParseException: Syntax error in line 7:
collection items terminated by ','
^
Encountered: IDENTIFIER
Expected: AS, CACHED, ESCAPED, LINES, LOCATION, STORED, TBLPROPERTIES, UNCACHED, WITH

CAUSED BY: Exception: Syntax error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[cdh02:21000] default> create table stu3(
> id int,
> name string,
> address struct<home_addr:string,office_addr:string>
> )row format delimited
> fields terminated by '\t'
> collection items terminated by ','
> lines terminated by '\n';
Query: create table stu3(
id int,
name string,
address struct<home_addr:string,office_addr:string>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
lines terminated by '\n'
ERROR: ParseException: Syntax error in line 7:
collection items terminated by ','
^
Encountered: IDENTIFIER
Expected: AS, CACHED, ESCAPED, LINES, LOCATION, STORED, TBLPROPERTIES, UNCACHED, WITH

CAUSED BY: Exception: Syntax error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
那我们在Hive中创建的复合数据类型,在Impala中是否支持查询呢?

下面验证一下,到Hive中创建一个带有复合数据类型(Hive中的复合数据类型案例中的建表语句)的表:
hive> create table student (
> id int comment 'id',
> name string comment 'name',
> favors array<string> ,
> scores map<string, int>,
> address struct<home_addr:string,office_addr:string>
> ) row format delimited
> fields terminated by '\t'
> collection items terminated by ','
> map keys terminated by ':'
> lines terminated by '\n';
OK
Time taken: 0.556 seconds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
准备测试数据:
[root@cdh01 ~]# vi student.data
1 zhangsan english,sing,swing chinese:80,math:90,english:100 bj,sh
2 lisi games,coding chinese:89,english:70,math:88 gz,sz

加载测试数据:
hive> load data local inpath '/root/student.data' into table student;
Loading data to table default.student
OK
Time taken: 2.937 seconds

在Hive中是可以正常查询的:
hive> select * from student;
OK
1 zhangsan ["english","sing","swing"] {"chinese":80,"math":90,"english":100} {"home_addr":"bj","office_addr":"sh"}
2 lisi ["games","coding"] {"chinese":89,"english":70,"math":88} {"home_addr":"gz","office_addr":"sz"}
Time taken: 0.761 seconds, Fetched: 2 row(s)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
到Impala中查询:

注意:需要先刷新元数据,否则无法识别在Hive中创建的表。
[cdh02:21000] default> invalidate metadata;
Query: invalidate metadata
Query submitted at: 2022-08-22 17:17:14 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=ea41e49792f60f79:6f565cc400000000
Fetched 0 row(s) in 4.48s

查询:
[cdh02:21000] default> select * from student;
Query: select * from student
Query submitted at: 2022-08-22 17:17:25 (Coordinator: http://cdh02:25000)
ERROR: NotImplementedException: Scan of table 'default.student' in format 'TEXT' is not supported because the table has a column 'favors' with a complex type 'ARRAY<STRING>'.
Complex types are supported for these file formats: PARQUET.

发现查询的时候报错了,提示Text数据类型不支持Array这些复合数据类型。
但是在PARQUET数据存储格式中是支持这些数据类型的。
不支持collect_list()/set()函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Impala中不支持collect_list()和collect_set()函数。
在Impala中创建表:student_favors

[cdh02:21000] default> create external table student_favors(
> name string,
> favor string
> )row format delimited
> fields terminated by '\t'
> location '/data/student_favors';
Query: create external table student_favors(
name string,
favor string
)row format delimited
fields terminated by '\t'
location '/data/student_favors'
+-------------------------+
| summary |
+-------------------------+
| Table has been created. |
+-------------------------+
Fetched 1 row(s) in 0.40s

准备测试数据:
[root@cdh01 ~]# vi student_favors.data
zs swing
zs footbal
zs sing
zs codeing
zs swing

加载数据。
[root@cdh01 ~]# hdfs dfs -put student_favors.data /data/student_favors

在Impala中查询数据(先刷新表)。
[cdh02:21000] default> refresh student_favors;
[cdh02:21000] default> select name,concat_ws(',',collect_list(favor)) as favor_list from student_favors group by name;
Query: select name,concat_ws(',',collect_list(favor)) as favor_list from student_favors group by name
Query submitted at: 2022-08-22 17:39:23 (Coordinator: http://cdh02:25000)
ERROR: AnalysisException: default.collect_list() unknown
1
错误提示collect_list()和collect_set()是未知函数。
不支持split()、explode()函数
1
2
Impala中不支持split()、explode()函数。
在Impala中创建表:student_favors_2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
准备测试数据:
[root@cdh01 ~]# vi student_favors_2.data
zs swing,footbal,sing
ls codeing,swing

加载数据:
[root@cdh01 ~]# hdfs dfs -put student_favors_2.data /data/student_favors_2

在Impala中查询数据(先刷新表)。
[cdh02:21000] default> refresh student_favors;
[cdh02:21000] default> select split(favorlist,',') from student_favors_2;
Query: select split(favorlist,',') from student_favors_2
Query submitted at: 2022-08-22 17:41:52 (Coordinator: http://cdh02:25000)
ERROR: AnalysisException: default.split() unknown
[cdh02:21000] default> select explode('') from student_favors_2;
Query: select explode('') from student_favors_2
Query submitted at: 2022-08-22 17:45:48 (Coordinator: http://cdh02:25000)
ERROR: AnalysisException: default.explode() unknown
[cdh02:21000] default> select name,favor_new from student_favors_2 lateral view explode(split(favorlist, ',')) table1 as favor_new ;
Query: select name,favor_new from student_favors_2 lateral view explode(split(favorlist, ',')) table1 as favor_new
Query submitted at: 2022-08-22 17:41:06 (Coordinator: http://cdh02:25000)
ERROR: ParseException: Syntax error in line 1:
...new from student_favors_2 lateral view explode(split(f...
^
Encountered: A reserved word cannot be used as an identifier: lateral
Expected: ADD, ALTER, AND, ARRAY, AS, ASC, BETWEEN, BIGINT, BINARY, BLOCK_SIZE, BOOLEAN, CACHED, CASCADE, CHANGE, CHAR, COMMENT, COMPRESSION, CROSS, DATE, DATETIME, DECIMAL, DEFAULT, DESC, DIV, REAL, DROP, ELSE, ENCODING, END, FLOAT, FOLLOWING, FROM, FULL, GROUP, IGNORE, HAVING, ILIKE, IN, INNER, INTEGER, IREGEXP, IS, JOIN, LEFT, LIKE, LIMIT, LOCATION, MAP, NOT, NULL, NULLS, OFFSET, ON, OR, ORDER, PARTITION, PARTITIONED, PRECEDING, PRIMARY, PURGE, RANGE, RECOVER, REGEXP, RENAME, REPLACE, RESTRICT, RIGHT, RLIKE, ROW, ROWS, SELECT, SET, SMALLINT, SORT, STORED, STRAIGHT_JOIN, STRING, STRUCT, TABLESAMPLE, TBLPROPERTIES, THEN, TIMESTAMP, TINYINT, TO, UNCACHED, UNION, USING, VALUES, VARCHAR, WHEN, WHERE, WITH, COMMA, IDENTIFIER

CAUSED BY: Exception: Syntax error
1
2
3
错误提示split()、explode()、lateral view都不支持。

从这可以看出来,Impala SQL和Hive SQL在细节层面还是存在一些区别的,大家使用的时候需要注意,不过基础的SQL语句都是可以兼容的。

Impala和Hive的典型应用场景

1
2
3
4
5
针对Impala和Hive的典型应用场景

在实际工作中,如果考虑稳定性,建议使用Hive,Hive的底层引擎可以考虑是MapReduce或者Tez。

如果考虑执行效率,建议使用Impala或者Hive on Spark。
1
2
3
4
5
6
7
针对Impala和Hive on Spark如何选择呢?

-从SQL兼容程度上来说,Hive on Spark是优于Impala的,因为Hive中的一些高级SQL函数Impala是不支持的。
-从任务执行的延迟上来说,Impala是优于Hive on Spark的,因为Hive on Spark每次执行都会提交一个Spark任务,这个任务的提交过程是需要消耗一定时间的。Impala的任务提交过程会更快。
-从资源利用率角度来说,Hive on Spark是优于Impala的。因为Impala默认是需要安装自己的集群,这个集群资源只能运行Impala任务,而Hive on Spark是可以在YARN上执行的,YARN的资源是共享的。不过在CDH5版本之后,Impala也开始支持Impala on YARN模式,他通过一个叫做Llama(Long-Lived Application Master)的中间组件协调Yarn和Impala,向Yarn资源管理器申请计算资源。
查看Impala的配置页面中的参数也可以看出来他目前支持ON YARN模式:
http://cdh01:7180/cmf/services/18/config

Impala集成HBase

1
2
3
4
5
6
为了解决HBase无法使用SQL实现数据分析的问题,可以通过Hive来实现,也可以通过Impala来实现。

通过Impala操作HBase时,需要在Hive中建表,然后把表同步到Impala中,无法直接在Impala中基于HBase创建表,因为Impala的CREATE TABLE功能中有很多语法并不支持。
所以大致的流程是这样的:HBase->Hive->Impala。

Impala不是直接读取HBase表的底层数据文件,而是通过HBase客户端API代码读取HBase表中的数据,只不过这些代码不需要我们开发了,我们直接写上层的SQL就可以了。

CDH平台中安装HBase

1
2
3
目前CDH平台中还没有安装HBase集群,在使用之前,需要先安装HBase(一主两从)。

添加服务。

image-20230620151444679

1
选中 HBase

image-20230620151509739

1
点击右下角的继续按钮

image-20230620151529650

1
2
自定义角色分配,根据集群中节点的负载情况给HBase REST Server和HBase Thrift Server选择合适的节点即可。
选好以后点击继续按钮

image-20230620151618579

1
默认不需要修改,直接点击继续按钮即可

image-20230620151641626

1
2
3
等待状态变为 已完成 ,点击继续按钮

点击完成按钮即可

image-20230620151700762

image-20230620151711600

1
重启Zookeeper,点击重启按钮

image-20230620151739109

1
点击重启过时服务按钮

image-20230620151808720

1
等待状态变为已完成,然后点击完成按钮即可。

image-20230620151826842

让Impala支持HBase

1
2
3
在Impala中修改配置,以支持HBase,否则Impala连不上HBase。

进入impala的配置页面,选中HBase、然后点击 保存更改按钮。

image-20230620151919324

1
重启Impala

image-20230620151940202

1
2
3
点击重启过时服务按钮

选中 重新部署客户端配置,然后点击立即重启按钮。

image-20230620152000974

image-20230620152012380

1
等待状态变为 已完成,点击完成按钮即可

image-20230620152035512

HBase->Hive->Impala

(1)在HBase中创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
下面开始实际操作一下这个流程。

(1)在HBase中创建表
hbase(main):004:0> create 'video_user','basic','extend'

解释:HBase的video_user表中存储用户信息,其中basic列族中存储用户基础信息,extend列族中存储用户扩展信息。

(2)向HBase表中初始化测试数据
put 'video_user','100001','basic:name','zs001'
put 'video_user','100001','basic:birthday','2006-07-21'
put 'video_user','100001','basic:email','fx0i2724@yeah.net'
put 'video_user','100001','basic:mobile','15208252251'
put 'video_user','100001','basic:r_time','2020-01-17 01:10:10'
put 'video_user','100001','extend:is_have_child','0'
put 'video_user','100001','extend:phone_brand','huawei'
put 'video_user','100001','extend:weight','67'
put 'video_user','100001','extend:height','160'


put 'video_user','200001','basic:name','zs002'
put 'video_user','200001','basic:birthday','2005-04-23'
put 'video_user','200001','basic:email','emau0i5i@sina.com'
put 'video_user','200001','basic:mobile','15105571420'
put 'video_user','200001','basic:r_time','2020-01-17 01:21:13'
put 'video_user','200001','extend:is_have_child','1'
put 'video_user','200001','extend:phone_brand','huawei'
put 'video_user','200001','extend:weight','63'
put 'video_user','200001','extend:height','168'


put 'video_user','300001','basic:name','zs003'
put 'video_user','300001','basic:birthday','2002-10-11'
put 'video_user','300001','basic:email','824c1s@hotmail.com'
put 'video_user','300001','basic:mobile','15701964301'
put 'video_user','300001','basic:r_time','2020-01-17 01:25:10'
put 'video_user','300001','extend:is_have_child','0'
put 'video_user','300001','extend:phone_brand','oppo'
put 'video_user','300001','extend:weight','53'
put 'video_user','300001','extend:height','170'

put 'video_user','400001','basic:name','zs004'
put 'video_user','400001','basic:birthday','2008-07-11'
put 'video_user','400001','basic:email','mgq14kt@3721.net'
put 'video_user','400001','basic:mobile','15802961143'
put 'video_user','400001','basic:r_time','2020-01-17 01:30:10'
put 'video_user','400001','extend:is_have_child','0'
put 'video_user','400001','extend:phone_brand','iphone'
put 'video_user','400001','extend:weight','68'
put 'video_user','400001','extend:height','180'

put 'video_user','500001','basic:name','zs005'
put 'video_user','500001','basic:birthday','2001-09-21'
put 'video_user','500001','basic:email','i4dwzt54b@yahoo.com.cn'
put 'video_user','500001','basic:mobile','13804117689'
put 'video_user','500001','basic:r_time','2020-01-17 02:13:12'
put 'video_user','500001','extend:is_have_child','1'
put 'video_user','500001','extend:phone_brand','xiaomi'
put 'video_user','500001','extend:weight','60'
put 'video_user','500001','extend:height','165'

put 'video_user','600001','basic:name','zs006'
put 'video_user','600001','basic:birthday','2002-11-11'
put 'video_user','600001','basic:email','cc33o9ak@sina.com'
put 'video_user','600001','basic:mobile','13206823730'
put 'video_user','600001','basic:r_time','2020-01-17 02:15:10'
put 'video_user','600001','extend:is_have_child','0'
put 'video_user','600001','extend:phone_brand','iphone'
put 'video_user','600001','extend:weight','69'
put 'video_user','600001','extend:height','175'

put 'video_user','700001','basic:name','zs007'
put 'video_user','700001','basic:birthday','2005-06-18'
put 'video_user','700001','basic:email','56vptzo5x@yeah.net'
put 'video_user','700001','basic:mobile','13307064763'
put 'video_user','700001','basic:r_time','2020-01-17 02:20:10'
put 'video_user','700001','extend:is_have_child','1'
put 'video_user','700001','extend:phone_brand','xiaomi'
put 'video_user','700001','extend:weight','55'
put 'video_user','700001','extend:height','177'

put 'video_user','800001','basic:name','zs008'
put 'video_user','800001','basic:birthday','2006-09-12'
put 'video_user','800001','basic:email','t3929y3k4@163.com'
put 'video_user','800001','basic:mobile','15200018067'
put 'video_user','800001','basic:r_time','2020-01-17 02:30:10'
put 'video_user','800001','extend:is_have_child','0'
put 'video_user','800001','extend:phone_brand','huawei'
put 'video_user','800001','extend:weight','57'
put 'video_user','800001','extend:height','170'

put 'video_user','900001','basic:name','zs009'
put 'video_user','900001','basic:birthday','2003-01-17'
put 'video_user','900001','basic:email','9i07c7ix@msn.com'
put 'video_user','900001','basic:mobile','15605340859'
put 'video_user','900001','basic:r_time','2020-01-17 02:33:12'
put 'video_user','900001','extend:is_have_child','1'
put 'video_user','900001','extend:phone_brand','huawei'
put 'video_user','900001','extend:weight','59'
put 'video_user','900001','extend:height','167'

put 'video_user','010001','basic:name','zs010'
put 'video_user','010001','basic:birthday','2004-05-12'
put 'video_user','010001','basic:email','ftewcpcj@3721.net'
put 'video_user','010001','basic:mobile','13105938235'
put 'video_user','010001','basic:r_time','2020-01-17 03:10:10'
put 'video_user','010001','extend:is_have_child','1'
put 'video_user','010001','extend:phone_brand','samsung'
put 'video_user','010001','extend:weight','64'
put 'video_user','010001','extend:height','168'
1
2
注意:为了避免rowkey出现热点,所以在向HBase中添加数据的时候,针对rowkey中的id字段的值进行了反转。
例如:900001,其实真正的id值是100009。

(3)在Hive中创建外部表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create external table video_user(
id string,
name string,
birthday string,
email string,
mobile string,
r_time timestamp,
is_have_child int,
phone_brand string,
weight int,
height int
)stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties(
"hbase.columns.mapping"=":key,basic:name,basic:birthday,basic:email,basic:mobile,basic:r_time,extend:is_have_child,extend:phone_brand,extend:weight,extend:height"
)
tblproperties("hbase.table.name" = "video_user");
1
2
3
4
5
解释:

-stored by后面指定的是表中数据存储的格式,这里表示使用HBase存储。
-with serdeproperties里面指定的是序列化和反序列化相关的参数,通过hbase.columns.mapping指定hive表字段和hbase表字段之间的映射关系,其中第一个:key字段表示是hbase中的rowkey字段,后面的字段会按照顺序和hive表字段进行映射,hbase.columns.mapping的值中间不能有空格和换行符。
-tblproperties里面指定表相关的信息,通过hbase.table.name指定hbase中的表名。
1
2
3
4
注意:

-id字段对应的是hbase中表的rowkey字段,这个字段类型建议指定为string类型,这样在SQL中对id进行过滤查询的时候可以将过滤条件翻译成针对HBase表中rowkey的条件查询,这样查询效率比较高。
-由于impala不支持date字段,所以birthday字段需要设置为string类型,在使用层面其实是没有区别的。

(4)将表同步到Impala中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@cdh01 ~]# impala-shell -i cdh02:21000
[cdh02:21000] default> invalidate metadata;
Query: invalidate metadata
Query submitted at: 2022-08-29 16:41:26 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=594ca9e564be417c:184e33dc00000000
Fetched 0 row(s) in 4.50s
[cdh02:21000] default> show tables;
Query: show tables
+-----------------------------+
| name |
+-----------------------------+
| h_t1 |
| im_external_partition_table |
| im_external_table |
| im_inner_table |
| im_t1 |
| im_t1_rcfile |
| student |
| student_favors |
| student_favors_2 |
| student_score |
| video_user |
+-----------------------------+
Fetched 11 row(s) in 0.02s
1
此时就可以在Impala中操作video_user这个表了,其实对应的就是操作hbase中的video_user表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[cdh02:21000] default> select * from video_user;
Query: select * from video_user
Query submitted at: 2022-08-29 17:10:15 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=d043dd6b4f6a29f4:81435a5700000000
+--------+------------+------------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
| id | birthday | email | mobile | name | r_time | height | is_have_child | phone_brand | weight |
+--------+------------+------------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
| 010001 | 2004-05-12 | ftewcpcj@3721.net | 13105938235 | zs010 | 2020-01-17 03:10:10 | 168 | 1 | samsung | 64 |
| 100001 | 2006-07-21 | fx0i2724@yeah.net | 15208252251 | zs001 | 2020-01-17 01:10:10 | 160 | 0 | huawei | 67 |
| 200001 | 2005-04-23 | emau0i5i@sina.com | 15105571420 | zs002 | 2020-01-17 01:21:13 | 168 | 1 | huawei | 63 |
| 300001 | 2002-10-11 | 824c1s@hotmail.com | 15701964301 | zs003 | 2020-01-17 01:25:10 | 170 | 0 | oppo | 53 |
| 400001 | 2008-07-11 | mgq14kt@3721.net | 15802961143 | zs004 | 2020-01-17 01:30:10 | 180 | 0 | iphone | 68 |
| 500001 | 2001-09-21 | i4dwzt54b@yahoo.com.cn | 13804117689 | zs005 | 2020-01-17 02:13:12 | 165 | 1 | xiaomi | 60 |
| 600001 | 2002-11-11 | cc33o9ak@sina.com | 13206823730 | zs006 | 2020-01-17 02:15:10 | 175 | 0 | iphone | 69 |
| 700001 | 2005-06-18 | 56vptzo5x@yeah.net | 13307064763 | zs007 | 2020-01-17 02:20:10 | 177 | 1 | xiaomi | 55 |
| 800001 | 2006-09-12 | t3929y3k4@163.com | 15200018067 | zs008 | 2020-01-17 02:30:10 | 170 | 0 | huawei | 57 |
| 900001 | 2003-01-17 | 9i07c7ix@msn.com | 15605340859 | zs009 | 2020-01-17 02:33:12 | 167 | 1 | huawei | 59 |
+--------+------------+------------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
Fetched 10 row(s) in 12.63s
1
此时这个SQL语句相当于我们使用Scan对HBase中的表进行全表扫描了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
这样表示会根据hbase表中的rowkey进行查询

[cdh02:21000] default> select * from video_user where id = '100001';
Query: select * from video_user where id = '100001'
Query submitted at: 2022-08-29 17:12:31 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=b043624845487ab2:a00f105800000000
+--------+------------+-------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
| id | birthday | email | mobile | name | r_time | height | is_have_child | phone_brand | weight |
+--------+------------+-------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
| 100001 | 2006-07-21 | fx0i2724@yeah.net | 15208252251 | zs001 | 2020-01-17 01:10:10 | 160 | 0 | huawei | 67 |
+--------+------------+-------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
Fetched 1 row(s) in 0.21s

此时这个SQL语句相当于我们在使用HBase中的Scan扫描数据时传入了RowFilter这个基于rowkey的过滤器,这个操作的查询效率还是比较高的。
1
2
3
4
5
6
7
8
9
10
11
这样表示会根据hbase表中的某个普通字段进行查询
[cdh02:21000] default> select * from video_user where name = 'zs002';
Query: select * from video_user where name = 'zs002'
Query submitted at: 2022-08-29 17:13:26 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=504ffd1cc5c62c73:a97d0f0200000000
+--------+------------+-------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
| id | birthday | email | mobile | name | r_time | height | is_have_child | phone_brand | weight |
+--------+------------+-------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
| 200001 | 2005-04-23 | emau0i5i@sina.com | 15105571420 | zs002 | 2020-01-17 01:21:13 | 168 | 1 | huawei | 63 |
+--------+------------+-------------------+-------------+-------+---------------------+--------+---------------+-------------+--------+
Fetched 1 row(s) in 0.53s
1
此时这个SQL语句相当于我们在使用HBase中的Scan扫描数据时传入了SingleColumnFilter过滤器,对普通列进行过滤查询,此时没有使用到rowkey,所以依然是全表扫描,效率一般。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
分组聚合查询:
[cdh02:21000] default> select is_have_child,count(*) from video_user group by is_have_child;
Query: select is_have_child,count(*) from video_user group by is_have_child
Query submitted at: 2022-08-29 17:14:49 (Coordinator: http://cdh02:25000)
Query progress can be monitored at: http://cdh02:25000/query_plan?query_id=ad4f2b0edb155853:5ec2ff0400000000
+---------------+----------+
| is_have_child | count(*) |
+---------------+----------+
| 0 | 5 |
| 1 | 5 |
+---------------+----------+
Fetched 2 row(s) in 0.77s

当然了,在Hive中执行这些查询操作也是可以的,只是查询速度没有在Impala中快。

本文标题:大数据开发工程师-数据分析引擎之Impala-2

文章作者:TTYONG

发布时间:2023年06月18日 - 23:06

最后更新:2023年06月21日 - 15:06

原始链接:http://tianyong.fun/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88-%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%BC%95%E6%93%8E%E4%B9%8BImpala-2.html

许可协议: 转载请保留原文链接及作者。

多少都是爱
0%