Save frequently used query statements as saved searches for quick reuse. Use the Java SDK to create, modify, list, and delete saved searches.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Simple Log Service SDK for Java is installed. For more information, see Install the Java SDK.
-
You have written logs to a Logstore and enabled indexing. Use the Java SDK to manage a Logstore, Create an index.
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 Endpoints.
Raw log
body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66Create a saved search
Create a saved search named ali-test-savedsearch:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.SavedSearch;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateSavedSearchRequest;
public class CreateSavedSearch {
public static void main(String[] args) throws LogException {
// 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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the Logstore name.
String logstoreName = "ali-test-logstore";
// Set the name of the saved search.
String savedSearchName = "ali-test-savedsearch";
// Query logs about failed GET or POST requests.
String query = "(request_method:GET or request_method:POST) not status in [200 299]";
System.out.println("ready to create savedsearch");
SavedSearch savedSearch = new SavedSearch();
// Set the Logstore.
savedSearch.setLogstore(logstoreName);
// Set the name of the saved search.
savedSearch.setSavedSearchName(savedSearchName);
// Set the display name of the saved search.
savedSearch.setDisplayName("savedsearch-displayname");
// Set the query statement for the saved search.
savedSearch.setSearchQuery(query);
CreateSavedSearchRequest request = new CreateSavedSearchRequest(projectName, savedSearch);
// Create the saved search.
client.createSavedSearch(request);
System.out.println(String.format("create savedsearch %s success", savedSearchName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
Expected result:
ready to create savedsearch
create savedsearch ali-test-savedsearch success
Modify a saved search
Modify the ali-test-savedsearch saved search:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.SavedSearch;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UpdateSavedSearchRequest;
public class UpdateSavedSearch {
public static void main(String[] args) throws LogException {
// 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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the Logstore name.
String logstoreName = "ali-test-logstore";
// Specify the name of the saved search.
String savedSearchName = "ali-test-savedsearch";
// Query logs about failed GET or POST requests.
String query = "(request_method:GET or request_method:POST) not status in [200 299]";
System.out.println("ready to update savedsearch");
SavedSearch savedSearch = new SavedSearch();
// Set the Logstore.
savedSearch.setLogstore(logstoreName);
// Set the name of the saved search.
savedSearch.setSavedSearchName(savedSearchName);
// Update the display name of the saved search.
savedSearch.setDisplayName("new-savedsearch-displayname");
// Set the query statement for the saved search.
savedSearch.setSearchQuery(query);
UpdateSavedSearchRequest request = new UpdateSavedSearchRequest(projectName, savedSearch);
// Update the saved search.
client.updateSavedSearch(request);
System.out.println(String.format("update savedsearch %s success", savedSearchName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
Expected result:
ready to update savedsearch
update savedsearch ali-test-savedsearch success
List all saved searches
List all saved searches in a project:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.SavedSearch;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.ListSavedSearchRequest;
import com.aliyun.openservices.log.response.ListSavedSearchResponse;
public class ListSavedSearch {
public static void main(String[] args) throws LogException {
// 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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
System.out.println("ready to list savedsearch");
// Query all saved searches.
ListSavedSearchRequest request = new ListSavedSearchRequest(projectName, 0, 10);
ListSavedSearchResponse response = client.listSavedSearch(request);
for (SavedSearch savedSearch : response.getSavedSearches()){
// Print the saved searches.
System.out.println("the savedsearch is :" + savedSearch.getDisplayName());
}
System.out.println(String.format("list savedsearch from project %s success", projectName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
Expected result:
ready to list savedsearch
the savedsearch is :savedsearch-displayname
the savedsearch is :savedsearch-displayname2
list savedsearch from project ali-test-project success
Get a saved search
Get the details of a saved search:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.SavedSearch;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetSavedSearchRequest;
import com.aliyun.openservices.log.response.GetSavedSearchResponse;
public class GetSavedSearch {
public static void main(String[] args) throws LogException {
// 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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the name of the saved search.
String savedSearchName = "ali-test-savedsearch";
System.out.println("ready to get savedsearch");
// Query the specified saved search.
GetSavedSearchRequest request = new GetSavedSearchRequest(projectName, savedSearchName);
GetSavedSearchResponse response = client.getSavedSearch(request);
SavedSearch savedSearch = response.getSavedSearch();
// Print the detailed configurations of the saved search.
System.out.println("the savedsearch name is :" + savedSearch.getSavedSearchName());
System.out.println("the savedsearch displayname is :" + savedSearch.getDisplayName());
System.out.println("the savedsearch query is :" + savedSearch.getSearchQuery());
System.out.println(String.format("get savedsearch from project %s success", projectName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
Expected result:
ready to get savedsearch
the savedsearch name is :ali-test-savedsearch
the savedsearch displayname is :savedsearch-displayname
the savedsearch query is :(request_method:GET or request_method:POST) not status in [200 299]
get savedsearch from project ali-test-project success
Delete a saved search
Delete a saved search from a project:
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.DeleteSavedSearchRequest;
public class DeleteSavedSearch {
public static void main(String[] args) throws LogException {
// 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");
// Specify the project name.
String projectName = "ali-test-project";
// Set the endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// Specify the name of the saved search.
String savedSearchName = "ali-test-savedsearch";
System.out.println("ready to delete savedsearch");
// Delete the specified saved search.
DeleteSavedSearchRequest request = new DeleteSavedSearchRequest(projectName, savedSearchName);
client.deleteSavedSearch(request);
System.out.println(String.format("delete savedsearch from project %s success", projectName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
Expected result:
ready to delete savedsearch
delete savedsearch from project ali-test-project success
References
In addition to its native SDK, SLS also supports the common Alibaba Cloud SDKs. For more information, see Log Service_SDKcenter-Alibaba CloudOpenAPIDeveloper Portal.
-
Related API operations: