接上一篇,本篇主要分享 Impala 的基本操作,包括表和数据的操作
一、Impala 操作命令
① Impala 的外部 shell
连接指定的 Impala
impala-shell -i ip2
使用
-q查询表中数据,并使用-o将数据写入文件中
impala-shell -q 'select * from demo_tbl' -o test.txt
在 hive 中创建表后,使用
-r刷新元数据
首先在 hive 中建表
hive> create table test_tbl(id int, name string);
然后在 Impala 中刷新元数据
impala-shell -r
在 Impala 中查看创建的 hive 表
show tables;
+---------+
| name |
+---------+
| test_tbl|
+---------+
使用
-p显示查询计划
impala-shell -p
select * from test_tbl;
使用
-B去格式化输出
impala-shell -q 'select * from test_tbl' -B --output_delimiter="\t" -o test_tbl.txt
② Impala 的内部 shell
查看执行计划
explain select * from test_tbl;
查看 hdfs 及 linux 文件系统
shell hadoop fs -ls /;
shell ls -al ./;
刷新指定表的元数据
load data local inpath 'test_tbl.txt' into table test_tbl;
select * from test_tbl;
refresh test_tbl;
select * from test_tbl;
查看历史命令
history;
二、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 | 不支持 | 字节数组 |
三、DDL 操作
1. 创建数据库
CREATE DATABASE [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path];
注:Impala 不支持 WITH DBPROPERTIE…语法,如下写法是错误的:
create database db_hive_test
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
2. 查询数据库
show databases;
show databases like 'demo*';
desc database hive_db;
3. 删除数据库
drop database hive_db;
drop database hive_db cascade;
注意:
Impala 不支持 alter database 语法
当数据库被 USE 语句选中时,无法删除
4. 创建表
创建内部表
create table if not exists test_system_tbl(
> id int, name string
> )
> row format delimited fields terminated by '\t'
> stored as textfile
> location '/user/hive/warehouse/test_system_tbl';
创建外部表
create external table test_external(
> id int,
> name string)
> row format delimited fields terminated by '\t' ;
5. 分区表操作
创建分区表
create table test_par(id int, name string)
> partitioned by (month string)
> row format delimited
> fields terminated by '\t';
增加分区
alter table test_par
add partition (month='202306')
partition (month='202307');
向分区表导入数据方式1
load data inpath 'test_par.txt' into table test_par partition(month='202306');
向分区表导入数据方式2
insert into table test_par partition (month = '202306')
按分区查询
select * from test_par where month = '202306';
删除分区
alter table test_par drop partition (month='202307');
查看分区
show partitions test_par;
四、DML 操作
1. 数据导入
基本和 hive 类似,但是不支持:load data local inpath…
2. 数据导出
Impala 数据导出使用impala -o
impala-shell -q 'select * from test_tbl' -B --output_delimiter="\t" -o test_tbl.txt
注意:
Impala 不支持 export 和 import 命令
Impala 不支持 insert overwrite… 语法导出数据
五、Impala 查询
和 hive 的查询语句基本一致
Impala 不支持 CLUSTER BY, DISTRIBUTE BY, SORT BY
Impala 不支持分桶表
Impala 不支持 COLLECT_SET(col) 和 explode(col)函数
Impala支持开窗函数
select goods_name, create_time, cost, sum(cost) over(partition by month(create_time)) from order_info;
往期推荐