public class SQLTask extends Task
Task.Properties, Task.Property
Modifier and Type | Field and Description |
---|---|
static String |
AnonymousSQLTaskName |
Constructor and Description |
---|
SQLTask() |
Modifier and Type | Method and Description |
---|---|
String |
getCommandText()
获取 Task 命令信息
|
String |
getQuery() |
static String |
getRawResult(Instance instance) |
static String |
getRawResult(Instance instance,
String taskName) |
static List<Record> |
getResult(Instance instance)
Return 1W records at most.
|
static List<Record> |
getResult(Instance instance,
String taskName)
Return 1W records at most.
|
static List<Record> |
getResultByInstanceTunnel(Instance instance)
使用 instance tunnel 的方式获取 Anonymous task 的结果
|
static List<Record> |
getResultByInstanceTunnel(Instance instance,
Long limit)
使用 instance tunnel 的方式获取 Anonymous task 的结果
|
static List<Record> |
getResultByInstanceTunnel(Instance instance,
String taskName)
使用 instance tunnel 的方式获取 instance 结果
当数据量不大于
#SQL_TASK_RESULT_LIMIT 条时,成功;
若数据量量大于 #SQL_TASK_RESULT_LIMIT 条, 返回 SQL_TASK_RESULT_LIMIT 条结果;
可使用 getResultByInstanceTunnel(Instance, String, Long) 接口调整 limit 参数, 来获取更多数据; |
static List<Record> |
getResultByInstanceTunnel(Instance instance,
String taskName,
Long limit)
Return 1W records with completed schema info at most.
|
static List<Record> |
getResultByInstanceTunnel(Instance instance,
String taskName,
Long limit,
boolean limitEnabled) |
static TableSchema |
getResultSchema(Instance instance)
获取运行 SQL 运行结果的 schema 信息。
|
static ResultSet |
getResultSet(Instance instance)
通过instance获取记录迭代器,从而可以让用户通过迭代器逐条获取记录来避免一次性获取全量数据到本地时撑爆内存的问题
注:本接口使用 instance tunnel 实现,没有记录数限制,可获取instance对应query结果集的全量数据。
但是只有instance的owner本人可以使用本接口,且当对应project打开protection时,
需要提前在policy中为对应SQL中涉及的相应表和视图添加exception,否则无权下载
|
static ResultSet |
getResultSet(Instance instance,
Long limit)
通过instance获取记录迭代器,从而可以让用户通过迭代器逐条获取记录来避免一次性获取全量数据到本地时撑爆内存的问题
注:本接口使用 instance tunnel 实现,没有记录数限制,可获取instance对应query结果集的全量数据。
但是只有instance的owner本人可以使用本接口,且当对应project打开protection时,
需要提前在policy中为对应SQL中涉及的相应表和视图添加exception,否则无权下载
|
static ResultSet |
getResultSet(Instance instance,
String taskName)
通过instance获取记录迭代器,从而可以让用户通过迭代器逐条获取记录来避免一次性获取全量数据到本地时撑爆内存的问题
注:本接口使用 instance tunnel 实现,没有记录数限制,可获取instance对应query结果集的全量数据。
但是只有instance的owner本人可以使用本接口,且当对应project打开protection时,
需要提前在policy中为对应SQL中涉及的相应表和视图添加exception,否则无权下载
|
static ResultSet |
getResultSet(Instance instance,
String taskName,
InstanceTunnel instanceTunnel,
Long limit,
boolean limitHint) |
static ResultSet |
getResultSet(Instance instance,
String taskName,
Long limit)
通过instance获取记录迭代器,从而可以让用户通过迭代器逐条获取记录来避免一次性获取全量数据到本地时撑爆内存的问题
注:本接口使用 instance tunnel 实现,没有记录数限制,可获取instance对应query结果集的全量数据。
但是只有instance的owner本人可以使用本接口,且当对应project打开protection时,
需要提前在policy中为对应SQL中涉及的相应表和视图添加exception,否则无权下载
|
static ResultSet |
getResultSet(Instance instance,
String taskName,
Long limit,
boolean limitHint)
通过instance获取记录迭代器,从而可以让用户通过迭代器逐条获取记录来避免一次性获取全量数据到本地时撑爆内存的问题
注:
1.只有instance的owner本人可以使用本接口
2.当limitHint为true时,结果最多只能获得1条记录,超过将截断,但无需进行逐表的权限检查
3.当limitHint为false时,没有记录数限制,可获取instance对应query结果集的全量数据。但前提是需要逐表(SQL中
涉及的表与视图)对用户进行权限检查,所以当查询涉及表所在project打开protection时,需要提前在policy中为相应表
和视图添加exception,否则无权下载
|
static ResultSet |
getResultSet(Instance instance,
String taskName,
Long limit,
boolean limitHint,
URI tunnelEndpoint)
通过instance获取记录迭代器,从而可以让用户通过迭代器逐条获取记录来避免一次性获取全量数据到本地时撑爆内存的问题
注:本接口使用 instance tunnel 实现,没有记录数限制,可获取instance对应query结果集的全量数据。
1.只有instance的owner本人可以使用本接口
2.当limitHint为true时,结果最多只能获得1条记录,超过将截断,但无需进行逐表的权限检查
3.当limitHint为false时,没有记录数限制,可获取instance对应query结果集的全量数据。但前提是需要逐表(SQL中
涉及的表与视图)对用户进行权限检查,所以当查询涉及表所在project打开protection时,需要提前在policy中为相应表
和视图添加exception,否则无权下载
|
static List<String> |
getSqlWarning(Instance instance)
获取运行 SQL 的编译警告信息,可能有多条.
|
static List<String> |
getSqlWarning(Instance instance,
String taskName)
获取运行 SQL 的编译警告信息,可能有多条.
|
static List<Record> |
parseCsvRecord(String csvResult)
解析 CSV 格式的数据字符串,转换为 record 列表
|
static void |
removeDefaultHints() |
static Instance |
run(Odps odps,
String sql)
运行 SQL.
特别注意,在执行数据读取操作时: 正常情况下的 task 执行后,task 的状态为 SUCCESS,并正常返回数据结果。 但是,当读取数据量超过 10MB,task 的状态将是 FAILED,返回的数据结果为 error message。 因此,大量数据的获取建议使用 TableTunnel 进行操作。示例代码: |
static Instance |
run(Odps odps,
String sql,
SQLTaskOption option) |
static Instance |
run(Odps odps,
String project,
String sql,
Map<String,String> hints,
Map<String,String> aliases)
运行SQL
|
static Instance |
run(Odps odps,
String project,
String sql,
String taskName,
Map<String,String> hints,
Map<String,String> aliases) |
static Instance |
run(Odps odps,
String project,
String sql,
String taskName,
Map<String,String> hints,
Map<String,String> aliases,
int priority)
Deprecated.
|
static Instance |
run(Odps odps,
String project,
String sql,
String taskName,
Map<String,String> hints,
Map<String,String> aliases,
Integer priority)
运行SQL
|
static Instance |
run(Odps odps,
String project,
String sql,
String taskName,
Map<String,String> hints,
Map<String,String> aliases,
Integer priority,
String mcqaConnHeader)
运行SQL
|
static void |
setDefaultHints(Map<String,String> hints) |
void |
setQuery(String query)
设置SQL查询语句
|
getComment, getName, getProperties, setComment, setName, setProperties, setProperty
public static final String AnonymousSQLTaskName
public String getQuery()
public static void removeDefaultHints()
public void setQuery(String query)
query
- 需要执行的SQL查询public static List<Record> parseCsvRecord(String csvResult) throws OdpsException
csvResult
- 数据字符串OdpsException
public static List<Record> getResult(Instance instance, String taskName) throws OdpsException
{ String taskName = "test_select_sql_task"; Instance i = SQLTask.run(odps, odps.getDefaultProject(), "select * from test_select_sql_result;", taskName, null, null, 3); instance.waitForSuccess(); Listrecords = SQLTask.getResult(i, taskName); }
instance
- OdpsException
#getResultByInstanceTunnel(Instance, String, Long)}
public static List<Record> getResultByInstanceTunnel(Instance instance, String taskName, Long limit) throws OdpsException, IOException
{ String taskName = "test_select_sql_task"; Instance i = SQLTask.run(odps, odps.getDefaultProject(), "select * from test_select_sql_result;", taskName, null, null, 3); instance.waitForSuccess(); List使用 instance tunnel 的方式获取 task 结果. 本接口与records = SQLTask.getResultByInstanceTunnel(i, taskName, 1000L); }
getResult(Instance, String)
略有不同:
1) 本接口返回的 record 带有完整 schema 信息;
2) 本接口返回的 record 结果集有 1W 的条数限制,但不再有 10M 的大小限制
注意 : 返回结果类型为 List
, 数据量较大时会带来较多内存开销,
大数据量下载建议直接使用 InstanceTunnel
或 getResultSet(Instance, String)
;instance
- instance 对象taskName
- task 名称limit
- 获取结果的数量OdpsException,
- IOExceptionOdpsException
IOException
public static List<Record> getResultByInstanceTunnel(Instance instance, String taskName, Long limit, boolean limitEnabled) throws OdpsException, IOException
OdpsException
IOException
public static List<Record> getResultByInstanceTunnel(Instance instance, Long limit) throws OdpsException, IOException
instance
- instance 对象limit
- 获取结果的数量OdpsException,
- IOExceptionOdpsException
IOException
getResultByInstanceTunnel(Instance, String, Long)
public static List<Record> getResultByInstanceTunnel(Instance instance, String taskName) throws OdpsException, IOException
#SQL_TASK_RESULT_LIMIT
条时,成功;
若数据量量大于 #SQL_TASK_RESULT_LIMIT
条, 返回 SQL_TASK_RESULT_LIMIT 条结果;
可使用 getResultByInstanceTunnel(Instance, String, Long)
接口调整 limit 参数, 来获取更多数据;instance
- instance 对象taskName
- task 名称OdpsException,
- IOExceptionOdpsException
IOException
public static List<Record> getResultByInstanceTunnel(Instance instance) throws OdpsException, IOException
instance
- instance 对象OdpsException,
- IOExceptionOdpsException
IOException
getResultByInstanceTunnel(Instance, String)
public static List<Record> getResult(Instance instance) throws OdpsException
{ Instance i = SQLTask.run(odps, "select * from test_select_sql_result;"); instance.waitForSuccess(); Listrecords = SQLTask.getResult(i); }
instance
- OdpsException
public static ResultSet getResultSet(Instance instance) throws OdpsException, IOException
instance
- OdpsException
IOException
public static ResultSet getResultSet(Instance instance, String taskName) throws OdpsException, IOException
instance
- taskName
- OdpsException
IOException
public static ResultSet getResultSet(Instance instance, Long limit) throws OdpsException, IOException
instance
- limit
- OdpsException
IOException
public static ResultSet getResultSet(Instance instance, String taskName, Long limit) throws OdpsException
instance
- taskName
- limit
- OdpsException
public static ResultSet getResultSet(Instance instance, String taskName, Long limit, boolean limitHint) throws OdpsException
instance
- taskName
- limit
- limitHint
- OdpsException
public static ResultSet getResultSet(Instance instance, String taskName, Long limit, boolean limitHint, URI tunnelEndpoint) throws OdpsException
instance
- taskName
- limit
- limitHint
- tunnelEndpoint
- 指定 tunnel endpointOdpsException
public static ResultSet getResultSet(Instance instance, String taskName, InstanceTunnel instanceTunnel, Long limit, boolean limitHint) throws OdpsException
OdpsException
public String getCommandText()
Task
getCommandText
in class Task
public static List<String> getSqlWarning(Instance instance) throws OdpsException
instance
- instance 对象OdpsException
public static List<String> getSqlWarning(Instance instance, String taskName) throws OdpsException
instance
- instance 对象taskName
- task 名称OdpsException
public static TableSchema getResultSchema(Instance instance) throws OdpsException
instance
- Instance
对象OdpsException
public static Instance run(Odps odps, String sql) throws OdpsException
TableTunnel
进行操作。{ String sql = "select ....;"; Instance instance = SQLTask.run(odps, sql); instance.waitForSuccess(); Mapresults = instance.getTaskResults(); Map taskStatus = instance.getTaskStatus(); for(Entry status : taskStatus.entrySet()) { if (TaskStatus.Status.SUCCESS == status.getValue().getStatus()) { String result = results.get(status.getKey()); System.out.println(result); } } }
{@link
- Odps}sql
- 需要执行的SQL查询Instance
OdpsException
public static Instance run(Odps odps, String project, String sql, Map<String,String> hints, Map<String,String> aliases) throws OdpsException
odps
- Odps
对象project
- 任务运行时所属的Project
名称sql
- 需要运行的SQL查询hints
- 能够影响SQL执行的Set信息,例如:odps.mapred.map.split.size等aliases
- Alias信息。详情请参考用户手册中alias命令的相关介绍Instance
OdpsException
public static Instance run(Odps odps, String project, String sql, String taskName, Map<String,String> hints, Map<String,String> aliases) throws OdpsException
OdpsException
public static Instance run(Odps odps, String project, String sql, String taskName, Map<String,String> hints, Map<String,String> aliases, Integer priority) throws OdpsException
odps
- Odps
对象project
- 任务运行时所属的Project
名称sql
- 需要运行的SQL查询taskName
- 任务名称hints
- 能够影响SQL执行的Set信息,例如:odps.mapred.map.split.size等aliases
- Alias信息。详情请参考用户手册中alias命令的相关介绍priority
- 作业优先级 (注:公共云环境此参数无效)Instance
OdpsException
@Deprecated public static Instance run(Odps odps, String project, String sql, String taskName, Map<String,String> hints, Map<String,String> aliases, int priority) throws OdpsException
OdpsException
public static Instance run(Odps odps, String project, String sql, String taskName, Map<String,String> hints, Map<String,String> aliases, Integer priority, String mcqaConnHeader) throws OdpsException
odps
- Odps
对象project
- 任务运行时所属的Project
名称sql
- 需要运行的SQL查询taskName
- 任务名称hints
- 能够影响SQL执行的Set信息,例如:odps.mapred.map.split.size等aliases
- Alias信息。详情请参考用户手册中alias命令的相关介绍priority
- 作业优先级 (注:公共云环境此参数无效)mcqaConnHeader
- 是否开启MCQA V2, 应当指定 MCQA 2.0 Quota.getMcqaConnHeader()
Instance
OdpsException
public static Instance run(Odps odps, String sql, SQLTaskOption option) throws OdpsException
OdpsException
public static String getRawResult(Instance instance, String taskName) throws OdpsException
OdpsException
public static String getRawResult(Instance instance) throws OdpsException
OdpsException
Copyright © 2025 Alibaba Cloud Computing. All rights reserved.