控制台命令与 SDK 映射
本文列出 MaxCompute 控制台(odpscmd)最常用的命令,以及如何通过 SDK 实现相同功能。如果你习惯使用 odpscmd,可以快速找到 SDK 中对应的 API。
项目与会话
use project — 切换默认项目
- Java
- Python
- Go
odps.setDefaultProject("my_project");
odps.project = 'my_project'
odpsIns.SetDefaultProjectName("my_project")
describe project / desc project — 查看项目信息
对应 SDK 中 Project 对象的 reload 和属性读取方法。
- Java
- Python
- Go
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());
}
project = odps.get_project('my_project')
print("Owner:", project.owner)
print("Comment:", project.comment)
print("创建时间:", project.creation_time)
project := odpsIns.Projects().Get("my_project")
err := project.Load()
fmt.Println("Owner:", project.Owner())
fmt.Println("Comment:", project.Comment())
setproject — 修改项目属性
setproject 命令可以查看或修改项目级别的属性,如 odps.sql.type.system.odps2、odps.sql.decimal.odps2 等。对应 SDK 中 Project 的 updateProject 方法。
- Java
- Python
- Go
// 查看所有项目属性(等价于不带参数的 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);
# 查看项目属性
project = odps.get_project()
print(project.properties)
# 修改项目属性
project.update_project(properties={
'odps.sql.type.system.odps2': 'true',
'odps.sql.decimal.odps2': 'true',
})
project := odpsIns.Projects().Get("my_project")
err := project.Load()
// 查看属性
props := project.Properties()
for k, v := range props {
fmt.Printf("%s=%s\n", k, v)
}
// 修改属性
newProps := map[string]string{
"odps.sql.type.system.odps2": "true",
}
err = project.UpdateProperties(newProps)
set key=value — 设置会话级标志
set 命令设置的是会话级别的标志(hint),影响后续 SQL 执行行为,并不修改服务端配置。SDK 中通过在提交 SQL 任务时附加 hints 实现。
- Java
- Python
- Go
// 在 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();
# 在 SQL 执行时附加 hints
hints = {
'odps.sql.mapper.split.size': '256',
'odps.sql.reducer.instances': '10',
}
odps.execute_sql(sql, hints=hints)
hints := map[string]string{
"odps.sql.mapper.split.size": "256",
"odps.sql.reducer.instances": "10",
}
task := odps.NewSQLTask("query", sql, "", hints)
instance, err := odpsIns.ExecTask(task)
list projects — 列出项目
- Java
- Python
- Go
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());
}
for project in odps.list_projects():
print(project.name)
projects := odpsIns.Projects()
err := projects.List(func(p *odps.Project, err error) {
fmt.Println(p.Name())
})
whoami — 查看当前用户
- Java
- Python
SecurityManager sm = odps.projects().get().getSecurityManager();
String result = sm.runQuery("whoami", false);
System.out.println(result);
result = odps.run_security_query("whoami")
print(result)
表操作
describe table / desc table — 查看表结构
desc 命令是最常用的元数据查看命令。对应 SDK 中 Table 对象的 reload 和 getSchema 方法。
- Java
- Python
- Go
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());
}
table = odps.get_table('my_table')
table.reload()
print("Owner:", table.owner)
print("Comment:", table.comment)
print("Schema:", table.table_schema)
for col in table.table_schema.columns:
print(f"{col.name:20s} {col.type:15s} {col.comment or ''}")
table := odpsIns.Tables().Get("my_table")
err := table.Load()
fmt.Println("Owner:", table.Owner())
fmt.Println("Comment:", table.Comment())
schema := table.Schema()
for _, col := range schema.Columns {
fmt.Printf("%-20s %-15s %s\n", col.Name, col.Type, col.Comment)
}
describe table partition / desc table partition — 查看分区详情
- Java
- Python
- Go
// 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());
table = odps.get_table('my_table')
partition = table.get_partition("dt='20250101'")
print("大小:", partition.size)
print("记录数:", partition.record_num)
table := odpsIns.Tables().Get("my_table")
partition := table.GetPartition(odps.NewPartitionSpec("dt='20250101'"))
err := partition.Load()
fmt.Println("大小:", partition.Size())
show tables — 列出表
- Java
- Python
- Go
// 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 tables
for table in odps.list_tables():
print(table.name)
# show tables like 'user%'
for table in odps.list_tables(prefix='user'):
print(table.name)
tables := odpsIns.Tables()
err := tables.List(func(t *odps.Table, err error) {
fmt.Println(t.Name())
})
show partitions — 列出分区
- Java
- Python
- Go
Table table = odps.tables().get("my_table");
Iterator<Partition> it = table.getPartitionIterator();
while (it.hasNext()) {
Partition p = it.next();
System.out.println(p.getPartitionSpec());
}
table = odps.get_table('my_table')
for partition in table.partitions:
print(partition.name)
table := odpsIns.Tables().Get("my_table")
partitions, err := table.GetPartitions()
for _, p := range partitions {
fmt.Println(p.PartitionSpec())
}
read table — 预览表数据
read 命令用于快速预览表中的少量数据,最多返回 1 万行。对应 SDK 中 Table 的 read 方法。
- Java
- Python
// 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));
}
# 等价于 read my_table 100
table = odps.get_table('my_table')
with table.open_reader() as reader:
for record in reader[:100]:
print(record[0])