impala(04)——impala的使用
程序员文章站
2022-03-05 15:00:24
...
impala的使用,首先通过impala-shell进入到impala的交互窗口
数据库操作
查看所有数据库
show databases;
创建与删除数据库
CREATE DATABASE IF NOT EXISTS mydb1;
drop database if exists mydb;
创建数据库表并指定数据库表数据存放hdfs的位置(与hive建表语法类似)
hdfs dfs -mkdir -p /input/impala
create external table t3(id int ,name string ,age int ) row format delimited fields terminated by '\t' location '/input/impala/external';
数据表操作
创建数据库表
创建student表, 创建employ表
CREATE TABLE IF NOT EXISTS mydb1.student (name STRING, age INT, contact INT );
create table employee (Id INT, name STRING, age INT,address STRING, salary BIGINT);
数据库表中插入数据
insert into employee (ID,NAME,AGE,ADDRESS,SALARY)VALUES (1, 'Ramesh', 32, 'Ahmedabad', 20000 );
insert into employee values (2, 'Khilan', 25, 'Delhi', 15000 );
Insert into employee values (3, 'kaushik', 23, 'Kota', 30000 );
Insert into employee values (4, 'Chaitali', 25, 'Mumbai', 35000 );
Insert into employee values (5, 'Hardik', 27, 'Bhopal', 40000 );
Insert into employee values (6, 'Komal', 22, 'MP', 32000 );
数据的覆盖
insert overwrite employee values (1, 'Ram', 26, 'Vishakhapatnam', 37000 );
执行覆盖之后,原始数据会丢失。导致执行之后,表中只剩余这一条数据。
另外一种建表语句
create table customer as select * from employee;
数据的查询
select * from employee;
select name,age from employee;
删除表
DROP table mydb1.employee;
清空表数据
truncate employee;
创建视图
CREATE VIEW IF NOT EXISTS employee_view AS select name, age from employee;
查看视图数据
select * from employee_view;
order by语句
基础语法
select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]
Select * from employee ORDER BY id asc;
group by 语句
Select name, sum(salary) from employee Group BY name;
having 语句
基础语法
select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]
按年龄对表进行分组,并选择每个组的最大工资,并显示大于20000的工资
select max(salary) from employee group by age having max(salary) > 20000;
limit语句
select * from employee order by id limit 4;
impala当中的数据表导入几种方式
第一种方式,通过load hdfs的数据到impala当中去
create table user(id int ,name string,age int ) row format delimited fields terminated by "\t";
准备数据user.txt并上传到hdfs的 /user/impala路径下去
1 xiaoer 22
2 zhangsan 33
3 lisi 44
4 wangwu 55
加载数据
load data inpath '/user/impala/' into table user;
查询加载的数据
select * from user;
如果查询不不到数据,那么需要刷新一遍数据表
refresh user;
第二种方式,通过查询已存在的表的数据导入
create table user2 as select * from user;