An eventstore is a unit in Simple Log Service for collecting, storing, and querying event data. Each eventstore belongs to a project, and you can create multiple eventstores in a project. This topic provides code examples for creating, updating, querying, and deleting eventstores.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
-
Install the Simple Log Service Java SDK V0.6.83 or later. For more information, see Install the Java SDK.
-
Create a project. For more information, see Use the Java SDK to manage a project.
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.
Sample code
The following sample code uses the Simple Log Service Java SDK to create, update, list, retrieve, and delete an eventstore.
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 endpoint for Simple Log Service. This example uses the China (Hangzhou) endpoint. Change it to your 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";
// This example gets the AccessKey ID and AccessKey secret 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));
}
// List 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 list 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())));
}
// Get the details of a specific 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 get 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();
}
}