文档

使用Java SDK管理事件库EventStore

更新时间:

事件库(EventStore)是日志服务中事件数据的采集、存储和查询单元。每个EventStore隶属于一个Project,每个Project中可创建多个EventStore。本文通过代码示例介绍如何创建、修改、查询、删除EventStore等。

前提条件

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见配置环境变量

    重要
    • 阿里云账号的AccessKey拥有所有API的访问权限,建议您使用RAM用户的AccessKey进行API访问或日常运维。

    • 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • 已安装0.6.83及以上版本的日志服务Java SDK。具体操作,请参见安装Java SDK

  • 已创建Project。具体操作,请参见使用Java SDK管理项目Project

注意事项

本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您通过与Project同地域的其他阿里云产品访问日志服务,请使用内网Endpointhttps://cn-hangzhou-intranet.log.aliyuncs.com。关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口

示例代码

本示例中,通过调用阿里云日志服务Java 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 {
  	// 日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
    private static final String ENDPOINT = "https://cn-hangzhou.log.aliyuncs.com";
  	// 日志服务的Project名称。
  	private static final String PROJECT = "ali-test-project";
  	// 日志服务的事件库名称。
    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();
    }
}

  • 本页导读 (1)
文档反馈