创建数据库
CREATE DATABASE IF NOT EXISTS database_name;
查看所有数据库
show databases
删除数据库
DROP DATABASE IF EXISTS sample_database;
进入数据库
use dbname
创建表
CREATE TABLE IF NOT EXISTS test.student
(name STRING, age INT, contact INT );
查看所有表
show tables
/////////指定HDFS路径建库
为了在HDFS文件系统中创建数据库,需要指定要创建数据库的位置,如下所示。
CREATE DATABASE IF NOT EXISTS database_name LOCATION hdfs_path;
实测会报无权限
CREATE DATABASE IF NOT EXISTS hdfstest LOCATION "/db/hdfstest";
改个有权限的目录即可:
CREATE DATABASE IF NOT EXISTS hdfstest LOCATION "/user/impala/db/hdfstest";
注:通过hadoop fs -ls /user类似命令找到有权限的目录
建表后会在hdfs数据库目录下创建表的目录
插入数据后数据会存在表目录中。
////////////insert
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 );
///////////查询
select * from employee
排序
select id,name,age,address,salary from employee order by id
select * from employee order by salary desc nulls last
select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]
可以使用关键字ASC或DESC分别按升序或降序排列表中的数据。
以同样的方式,如果我们使用NULLS FIRST,表中的所有空值都排列在顶行; 如果我们使用NULLS LAST,包含空值的行将最后排列。
分组查询
select name,sum(salary) from employee group by name
select name,sum(salary) from employee group by name having sum(salary)>20000
翻页查询
select * from employee order by id limit 2 offset 2
///////////////////union
select * from employee order by id limit 2
union
select * from employee order by id limit 4
select * from employee order by id limit 2
union all
select * from employee order by id limit 4
////////////////////with
with x as (select 1), y as (select 2) (select * from x union y);
with t1 as (select * from employee where age>30),
t2 as (select * from employee where age>25)
(select * from t1 union all select * from t2);
////////////////////////distinct
select distinct id,name from employee;
/////////////覆盖插入(表的所有数据都被删除)
Insert overwrite employee values (1, 'Ram', 26,
'Vishakhapatnam', 37000 )
///////获取表的描述(表结构)
describe employee
////////改表名
ALTER TABLE [old_db_name.]old_table_name RENAME TO [new_db_name.]new_table_name
ALTER TABLE hdfstest.employee RENAME TO hdfstest.users;
/////////添加列
ALTER TABLE users ADD COLUMNS (account_no BIGINT, phone_no BIGINT);
////////删除列
ALTER TABLE users DROP account_no;
/////////////更改列名和类型
ALTER TABLE users CHANGE phone_no e_mail string;
////////删除表
drop table if exists my_db.student;
///////清空表
truncate table_name;
////////创建视图
CREATE VIEW IF NOT EXISTS users_view AS
select name, age from users;
/////////修改视图
alter view users_view AS
select name, age,e_mail from users;
//////////删除视图
drop view users_view
参考链接:https://www.w3cschool.cn/impala/