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

使用Java SDK管理索引

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

索引是一种倒排的数据存储结构,由关键词和指向实际数据的逻辑指针组成,用于快速根据关键词定位到具体数据行,类似于数据的目录。您只有配置索引后,才能进行查询和分析操作。本文通过代码示例介绍如何创建、修改、查询、删除索引。

前提条件

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

  • 已创建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泄露,威胁您账号下所有资源的安全。

  • 已安装日志服务Java SDK。具体操作,请参见安装Java SDK。

  • 已写入日志到Logstore。具体操作,请参见数据采集概述。

注意事项

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

原始日志样例

body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66

创建索引示例代码

控制台界面支持界面化配置索引,操作更便捷。具体操作,请参见创建索引。

以下代码用于创建名为testindex的索引。该示例中,基于原始日志样例,开启全文索引,为request_method、status字段开启字段索引。

索引配置

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Index;
import com.aliyun.openservices.log.exception.LogException;

public class CreateIndex {
    public static void main(String[] args) throws LogException {
        // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 输入Project名称。
        String projectName = "ali-test-project";
        // 输入Logstore名称。
        String logstoreName = "ali-test-logstore";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

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

        try {
            //索引。
            //创建索引前,必须规划好全文索引、字段索引配置。该示例中,开启全文索引,为request_method、status字段开启字段索引。
            String logstoreindex = "{\"line\": {\"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"chn\": false}, \"keys\": {\"request_method\": {\"type\": \"text\", \"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"alias\": \"\", \"doc_value\": true, \"chn\": false}, \"status\": {\"type\": \"long\", \"alias\": \"\", \"doc_value\": true}}, \"log_reduce\": false, \"max_text_len\": 2048}";
            Index index = new Index();
            System.out.println("ready to create index");

            index.FromJsonString(logstoreindex);

            client.CreateIndex(projectName, logstoreName, index);

            System.out.println(String.format("create index for %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 index
create index for ali-test-logstore success

更新索引示例代码

控制台界面支持界面化配置索引,操作更便捷。具体操作,请参见创建索引。

以下代码用于更新索引。该示例中,基于原始日志样例,开启全文索引,为request_method、status开启字段索引,并设置request_method字段大小写敏感为True。

UpdateIndex

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Index;
import com.aliyun.openservices.log.exception.LogException;

public class UpdateIndex {
    public static void main(String[] args) throws LogException {
        // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 输入Project名称。
        String projectName = "ali-test-project";
        // 输入Logstore名称。
        String logstoreName = "ali-test-logstore";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

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

        try {
            // 更新索引。
            String logstoreindex = "{\"line\": {\"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"chn\": false}, \"keys\": {\"request_method\": {\"type\": \"text\", \"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": true, \"alias\": \"\", \"doc_value\": true, \"chn\": false}, \"status\": {\"type\": \"long\", \"alias\": \"\", \"doc_value\": true}}, \"log_reduce\": false, \"max_text_len\": 2048}";
            Index index = new Index();
            System.out.println("ready to update index");

            index.FromJsonString(logstoreindex);

            client.UpdateIndex(projectName, logstoreName, index);

            System.out.println(String.format("update index for %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 index
update index for ali-test-logstore success

查询索引示例代码

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

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Index;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetIndexResponse;

public class ListIndex {
    public static void main(String[] args) throws LogException {
        // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 输入Project名称。
        String projectName = "ali-test-project";
        // 输入Logstore名称。
        String logstoreName = "ali-test-logstore";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

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

        try {
            Index index = new Index();
            System.out.println("ready to get index");

            GetIndexResponse response = client.GetIndex(projectName, logstoreName);

            index = response.GetIndex();
            // 输出索引。
            System.out.println("The index is :" + index.ToJsonString());

            System.out.println(String.format("get index for %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 get index
The index is :{"log_reduce":false,"line":{"caseSensitive":false,"chn":false,"token":[","," ","'","\"",";","=","(",")","[","]","{","}","?","@","&","<",">","/",":","\n","\t","\r"]},"keys":{"request_method":{"doc_value":true,"caseSensitive":true,"chn":false,"alias":"","type":"text","token":[","," ","'","\"",";","=","(",")","[","]","{","}","?","@","&","<",">","/",":","\n","\t","\r"]},"status":{"doc_value":true,"alias":"","type":"long"}},"ttl":30,"max_text_len":2048}
get index for ali-test-logstore success

删除索引示例代码

以下代码用于删除指定Logstore的索引信息。

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;

public class DeleteIndex {
    public static void main(String[] args) throws LogException {
        // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // 输入Project名称。
        String projectName = "ali-test-project";
        // 输入Logstore名称。
        String logstoreName = "ali-test-logstore";
        // 设置日志服务的服务接入点。此处以杭州为例,其它地域请根据实际情况填写。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

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

        try {
            System.out.println("ready to delete index");

            client.DeleteIndex(projectName, logstoreName);

            System.out.println(String.format("delete index for %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 index
delete index for ali-test-logstore success

相关文档

  • 在调用API接口过程中,若服务端返回结果中包含错误信息,则表示调用API接口失败。您可以参考API错误码对照表查找对应的解决方法。更多信息,请参见API错误处理对照表。

  • 阿里云OpenAPI开发者门户提供调试、SDK、示例和配套文档。通过OpenAPI,您无需手动封装请求和签名操作,就可以快速对日志服务API进行调试。更多信息,请参见OpenAPI开发者门户。

  • 为满足越来越多的自动化日志服务配置需求,日志服务提供命令行工具CLI(Command Line Interface)。更多信息,请参见日志服务命令行工具CLI。

  • 关于Index API接口说明,请参见如下:

    • CreateIndex - 创建索引

    • GetIndex - 获取索引

    • UpdateIndex - 更新索引

    • DeleteIndex - 删除索引

  • 更多示例代码,请参见Aliyun Log Java SDK on GitHub。

相关文章

使用Java SDK管理索引 2025-04-22 10:35

索引是一种倒排的数据存储结构,由关键词和指向实际数据的逻辑指针组成,用于快速根据关键词定位到具体数据行,类似于数据的目录。您只有配置索引后,才能进行查询和分析操作。本文通过代码示例介绍如何创建、修改、查询、删除索引。 前提条件

使用Java SDK写入日志 2025-04-22 10:35

如果想将应用程序的运行日志、操作系统日志、用户日志等上传到日志服务,您可以使用日志服务Java SDK提供的PutLogs方法。本文主要介绍使用日志服务Java SDK将日志写入到日志服务的操作步骤。 前提条件

使用Aliyun Log Java Producer写入日志数据 2025-04-22 10:35

如果您在使用Flink、Spark、Storm等大数据计算引擎时,需要将日志进行压缩、批量上传日志到日志服务、减少网络传输资源的占用,API或者SDK往往无法满足大数据场景对数据写入能力的要求,您可以使用Aliyun Log Java Producer,便捷高效地将数据上传到日志服务。

使用GetLogs接口查询日志 2025-04-22 10:35

完成日志采集后,您可以调用GetLogs接口查询采集到的日志。本文介绍GetLogs接口示例。 前提条件 已完成日

使用Java SDK管理快速查询 2025-04-22 10:35

当您需要频繁查看某一查询和分析结果时,可以将对应查询和分析语句另存为快速查询。本文通过代码示例介绍如何创建、修改、查询、删除快速查询等。 前提条件

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