Simple Log Serviceは、Eventstoreを使用してイベントを収集、保存、およびクエリします。 各Eventstoreはプロジェクトに属します。 1つのプロジェクトに複数のEventstoreを作成できます。 このトピックでは、Simple Log Service SDK for Javaを使用してLogstoreを作成、変更、クエリ、および削除する方法について説明し、サンプルコードを提供します。
前提条件
RAM (Resource Access Management) ユーザーが作成され、必要な権限がRAMユーザーに付与されます。 詳細については、「RAMユーザーの作成とRAMユーザーへの権限付与」をご参照ください。
ALIBABA_CLOUD_ACCESS_KEY_IDおよびALIBABA_CLOUD_ACCESS_KEY_SECRET環境変数が設定されています。 詳細については、「環境変数の設定」をご参照ください。
重要Alibaba CloudアカウントのAccessKeyペアには、すべてのAPI操作に対する権限があります。 RAMユーザーのAccessKeyペアを使用して、API操作を呼び出したり、ルーチンのO&Mを実行したりすることを推奨します。
プロジェクトコードにAccessKey IDまたはAccessKey secretを保存しないことを推奨します。 そうしないと、AccessKeyペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。
Simple Log Service SDK for Java V0.6.83以降がインストールされています。 詳細については、「Simple Log Service SDK For Javaのインストール」をご参照ください。
プロジェクトが作成されます。 詳細については、「Simple Log Service SDK For Javaを使用したプロジェクトの管理」をご参照ください。
使用上の注意
この例では、中国 (杭州) リージョンのパブリックSimple Log Serviceエンドポイントが使用されています。これは https://cn-hangzhou.log.aliyuncs.com
です。 プロジェクトと同じリージョンにある他のAlibaba Cloudサービスを使用してSimple Log Serviceにアクセスする場合は、内部のSimple Log Serviceエンドポイント ( https://cn-hangzhou-intranet.log.aliyuncs.com
) を使用できます。 Simple Log Serviceのサポートされているリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。
サンプルコード
この例では、Simple Log Service SDK for Javaを使用して、関連するAPI操作を呼び出し、Eventstoreを作成、更新、クエリ、および削除し、複数の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 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();
}
}