public class SQLTask extends Task
Task.Properties, Task.Property| Constructor and Description |
|---|
SQLTask() |
| Modifier and Type | Method and Description |
|---|---|
String |
getCommandText()
获取 Task 命令信息
|
String |
getQuery() |
static List<Record> |
getResult(Instance instance)
Columns of each record in result are ALL OdpsType.STIRNG, ignore their real type in odps
Return value is valid only when SQL query is select, otherwise, result will be empty. |
static List<Record> |
getResult(Instance instance,
String taskName)
Deprecated.
|
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)
Columns of each record in result are ALL OdpsType.STIRNG, ignore their real type in odps
Return value is valid only when SQL query is select, otherwise, result will be empty. |
static Instance |
run(Odps odps,
String sql)
运行 SQL.
执行读取数据时,最多返回 1W 条记录,若超过,数据将被截断。 特别注意,在执行数据读取操作时: 正常情况下的 task 执行后,task 的状态为 SUCCESS,并正常返回数据结果。 但是,当读取数据量超过 10MB,task 的状态将是 FAILED,返回的数据结果为 error message。 因此,大量数据的获取建议使用 TableTunnel 进行操作。示例代码: |
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) |
void |
setQuery(String query)
设置SQL查询语句
|
getComment, getName, getProperties, setComment, setName, setProperties, setPropertypublic String getQuery()
public void setQuery(String query)
query - 需要执行的SQL查询@Deprecated 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();
List records = 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 records = SQLTask.getResultByInstanceTunnel(i, taskName, 1000L);
}
使用 instance tunnel 的方式获取 task 结果.
本接口与 getResult(Instance, String) 略有不同:
1) 本接口返回的 record 带有完整 schema 信息;
2) 本接口返回的 record 结果集不再有 1W 的条数限制.
注意 : 返回结果类型为 List, 数据量较大时会带来较多内存开销,
大数据量下载建议直接使用 InstanceTunnel;instance - instance 对象taskName - task 名称limit - 获取结果的数量OdpsException, - IOExceptionOdpsExceptionIOExceptionpublic static List<Record> getResultByInstanceTunnel(Instance instance, Long limit) throws OdpsException, IOException
instance - instance 对象limit - 获取结果的数量OdpsException, - IOExceptionOdpsExceptionIOExceptiongetResultByInstanceTunnel(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, - IOExceptionOdpsExceptionIOExceptionpublic static List<Record> getResultByInstanceTunnel(Instance instance) throws OdpsException, IOException
instance - instance 对象OdpsException, - IOExceptionOdpsExceptionIOExceptiongetResultByInstanceTunnel(Instance, String)public static List<Record> getResult(Instance instance) throws OdpsException
{
Instance i = SQLTask.run(odps, "select * from test_select_sql_result;");
instance.waitForSuccess();
List records = SQLTask.getResult(i);
}
instance - OdpsExceptionpublic String getCommandText()
TaskgetCommandText in class Taskpublic static Instance run(Odps odps, String sql) throws OdpsException
TableTunnel 进行操作。
{
String sql = "select ....;";
Instance instance = SQLTask.run(odps, sql);
instance.waitForSuccess();
Map results = 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查询InstanceOdpsExceptionpublic 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等alias - Alias信息。详情请参考用户手册中alias命令的相关介绍InstanceOdpsExceptionpublic static Instance run(Odps odps, String project, String sql, String taskName, Map<String,String> hints, Map<String,String> aliases) throws OdpsException
OdpsExceptionCopyright © 2016 Alibaba Cloud Computing. All rights reserved.