impala sql清单
創建數據庫
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/
總結
以上是生活随笔為你收集整理的impala sql清单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ie直接编辑网页
- 下一篇: 商品评价判别,文本分类——学习笔记