If you need to frequently view the results of a query statement, you can save the query statement as a saved search. This topic describes how to create, modify, query, and delete a saved search by using Simple Log Service SDK for Java and provides sample code.
Prerequisites
A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.
The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables in Linux, macOS, and Windows.
ImportantThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.
Simple Log Service SDK for Java is installed. For more information, see Install Simple Log Service SDK for Java.
Logs are written to a Logstore and indexing is enabled. For more information, see Use Simple Log Service SDK for Java to manage a Logstore and Create indexes.
Usage notes
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is https://cn-hangzhou.log.aliyuncs.com
. If you want to access Simple Log Service by using 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.66
Sample code that is used to create a saved search
The following sample code provides an example on how to 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 {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
String projectName = "ali-test-project";
// The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter 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 {
// Enter the name of the Logstore.
String logstoreName = "ali-test-logstore";
// Enter the name of the saved search
String savedSearchName = "ali-test-savedsearch";
// Query the logs that contain failed GET requests or failed 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();
// The name of the Logstore.
savedSearch.setLogstore(logstoreName);
// The name of the saved search.
savedSearch.setSavedSearchName(savedSearchName);
// The display name of the saved search.
savedSearch.setDisplayName("savedsearch-displayname");
// The search statement or the query statement of 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
Sample code that is used to modify a saved search
The following sample code provides an example on how to 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 {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
String projectName = "ali-test-project";
// The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter 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 {
// Enter the name of the Logstore.
String logstoreName = "ali-test-logstore";
// Enter the name of the saved search.
String savedSearchName = "ali-test-savedsearch";
// Query the logs that contain failed GET requests or failed 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();
// The name of the Logstore.
savedSearch.setLogstore(logstoreName);
// The name of the saved search.
savedSearch.setSavedSearchName(savedSearchName);
// The display name of the saved search.
savedSearch.setDisplayName("new-savedsearch-displayname");
// The search statement or the query statement of the saved search.
savedSearch.setSearchQuery(query);
UpdateSavedSearchRequest request = new UpdateSavedSearchRequest(projectName, savedSearch);
// Modify 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
Sample code that is used to query all saved searches
The following sample code provides an example on how to query all saved searches in a specified 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 {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
String projectName = "ali-test-project";
// The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter 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()){
// Display the information about 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
Sample code that is used to query a saved search
The following sample code provides an example on how to query the information about a specified 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 {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
String projectName = "ali-test-project";
// The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter 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 {
// 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();
// Display the detailed configuration 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
Sample code that is used to delete a saved search
The following sample code provides an example on how to delete a saved search in a specified 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 {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
String projectName = "ali-test-project";
// The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter 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 {
// The name of the saved search.
String savedSearchName = "ali-test-savedsearch";
System.out.println("ready to delete savedsearch");
// Delete the 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
- If the response that is returned by Log Service contains error information after you call an API operation, the call fails. You can handle errors based on the error codes that are returned when API calls fail. For more information, see Error codes.
- Alibaba Cloud OpenAPI Explorer provides debugging capabilities, SDKs, examples, and related documents. You can use OpenAPI Explorer to debug Log Service API operations without the need to manually encapsulate or sign requests. For more information, visit OpenAPI Portal.
- Log Service provides the command-line interface (CLI) to meet the requirements for automated configurations in Log Service. For more information, see Log Service CLI.
For more information about saved search-related API operations, see the following topics:
- For more information about sample code, see Alibaba Cloud Log Service SDK for Java on GitHub.