Use DLF SDK for Java to submit a data exploration task
Use the DLF SDK for Java to submit a SQL query, poll until results are ready, and retrieve paginated output — all in a single end-to-end example.
Prerequisites
Before you begin, make sure you have:
-
An AccessKey pair. Use a RAM user's AccessKey pair rather than your Alibaba Cloud account credentials to reduce security risk.
-
JDK 1.7 or later installed
Install the DLF SDK for Java
Add the following dependency to your pom.xml. For the latest version, check the Maven repository.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>datalake20200710</artifactId>
<version>2.0.12</version>
</dependency>
Run a data exploration query
The following example shows how to submit a SQL query task, wait for it to complete, and retrieve the results using the SubmitQuery, GetQueryResult, and CancelQuery API operations. For the full API reference, see Data exploration.
The example is structured as four methods:
-
buildClient— initializes the DLF client using credentials from environment variables -
submitQuery— sends the SQL statement and returns aqueryId -
waitForResult— pollsGetQueryResultuntil the job completes -
printResult— prints the schema and rows on success, or the error message on failure
import com.aliyun.datalake20200710.Client;
import com.aliyun.datalake20200710.models.GetQueryResultRequest;
import com.aliyun.datalake20200710.models.GetQueryResultResponse;
import com.aliyun.datalake20200710.models.SubmitQueryRequest;
import com.aliyun.datalake20200710.models.SubmitQueryResponse;
import com.aliyun.teaopenapi.models.Config;
public class QueryExample {
public static void main(String[] args) throws Exception {
Client client = buildClient();
String queryId = submitQuery(client, "show databases;");
GetQueryResultResponse result = waitForResult(client, queryId);
printResult(result);
}
// Initialize the DLF client using credentials from environment variables.
// Store AccessKey ID and AccessKey secret in environment variables (AK_ENV, SK_ENV)
// instead of hardcoding them in source code.
// Use a RAM user's credentials rather than your Alibaba Cloud account to limit permissions.
private static Client buildClient() throws Exception {
Config authConfig = new Config();
authConfig.accessKeyId = System.getenv("AK_ENV");
authConfig.accessKeySecret = System.getenv("SK_ENV");
authConfig.endpoint = "dlf.cn-hangzhou.aliyuncs.com";
authConfig.regionId = "cn-hangzhou";
return new Client(authConfig);
}
// Submit a SQL query task and return the queryId for tracking.
private static String submitQuery(Client client, String sql) throws Exception {
SubmitQueryRequest queryRequest = new SubmitQueryRequest();
queryRequest.setSql(sql);
SubmitQueryResponse queryResponse = client.submitQuery(queryRequest);
return queryResponse.getBody().getData();
}
// Poll GetQueryResult every second until the job completes.
// To cancel a running query instead, call CancelQuery with the queryId.
private static GetQueryResultResponse waitForResult(Client client, String queryId)
throws Exception {
GetQueryResultRequest resultRequest = new GetQueryResultRequest();
resultRequest.setQueryId(queryId);
resultRequest.setPageNumber(1);
resultRequest.setPageSize(100);
GetQueryResultResponse result = client.getQueryResult(resultRequest);
while (!result.getBody().getJobCompleted()) {
Thread.sleep(1000);
result = client.getQueryResult(resultRequest);
}
return result;
}
// Print query results on success, or the error message on failure.
private static void printResult(GetQueryResultResponse result) {
if ("AVAILABLE".equals(result.getBody().getStatus())) {
System.out.println(result.getBody().getSchema());
System.out.println(result.getBody().getRows());
} else {
System.out.println(result.getBody().getErrorMessage());
}
}
}
To cancel a running query, call CancelQuery with the queryId before waitForResult returns:
// CancelQueryRequest cancelRequest = new CancelQueryRequest();
// cancelRequest.setQueryId(queryId);
// CancelQueryResponse cancelResult = client.cancelQuery(cancelRequest);
// System.out.println(cancelResult.getBody().getSuccess());
What's next
-
Data exploration — full list of API operations, parameters, and response fields