数据定义
- 创建数据库 CREATE DATABASE (if not exists) name location ;
- show database like ‘g.*’;
- drop database if exists name cascade;
- create table if not exists database.tablename()(like db.tableName);
- alter table tableName rename to newName;
- alter table tableName
change column colName colNewName INT - alter table tableName add columns(
app_name String comment ‘application name’,
session_id long comment ‘The current session id’); - 删除或者替换列
数据操作
- 装载数据
LOAD DATA LOCAL INPATH ‘${env:HOME}/california-employees’
OVERWRITE INTO TABLE tableName
PARTITION (country = ‘US’,state = ‘CA’) - INSERT OVERWRITE TABLE tablename partition (country = ‘US’ , status = ‘OR’) select * from tableName se where se.cnty = ‘US’ and se.st = ‘OR’
- 动态分区:
INSERT OVERWRITE TABLE tablename PARTITION (country,state) SELECT …,se.cnty,se.st FROM staged_emplayees se; - 导入部分数据
CREATE TABLE new AS SELECT name,salary,address FROM oldTables WHERE … - 导出数据
查询
- 表生成函数:
- 抽样查询
select * FROM numbers TABLESAMPLE(BUCKET 3 OUT OF 10 ON rand()) s;