Simple Log Service uses Eventstores to collect, store, and query events. Each Eventstore belongs to a project. You can create multiple Eventstores in a project. This topic describes how to create, modify, query, and delete a Logstore 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 V0.6.83 or later is installed. For more information, see Install Simple Log Service SDK for Java.
A project is created. For more information, see Use Simple Log Service SDK for Java to manage a project.
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.
Sample code
In this example, Simple Log Service SDK for Java is used to call the related API operations to create, update, query, and delete an Eventstore and query multiple Eventstores.
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.*;
import com.aliyun.openservices.log.response.*;
public class ManageEventStore {
// 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.
private static final String ENDPOINT = "https://cn-hangzhou.log.aliyuncs.com";
// The name of the project.
private static final String PROJECT = "ali-test-project";
// The name of the Eventstore.
private static final String EVENT_STORE_NAME = "test-events";
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
private static final Client client = new Client(
ENDPOINT,
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
);
// Create an Eventstore.
public static void createEventStore() throws LogException {
LogStore eventStore = new LogStore();
eventStore.SetLogStoreName(EVENT_STORE_NAME);
eventStore.SetTtl(30);
eventStore.SetShardCount(2);
eventStore.setmAutoSplit(true);
eventStore.setmMaxSplitShard(64);
// Create a CreateLogStoreRequest object, specify a project name and a LogStore object, and call the createEventStore method on the client to create an Eventstore.
CreateLogStoreRequest request = new CreateLogStoreRequest(PROJECT, eventStore);
client.createEventStore(request);
System.out.println(String.format("Create eventStore %s success", EVENT_STORE_NAME));
}
// Update an Eventstore.
public static void updateEventStore() throws LogException {
LogStore eventStore = new LogStore();
eventStore.SetLogStoreName(EVENT_STORE_NAME);
eventStore.SetTtl(60);
// Create an UpdateLogStoreRequest object, specify a project name and a LogStore object, and call the updateEventStore method on the client to update an Eventstore.
UpdateLogStoreRequest request = new UpdateLogStoreRequest(PROJECT, eventStore);
client.updateEventStore(request);
System.out.println(String.format("Update eventStore %s success", EVENT_STORE_NAME));
}
// Query all eventstores.
public static void listEventStores() throws LogException {
// Create a ListLogStoresRequest object, specify a project name, a start index, and the number of entries to return, and call the listEventStores method on the client to query all Eventstores.
ListLogStoresRequest request = new ListLogStoresRequest(PROJECT, 0, 10);
ListLogStoresResponse response = client.listEventStores(request);
System.out.println(String.format("List eventStores: %s", String.join(",", response.GetLogStores())));
}
// Query the details of a specified Eventstore.
public static void getEventStore() throws LogException {
// Create a GetLogStoreRequest object, specify a project name and an Eventstore name, and call the getEventStore method on the client to query the details of the Eventstore.
GetLogStoreRequest request = new GetLogStoreRequest(PROJECT, EVENT_STORE_NAME);
GetLogStoreResponse response = client.getEventStore(request);
System.out.println(String.format("Get eventStore %s success", response.GetLogStore().GetLogStoreName()));
}
// Delete an Eventstore.
public static void deleteEventStore() throws LogException {
// Create a DeleteLogStoreRequest object, specify a project name and an Eventstore name, and call the deleteEventStore method on the client to delete the Eventstore.
DeleteLogStoreRequest request = new DeleteLogStoreRequest(PROJECT, EVENT_STORE_NAME);
client.deleteEventStore(request);
System.out.println(String.format("Delete eventStore %s success", EVENT_STORE_NAME));
}
public static void main(String[] args) throws LogException {
createEventStore();
updateEventStore();
listEventStores();
getEventStore();
deleteEventStore();
}
}