Use Dedicated SQL with the Java SDK
Standard SQL may not scan all logs within a specified time range in a single query. Dedicated SQL adds compute resources to increase the data volume that can be analyzed per query. This topic provides code examples for using Dedicated SQL with the Java SDK.
Prerequisites
Simple Log Service is activated.
-
Installed Java SDK 0.6.68 or a later version. For more information, see Install the Java SDK.
Background information
Simple Log Service provides Dedicated SQL to enhance SQL analysis capabilities and support computations on hundreds of billions of rows of data. For more information, see High-performance and high-accuracy query and analysis (Dedicated SQL).
Precautions
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service from other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com.
For more information about the supported regions and endpoints of Simple Log Service, see Endpoint.
Step 1: Create a Simple Log Service client
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Enter the project name.
String project = "aliyun-test-project";
// Set the endpoint. This example uses China (Hangzhou). Change it to your actual region.
String host = "cn-hangzhou.log.aliyuncs.com";
// Enter the Logstore name.
String logStore = "aliyun-test-logstore";
// Set the shard ID.
int shardId = 0;
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
Step 2: Use Dedicated SQL
-
Before you use Dedicated SQL, confirm that it is enabled.
Check in the Simple Log Service console
To check whether Dedicated SQL is enabled in the Simple Log Service console, see High-performance and high-accuracy query and analysis (Dedicated SQL).
Check using OpenAPI
To check whether Dedicated SQL is enabled using OpenAPI, see GetSqlInstance.
-
Simple Log Service provides the executeLogStoreSql and executeProjectSql operations to use Dedicated SQL.
The following code provides an example. For more information, see Aliyun Log Java SDK.
NoteTo filter data before an analysis, use the
Search statement|Analytic statementsyntax for higher efficiency. The executeLogStoreSql operation is recommended.executeLogStoreSql
Run Dedicated SQL queries in a specified Logstore. This operation uses standard SQL-92 syntax in the format
Search statement|Analytic statement.import com.aliyun.openservices.log.Client; import com.aliyun.openservices.log.common.LogContent; import com.aliyun.openservices.log.common.LogItem; import com.aliyun.openservices.log.common.QueriedLog; import com.aliyun.openservices.log.exception.LogException; import com.aliyun.openservices.log.response.GetLogsResponse; import java.util.Date; public class SlsSample { public static void main(String args[]) throws LogException, InterruptedException { // This example obtains the AccessKey ID and AccessKey secret from environment variables. String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); // Enter the project name. String project = "aliyun-test-project"; // Set the endpoint. This example uses China (Hangzhou). Change it to your actual region. String host = "cn-hangzhou.log.aliyuncs.com"; // Enter the Logstore name. String logStore = "aliyun-test-logstore"; // Set the shard ID. int shardId = 0; // Create a Simple Log Service client. Client client = new Client(host, accessId, accessKey); // Execute an SQL analysis in the specified Logstore. try { String sql = "* | select count(1)"; int from = (int) (new Date().getTime() / 1000 - 600); int to = (int) (new Date().getTime() / 1000); GetLogsResponse logsResponse = client.executeLogstoreSql(project, logStore, 1627268185, 1627269085, "* | SELECT count(*)", true); System.out.println("Returned sql result count:" + logsResponse.GetCount()); for (QueriedLog log : logsResponse.getLogs()) { LogItem item = log.GetLogItem(); System.out.println("time : " + item.mLogTime); for (LogContent content : item.mContents) { System.out.println(content.mKey + ":" + content.mValue); } } // Print the statistics of the calculation results. // The number of processed log entries. System.out.println("proccesedRows:" + logsResponse.getProcessedRow()); // The duration of the SQL analysis. System.out.println("elapsedMilli:" + logsResponse.getElapsedMilliSecond()); // The CPU time consumed by the SQL analysis after you enable Dedicated SQL. Unit: seconds. Dedicated SQL is billed based on CPU time. For more information, see Billing items. System.out.println("cpuSec:" + logsResponse.getCpuSec()); // The number of CPU cores used for the SQL analysis after you enable Dedicated SQL. System.out.println("cpuCores:" + logsResponse.getCpuCores()); System.out.println("requestId:" + logsResponse.GetRequestId()); } catch (LogException e) { System.out.println("error code :" + e.GetErrorCode()); System.out.println("error message :" + e.GetErrorMessage()); throw e; } } }To use Dedicated SQL by calling the executeLogstoreSql operation, use the format
GetLogsResponse logsResponse = client.executeLogstoreSql(project, logStore, from, to, query, powerSql). The following table describes the parameters.Parameter Name
Type
Required
Example
Description
project
String
Yes
N/A
The project name.
Defined when you create the client. You do not need to set this parameter separately.
logStore
String
Yes
N/A
The Logstore name.
Defined when you create the client. You do not need to set this parameter separately.
from
Long
Yes
1627268185
The start time of the query, as a UNIX timestamp in seconds elapsed since 1970-01-01 00:00:00 UTC.
to
Long
Yes
1627269085
The end time of the query, as a UNIX timestamp in seconds elapsed since 1970-01-01 00:00:00 UTC.
query
String
Yes
"* | SELECT count(*)"
Simple Log Service query and analytic statement. The format is
Search statement|Analytic statement. For more information, see Basic syntax.Simple Log Service returns 100 rows of results by default. You can also use the LIMIT clause to specify the number of rows to return. For more information, see LIMIT clause.
powerSql
Boolean
No
true
Whether to use Dedicated SQL. For more information, see High-performance and high-accuracy query and analysis (Dedicated SQL).
-
true: Use Dedicated SQL.
-
false (default): Use Standard SQL.
executeProjectSql
import com.aliyun.openservices.log.Client; import com.aliyun.openservices.log.common.LogContent; import com.aliyun.openservices.log.common.LogItem; import com.aliyun.openservices.log.common.QueriedLog; import com.aliyun.openservices.log.exception.LogException; import com.aliyun.openservices.log.response.GetLogsResponse; import java.util.Date; public class SlsSample { public static void main(String args[]) throws LogException, InterruptedException { // This example obtains the AccessKey ID and AccessKey secret from environment variables. String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); // Enter the project name. String project = "aliyun-test-project"; // Set the endpoint. This example uses China (Hangzhou). Change it to your actual region. String host = "cn-hangzhou.log.aliyuncs.com"; // Enter the Logstore name. String logStore = "aliyun-test-logstore"; // Set the shard ID. int shardId = 0; // Create a Simple Log Service client. Client client = new Client(host, accessId, accessKey); // Execute an SQL analysis in the specified project. try { int now = (int) (new Date().getTime() / 1000); String sql = "select count(1) as cnt from xxx where __time__ > " + now; GetLogsResponse logsResponse = client.executeProjectSql(project, "select avg(latency),max(latency) ,count(1) as c from sample-logstore where status>200 and __time__>=1500975424 and __time__ < 1501035044 GROUP BY method ORDER BY c", true); System.out.println("Returned sql result count:" + logsResponse.GetCount()); for (QueriedLog log : logsResponse.getLogs()) { LogItem item = log.GetLogItem(); for (LogContent content : item.mContents) { System.out.println(content.mKey + ":" + content.mValue); } } // Print the statistics of the calculation results. // The number of processed log entries. System.out.println("proccesedRows:" + logsResponse.getProcessedRow()); // The duration of the SQL analysis. System.out.println("elapsedMilli:" + logsResponse.getElapsedMilliSecond()); // The CPU time consumed by the SQL analysis after you enable Dedicated SQL. Unit: seconds. Dedicated SQL is billed based on CPU time. For more information, see Billing items. System.out.println("cpuSec:" + logsResponse.getCpuSec()); // The number of CPU cores used for the SQL analysis after you enable Dedicated SQL. System.out.println("cpuCores:" + logsResponse.getCpuCores()); System.out.println("requestId:" + logsResponse.GetRequestId()); } catch (LogException e) { System.out.println("error code :" + e.GetErrorCode()); System.out.println("error message :" + e.GetErrorMessage()); throw e; } } }To use Dedicated SQL by calling the executeProjectSql operation, use the format
GetLogsResponse logsResponse = client.executeProjectSql(project, query, powerSql). The following table describes the parameters.Parameter Name
Type
Required
Example
Description
project
String
Yes
N/A
The project name.
Defined when you create the client. You do not need to set this parameter separately.
query
String
Yes
"select avg(latency),max(latency) ,count(1) as c from sample-logstore where status>200 and __time__>=1500975424 and __time__ < 1501035044 GROUP BY method ORDER BY c"
Simple Log Service returns 100 rows of results by default. You can also use the LIMIT clause to specify the number of rows to return. For more information, see LIMIT clause.
NoteThis is a standard SQL statement. You must add the filter conditions and the time range to the WHERE clause of the SQL statement. You must add the
__time__condition to thewhereclause to specify a time range.powerSql
Boolean
No
true
Whether to use Dedicated SQL. For more information, see High-performance and high-accuracy query and analysis (Dedicated SQL).
-
true: Use Dedicated SQL.
-
false (default): Use Standard SQL.
-
References
-
To use Dedicated SQL in the console, see High-performance and high-accuracy query and analysis (Dedicated SQL).
-
To use Dedicated SQL in OpenAPI Portal, see Create a Dedicated SQL instance.
-
If an API operation returns an error, see API error codes.
-
Alibaba Cloud OpenAPI Explorer lets you debug Simple Log Service API operations without manually encapsulating or signing requests. For more information, see OpenAPI Developer Portal.
-
Simple Log Service provides a CLI for automated configuration. For more information, see Simple Log Service CLI.
-
For more code examples, see Aliyun Log Java SDK on GitHub.
-
For more code examples, see Aliyun Log Python SDK on GitHub.