全部產品
Search
文件中心

Simple Log Service:使用Java SDK管理事件庫EventStore

更新時間:Aug 28, 2024

事件庫(EventStore)是Log Service中事件數目據的採集、儲存和查詢單元。每個EventStore隸屬於一個Project,每個Project中可建立多個EventStore。本文通過程式碼範例介紹如何建立、修改、查詢、刪除EventStore等。

前提條件

  • 已開通Log Service。更多資訊,請參見開通Log Service

  • 已建立RAM使用者並完成授權。具體操作,請參見建立RAM使用者並完成授權

  • 已配置環境變數ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具體操作,請參見在Linux、macOS和Windows系統配置環境變數

    重要
    • 阿里雲帳號的AccessKey擁有所有API的存取權限,建議您使用RAM使用者的AccessKey進行API訪問或日常營運。

    • 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。

注意事項

本樣本以華東1(杭州)的公網Endpoint為例,其公網Endpoint為https://cn-hangzhou.log.aliyuncs.com。如果您通過與Project同地區的其他阿里雲產品訪問Log Service,請使用內網Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。關於Log Service支援的地區與Endpoint的對應關係,請參見服務入口

範例程式碼

本樣本中,通過調用阿里雲Log ServiceJava SDK中的相關API實現對事件庫(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 {
  	// Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
    private static final String ENDPOINT = "https://cn-hangzhou.log.aliyuncs.com";
  	// Log Service的Project名稱。
  	private static final String PROJECT = "ali-test-project";
  	// Log Service的事件庫名稱。
    private static final String EVENT_STORE_NAME = "test-events";
  	
  	// 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。  
    private static final Client client = new Client(
            ENDPOINT,
            System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    );

  	// 建立事件庫(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);

      	// 建立一個CreateLogStoreRequest對象,傳入專案名稱和LogStore對象,並調用用戶端的createEventStore方法建立事件庫。
        CreateLogStoreRequest request = new CreateLogStoreRequest(PROJECT, eventStore);
        client.createEventStore(request);
        System.out.println(String.format("Create eventStore %s success", EVENT_STORE_NAME));
    }

  	// 更新事件庫(EventStore)
    public static void updateEventStore() throws LogException {
        LogStore eventStore = new LogStore();
        eventStore.SetLogStoreName(EVENT_STORE_NAME);
        eventStore.SetTtl(60);

      	// 建立一個UpdateLogStoreRequest對象,傳入專案名稱和LogStore對象,並調用用戶端的updateEventStore方法更新事件庫。
        UpdateLogStoreRequest request = new UpdateLogStoreRequest(PROJECT, eventStore);
        client.updateEventStore(request);
        System.out.println(String.format("Update eventStore %s success", EVENT_STORE_NAME));
    }

  	// 列出所有事件庫(EventStore)
    public static void listEventStores() throws LogException {
      	//建立一個ListLogStoresRequest對象,傳入專案名稱、起始索引和返回數量,並調用用戶端的listEventStores方法列出所有事件庫。
        ListLogStoresRequest request = new ListLogStoresRequest(PROJECT, 0, 10);
        ListLogStoresResponse response = client.listEventStores(request);
        System.out.println(String.format("List eventStores: %s", String.join(",", response.GetLogStores())));
    }

  	// 擷取指定事件庫(EventStore)的詳細資料
    public static void getEventStore() throws LogException {
      	// 建立一個GetLogStoreRequest對象,傳入專案名稱和事件庫名稱,並調用用戶端的getEventStore方法擷取事件儲存的詳細資料。
        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()));
    }

  	// 刪除事件庫(EventStore)
    public static void deleteEventStore() throws LogException {
      	// 建立一個DeleteLogStoreRequest對象,傳入專案名稱和事件儲存名稱,並調用用戶端的deleteEventStore方法刪除事件儲存。
      	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();
    }
}