赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 日志服务
  4. 开发参考
  5. 日志服务SDK
  6. Java SDK
  7. 资源管理
  8. 使用Java SDK管理事件库EventStore

使用Java SDK管理事件库EventStore

  • 资源管理
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

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

前提条件

  • 已开通日志服务。更多信息,请参见开通日志服务。

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

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见在Linux、macOS和Windows系统配置环境变量。

    重要
    • 阿里云账号的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();
    }
}

相关文章

使用Java SDK管理Project 2025-04-22 10:36

项目(Project)是日志服务的资源管理单元,包含Logstore、MetricStore和机器组等资源,同时也是您访问日志服务资源的入口。本文通过代码示例介绍如何创建、修改、查询、删除Project等。 前提条件

使用Java SDK管理日志库Logstore 2025-04-22 10:36

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

使用Java SDK管理Shard 2025-04-22 10:36

日志服务使用Shard控制Logstore或MetricStore读写数据的能力,数据必定保存在某一个Shard中。本文通过代码示例介绍如何查询、分裂、合并Shard。 前提条件 <

使用Java SDK管理事件库EventStore 2025-04-22 10:36

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

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号