跳到主要内容

控制台命令与 SDK 映射

本文列出 MaxCompute 控制台(odpscmd)最常用的命令,以及如何通过 SDK 实现相同功能。如果你习惯使用 odpscmd,可以快速找到 SDK 中对应的 API。

项目与会话

use project — 切换默认项目

odps.setDefaultProject("my_project");

describe project / desc project — 查看项目信息

对应 SDK 中 Project 对象的 reload 和属性读取方法。

Project project = odps.projects().get("my_project");
project.reload();
System.out.println("Owner: " + project.getOwner());
System.out.println("Comment: " + project.getComment());
System.out.println("创建时间: " + project.getCreatedTime());

// 获取项目属性
Map<String, String> properties = project.getProperties();
for (Map.Entry<String, String> entry : properties.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}

setproject — 修改项目属性

setproject 命令可以查看或修改项目级别的属性,如 odps.sql.type.system.odps2odps.sql.decimal.odps2 等。对应 SDK 中 Project 的 updateProject 方法。

// 查看所有项目属性(等价于不带参数的 setproject)
Project project = odps.projects().get();
Map<String, String> properties = project.getAllProperties();
for (Map.Entry<String, String> e : properties.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}

// 修改项目属性(等价于 setproject key=value)
Map<String, String> newProps = new HashMap<>();
newProps.put("odps.sql.type.system.odps2", "true");
newProps.put("odps.sql.decimal.odps2", "true");
odps.projects().updateProject(project.getName(), newProps);

set key=value — 设置会话级标志

set 命令设置的是会话级别的标志(hint),影响后续 SQL 执行行为,并不修改服务端配置。SDK 中通过在提交 SQL 任务时附加 hints 实现。

// 在 SQL 执行时附加 hints
Map<String, String> hints = new HashMap<>();
hints.put("odps.sql.mapper.split.size", "256");
hints.put("odps.sql.reducer.instances", "10");

Instance instance = SQLTask.run(odps, odps.getDefaultProject(), sql, hints, null);
instance.waitForSuccess();

list projects — 列出项目

ProjectFilter filter = new ProjectFilter();
filter.setOwner("ALIYUN$user@example.com");

Iterator<Project> it = odps.projects().iteratorByFilter(filter);
while (it.hasNext()) {
Project p = it.next();
System.out.println(p.getName());
}

whoami — 查看当前用户

SecurityManager sm = odps.projects().get().getSecurityManager();
String result = sm.runQuery("whoami", false);
System.out.println(result);

表操作

describe table / desc table — 查看表结构

desc 命令是最常用的元数据查看命令。对应 SDK 中 Table 对象的 reload 和 getSchema 方法。

Table table = odps.tables().get("my_project", "my_table");
table.reload();

System.out.println("Owner: " + table.getOwner());
System.out.println("创建时间: " + table.getCreatedTime());
System.out.println("最后修改: " + table.getLastMetaModifiedTime());
System.out.println("生命周期: " + table.getLife());
System.out.println("注释: " + table.getComment());

// 列信息
for (Column col : table.getSchema().getColumns()) {
System.out.printf("%-20s %-15s %s%n",
col.getName(), col.getTypeInfo(), col.getComment());
}

// 分区列
for (Column col : table.getSchema().getPartitionColumns()) {
System.out.printf("[分区列] %-20s %-15s%n", col.getName(), col.getTypeInfo());
}

describe table partition / desc table partition — 查看分区详情

// desc table my_table partition(dt='20250101')
Table table = odps.tables().get("my_table");
Partition partition = table.getPartition(new PartitionSpec("dt='20250101'"));
partition.reload();

System.out.println("创建时间: " + partition.getCreatedTime());
System.out.println("大小: " + partition.getSize());
System.out.println("记录数: " + partition.getRecordNum());

show tables — 列出表

// show tables
Iterator<Table> it = odps.tables().iterator("my_project");
while (it.hasNext()) {
Table t = it.next();
System.out.println(t.getName());
}

// show tables like 'user%'(按前缀过滤)
TableFilter filter = new TableFilter();
filter.setName("user");
Iterator<Table> filtered = odps.tables().iterator("my_project", filter);

show partitions — 列出分区

Table table = odps.tables().get("my_table");
Iterator<Partition> it = table.getPartitionIterator();
while (it.hasNext()) {
Partition p = it.next();
System.out.println(p.getPartitionSpec());
}

read table — 预览表数据

read 命令用于快速预览表中的少量数据,最多返回 1 万行。对应 SDK 中 Table 的 read 方法。

// read my_table 100
Table table = odps.tables().get("my_table");
RecordReader reader = table.read(100);
Record record;
while ((record = reader.read()) != null) {
System.out.println(record.get(0));
}

详见 数据预览

drop table — 删除表

// drop table my_table
odps.tables().delete("my_table");

// drop table if exists my_table
odps.tables().delete("my_table", true);

实例操作

show instances / show p — 列出实例

show instances(简写 show p)列出当前项目下的实例。对应 SDK 中的实例迭代器。

InstanceFilter filter = new InstanceFilter();
filter.setStatus(Instance.Status.RUNNING);

Iterator<Instance> it = odps.instances().iterator(filter);
while (it.hasNext()) {
Instance inst = it.next();
System.out.printf("%s %s %s%n", inst.getId(), inst.getStatus(), inst.getStartTime());
}

status instance — 查看实例状态

Instance instance = odps.instances().get("instance_id");
System.out.println("状态: " + instance.getStatus());

// 查看各 Task 的状态
Map<String, Instance.TaskStatus> taskStatus = instance.getTaskStatus();
for (Map.Entry<String, Instance.TaskStatus> entry : taskStatus.entrySet()) {
System.out.printf("Task %s: %s%n", entry.getKey(), entry.getValue().getStatus());
}

kill instance — 终止实例

Instance instance = odps.instances().get("instance_id");
instance.stop();

wait instance — 等待实例完成

Instance instance = odps.instances().get("instance_id");
instance.waitForSuccess();

// 获取结果
Map<String, String> results = instance.getTaskResults();
System.out.println(results.get("AnonymousSQLTask"));

资源与函数

add resource / add file / add jar / add py — 上传资源

add 命令用于上传文件、JAR 包或 Python 脚本作为 MaxCompute 资源。对应 SDK 中的 Resources.create 方法。

// add file my_file.txt
FileResource resource = new FileResource();
resource.setName("my_file.txt");
try (FileInputStream in = new FileInputStream("/path/to/my_file.txt")) {
odps.resources().create(resource, in);
}

// add jar my_udf.jar -f(-f 表示覆盖更新)
JarResource jarRes = new JarResource();
jarRes.setName("my_udf.jar");
try (FileInputStream in = new FileInputStream("/path/to/my_udf.jar")) {
odps.resources().update(jarRes, in);
}

详见 资源管理

create function — 创建函数

// create function my_udf as 'com.example.MyUDF' using 'my_udf.jar'
Function function = new Function();
function.setName("my_udf");
function.setClassPath("com.example.MyUDF");
function.setResources(Collections.singletonList(odps.resources().get("my_udf.jar")));
odps.functions().create(function);

详见 函数管理

show resources / show functions — 列出资源和函数

// show resources
Iterator<Resource> resIt = odps.resources().iterator();
while (resIt.hasNext()) {
Resource r = resIt.next();
System.out.printf("%s %s %s%n", r.getName(), r.getType(), r.getLastModifiedTime());
}

// show functions
Iterator<Function> funcIt = odps.functions().iterator();
while (funcIt.hasNext()) {
Function f = funcIt.next();
System.out.printf("%s %s%n", f.getName(), f.getClassPath());
}

drop resource / drop function — 删除资源和函数

// drop resource my_file.txt
odps.resources().delete("my_file.txt");

// drop function my_udf
odps.functions().delete("my_udf");

权限与安全

grant / revoke — 授权与撤销

grant 和 revoke 命令用于管理 ACL 权限。对应 SDK 中 SecurityManager 的 runQuery 方法。

SecurityManager sm = odps.projects().get().getSecurityManager();

// grant Select on table my_table to user ALIYUN$user@example.com
sm.runQuery("GRANT Select ON TABLE my_table TO USER ALIYUN$user@example.com", false);

// revoke Select on table my_table from user ALIYUN$user@example.com
sm.runQuery("REVOKE Select ON TABLE my_table FROM USER ALIYUN$user@example.com", false);

show grants — 查看权限

SecurityManager sm = odps.projects().get().getSecurityManager();

// show grants for current user
String result = sm.runQuery("SHOW GRANTS", false);
System.out.println(result);

// show grants for specific user
result = sm.runQuery("SHOW GRANTS FOR USER ALIYUN$user@example.com", false);
System.out.println(result);

详见 权限校验ACL 查询

show securityconfiguration — 查看安全配置

SecurityManager sm = odps.projects().get().getSecurityManager();
SecurityConfiguration config = sm.getSecurityConfiguration();

System.out.println("LabelSecurity: " + config.isLabelSecurityEnabled());
System.out.println("ProjectProtection: " + config.isProjectProtectionEnabled());
System.out.println("ObjectCreatorHasAccess: " + config.isObjectCreatorHasAccessEnabled());
System.out.println("ObjectCreatorHasGrant: " + config.isObjectCreatorHasGrantEnabled());

SQL 执行

SQL 查询 — 执行 SQL 语句

控制台中直接输入 SQL 语句即可执行。SDK 中通过 SQLTask 提交。

Instance instance = SQLTask.run(odps, "SELECT * FROM my_table LIMIT 10;");
instance.waitForSuccess();
Map<String, String> results = instance.getTaskResults();
System.out.println(results.get("AnonymousSQLTask"));

详见 执行 SQL

cost sql — 预估 SQL 费用

// 通过 dry run 模式估算费用
Instance instance = SQLTask.run(odps, odps.getDefaultProject(),
"SELECT * FROM my_table;", "SQL", null, null, null, true);
instance.waitForSuccess();
System.out.println(instance.getTaskResults().get("AnonymousSQLTask"));

数据通道

tunnel upload — 上传数据

TableTunnel tunnel = new TableTunnel(odps);
UploadSession session = tunnel.buildUploadSession()
.setProjectName("my_project")
.setTableName("my_table")
.build();

try (TunnelRecordWriter writer = session.openRecordWriter(0)) {
Record record = session.newRecord();
record.set(0, "value");
writer.write(record);
}
session.commit(new long[]{0});

详见 Tunnel 上传

tunnel download — 下载数据

TableTunnel tunnel = new TableTunnel(odps);
DownloadSession session = tunnel.buildDownloadSession()
.setProjectName("my_project")
.setTableName("my_table")
.build();

try (TunnelRecordReader reader = session.openRecordReader(0, session.getRecordCount())) {
while (reader.hasNext()) {
Record record = reader.next();
System.out.println(record.get(0));
}
}

详见 Tunnel 下载

Quota 管理

show quotas — 列出配额

Iterator<Quota> it = odps.quotas().iterator();
while (it.hasNext()) {
Quota q = it.next();
System.out.println(q.getName());
}

desc quota — 查看配额详情

Quota quota = odps.quotas().get("quota_name");
System.out.println("名称: " + quota.getName());
System.out.println("集群: " + quota.getCluster());

快速对照表

控制台命令SDK 对应文档链接
use projectodps.setDefaultProject()
desc projectProject.reload() + 属性方法
setproject key=valProjects.updateProject()
set key=valSQL hints 参数
list projectsProjects.iteratorByFilter()
whoamiSecurityManager.runQuery("whoami")
desc tableTable.reload() + Table.getSchema()修改表
show tablesTables.iterator()创建表
show partitionsTable.getPartitionIterator()分区管理
read tableTable.read()数据预览
drop tableTables.delete()
show instances / show pInstances.iterator()
status instanceInstance.getStatus()
kill instanceInstance.stop()
wait instanceInstance.waitForSuccess()
add file/jar/pyResources.create()资源管理
create functionFunctions.create()函数管理
show resourcesResources.iterator()资源管理
show functionsFunctions.iterator()函数管理
drop resourceResources.delete()资源管理
drop functionFunctions.delete()函数管理
grant/revokeSecurityManager.runQuery()ACL 查询
show grantsSecurityManager.runQuery()权限校验
SQL 语句SQLTask.run()执行 SQL
cost sqlSQLTask dry run
tunnel uploadTableTunnel + UploadSessionTunnel 上传
tunnel downloadTableTunnel + DownloadSessionTunnel 下载
show quotasQuotas.iterator()
desc quotaQuotas.get()