阿里云odps基本语法

odps sql:与hive sql语法基本一致

odpscmd.bat

SQL语句不分大小写,使用“–”进行注释,使用分号作为语句结束符号

数据定义语言(DDL),数据操作语言(DML),数据控制语言(DCL)和事务控制语言(TCL);在ODPS中使用的主要是DDL跟DML

查看表空间:1:show tables; 2:ls tables;3:list tables;

查看表:desc biaoming;

切库:use 库名;

退出:1:quit;2:q;

上传文本txt到对应分区表:tunnel upload -h true -fd '\t' C:\Users\Desktop\stg_police_situation_nature_ps.txt  odps_prod.stg_police_situation_nature_ps/dt='20180724',adcode='330100';

下载数据:tunnel download  odps_prod.ods_tfc_area_range_znkk/dt='20180701' C:\Users\Desktop\daduixiaqu.txt;

查看最近的历史记录操作:tunnel show history -n 5;

修改表名:alter table aaa_ps rename to aaa_znkk;

修改表注释:alter table table_name set comment 'table_new_name';

修改表的生命周期:alter table table_name set lifecycle days;(其中day为正整数天)

查看分区:show partitions 表名;

删除分区:alter table 表名 drop partition(dt='20180601');

删除数据:TRUNCATE TABLE table_name;

创建分区;alter table odps_prod.dwd_tfc_rltn_wide_custinter_inter add partition(adcode='330100',data_version='20180724');

清空表数据:truncate table temp_a;

下载项目上的资源(.py,.jar等):get resource judgelocation.jar C:\Users\Desktop\;

DDL语句

1.表的创建

(1)新建一个自定义的表:create table if not exists A (字段名 字段类型);

(2)新建一个表,字段与现有表一致,但数据并没有复制:create table if not exists A like B;

2.表的删除

drop table A; 

3.表的重命名

alter table A rename to B; 

4.表的生命周期

alter table A set lifecycles 30;

5.增加字段

alter table A add columns(字段名 字段类型); 

6.查看表的信息

desc A 

7修改表的字段或者注释:

alter table tablename change column col_old_name col_new_name column_type comment'';

eg:

DML语句 

1.表中数据的更新(insert语句)

(1)insert overwrite table A select * from B :表示将表A的数据复制到表B中,前提是字段一致;

PS:ODPS平台的SQL仅支持以上的更新数据库语句;其中关键字overwrite表示覆盖写入,可换成into表示追尾写入;

2表中数据的读取(select语句)

(1)select * from A:读取全部字段数据

(2)select uid from A:读取某一字段全部数据

(3)select distinct uid from A :读取某一字段不重复数据

(4)select * from A where uid=’a’:读取某一字段值等于指定值的数据

(5)select * from (select * from A)a:嵌套语句 

(6)select sum(uid) from A group by uid:分组查询 

(7)select * from A order by uid limit 4:全局排序,必须与limit 配合使用

(8)select row_number() over (partition by item order by score)as row_num from A:对字段item按字段score值进行局部排序并给出序号

3.表的合并(union all 语句)

select * from( 

select * from t1 where uid=’a’ 

union all 

select * from t2 where uid=’a’)t;

4.表的连接(join 语句与left outer join 语句)

(1)join

select * from A a join B b on a.item = b.item :返回A,B表中字段item值相同的数据

(2)left outer join

select * from A a left outer join B b on a.item = b.item :返回A表全部数据

(四)内建函数

查看附件文档 

MaxCompute-sql:https://help.aliyun.com/document_detail/27860.html

用户和权限管理-------------

向项目空间中添加用户:add user username;eg:add user [email protected]

查看用户:list users;

删除用户:remove user username;

授限:向user_name授予名为user_project_name的project的createTable创建表权限

grant CreateTable on project $user_project_name to user user_name;

获取表信息的权限

grant Describe to table $user_table_name to user user_name;

函数执行权限

grant Execute on function $user_function_name to user user_name;

创建角色:create role role_name;

向角色中添加用户:grant role_name to user_name;

将用户移除角色:remove role_name from user_name;

删除角色:drop role role_name;

表操作

创建带有生命周期的分区表:create table test3 (key boolean) partitioned by (pt string, ds 

string) lifecycle 100;

获取表信息:desc table_name;

删除表:drop table table_name;

UDF

CREATE FUNCTION test_lower AS org.alidata.odps.udf.examples.Lower 

USING my_lower.jar; 

在 sql中使用此函数: 

select test_lower('A') from my_test_table; 

数据操作

注意事项

mapjoin中,最多支持6张小表的mapjoin;

ODPS SQL 目前最多支持 128 个并发 union 操作; 

最多支持 128 个并发 insert overwrite/into 操作;


猜你喜欢

转载自blog.51cto.com/13184837/2402084