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

前提条件

  • 已开通日志服务。更多信息,请参见开通日志服务
  • 已创建并获取AccessKey。更多信息,请参见访问密钥

    阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。RAM用户需具备操作日志服务资源的权限。具体操作,请参见为RAM用户授权

  • 已安装Java开发环境。

    日志服务Java SDK支持JRE 6.0及以上的Java运行环境,您可以执行java -version命令检查您已安装的Java版本。如果未安装,可以从Java官方网站下载安装包并完成安装。

  • 已安装日志服务Java SDK。具体操作,请参见安装Java SDK
  • 已创建Project。具体操作,请参见创建Project示例代码

注意事项

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

创建Logstore示例代码

以下代码用于创建名为ali-test-logstore的Logstore。

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.CreateLogStoreRequest;

public class CreateLogstore {
    public static void main(String[] args) throws LogException {
        //阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
        String accessId = "yourAccessKeyId";
        String accessKey = "yourAccessKeySecret";
        //Project名称。
        String projectName = "ali-test-project";
        //日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        //创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        try {
            //Logstore名称。
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to create logstore");

            //创建Logstore,数据保存时间为60天,2个Shard,开启WebTracking功能。
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            //自动分裂Shard。
            logStore.setmAutoSplit(true);
            //Shard自动分裂最大数为64。
            logStore.setmMaxSplitShard(64);
            //开启记录外网IP地址功能。
            logStore.setAppendMeta(true);
            //设置数据在Logstore热存储层中的存储时间为30天。
            logStore.setHotTTL(30);
            //设置为标准型Logstore。
            logStore.setMode("standard");

            CreateLogStoreRequest request = new CreateLogStoreRequest(projectName, logStore);
            //创建Logstore。
            client.CreateLogStore(request);

            System.out.println(String.format("create logstore %s success", logstoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

预期结果如下:

ready to create logstore
create logstore ali-test-logstore success

修改Logstore示例代码

以下代码用于修改名为ali-test-logstore的Logstore配置信息。

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.UpdateLogStoreRequest;

public class UpdateLogstore {
    public static void main(String[] args) throws LogException {
        //阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
        String accessId = "yourAccessKeyId";
        String accessKey = "yourAccessKeySecret";
        //Project名称。
        String projectName = "ali-test-project";
        //日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        //创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        try {
            //Logstore名称。
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to update logstore");

            //更新数据在Logstore热存储层中的存储时间为45天。
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            logStore.setHotTTL(45);

            UpdateLogStoreRequest request = new UpdateLogStoreRequest(projectName, logStore);
            //更新Logstore。
            client.UpdateLogStore(request);

            System.out.println(String.format("update logstore %s success", logstoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

预期结果如下:

ready to update logstore
update logstore ali-test-logstore success

查询所有Logstore示例代码

以下代码用于查询目标Project下的所有Logstore。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.ListLogStoresRequest;
import com.aliyun.openservices.log.response.ListLogStoresResponse;

public class ListLogstore {

    public static void main(String[] args) throws LogException {
        //阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
        String accessId = "yourAccessKeyId";
        String accessKey = "yourAccessKeySecret";
        //Project名称。
        String projectName = "ali-test-project";
        //日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        //创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        try {
            System.out.println("ready to list logstore");

            //查询10个logstore。
            ListLogStoresRequest request = new ListLogStoresRequest(projectName, 0, 10, "", "None");

            ListLogStoresResponse response = client.ListLogStores(request);

            for (String logStore : response.GetLogStores()) {
                System.out.println(logStore.toString());
            }
        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

预期结果如下:

ready to list logstore
ali-test-logstore

查询指定Logstore示例代码

以下代码用于查询指定Logstore信息。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogStoreRequest;
import com.aliyun.openservices.log.response.GetLogStoreResponse;

public class GetLogstore {

    public static void main(String[] args) throws LogException {
        //阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
        String accessId = "yourAccessKeyId";
        String accessKey = "yourAccessKeySecret";
        //Project名称。
        String projectName = "ali-test-project";
        //日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        //创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        try {
            //Logstore名称。
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to get logstore");

            //查询指定logstore。
            GetLogStoreRequest request = new GetLogStoreRequest(projectName, logStoreName);

            GetLogStoreResponse response = client.GetLogStore(request);

            System.out.println("The Logstore name is : " + response.GetLogStore().GetLogStoreName());

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

预期结果如下:

ready to get logstore
The Logstore name is : ali-test-logstore

删除Logstore示例代码

以下代码用于删除名为ali-test-logstore的Logstore。

重要
  • Logstore一旦删除,其存储的数据将会被永久删除,不可恢复,请谨慎操作。
  • 删除Logstore前需先删除其对应的所有Logtail配置。
  • 如果该Logstore上还启用了日志投递,建议删除前停止向该Logstore写入新数据,并确认Logstore中已有的数据已经全部投递成功。
  • 如果您使用阿里云账号删除Logstore时,控制台提示权限不足,请提交工单进行删除。
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class DeleteLogstore {

    public static void main(String[] args) throws LogException {
        //阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。
        String accessId = "yourAccessKeyId";
        String accessKey = "yourAccessKeySecret";
        //Project名称。
        String projectName = "ali-test-project";
        //日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        //创建日志服务Client。
        Client client = new Client(host, accessId, accessKey);

        try {
            //Logstore名称。
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to delete logstore");

            //删除指定logstore。
            client.DeleteLogStore(projectName,logStoreName);
            System.out.println(String.format("delete logstore %s success", logStoreName));

        } catch (LogException e) {
            System.out.println("LogException e :" + e.toString());
            System.out.println("error code :" + e.GetErrorCode());
            System.out.println("error message :" + e.GetErrorMessage());
            throw e;
        }
    }
}

预期结果如下:

ready to delete logstore
delete logstore ali-test-logstore success

相关文档