全部產品
Search
文件中心

Simple Log Service:使用Java SDK管理索引

更新時間:Oct 25, 2024

索引是一種倒排的資料存放區結構,由關鍵詞和指向實際資料的邏輯指標組成,用於快速根據關鍵詞定位到具體資料行,類似於資料的目錄。您只有配置索引後,才能進行查詢和分析操作。本文通過程式碼範例介紹如何建立、修改、查詢、刪除索引。

前提條件

  • 已開通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的對應關係,請參見服務入口

原始日誌範例

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";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        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";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        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";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        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";
        // 設定Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // 建立Log ServiceClient。
        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

相關文檔