All Products
Search
Document Center

Simple Log Service:Gunakan Simple Log Service SDK untuk Java guna mengelola indeks

Last Updated:Jun 21, 2026

Indeks adalah struktur penyimpanan terbalik yang terdiri dari kata kunci dan pointer logis yang merujuk ke data aktual. Anda dapat menggunakan indeks untuk menemukan baris data secara cepat berdasarkan kata kunci, mirip dengan katalog data. Pengkuerian dan analisis data log hanya dapat dilakukan setelah indeks dikonfigurasi. Topik ini menjelaskan cara membuat, memodifikasi, mengkueri, dan menghapus indeks menggunakan Simple Log Service SDK untuk Java serta menyediakan contoh kode.

Prasyarat

  • Simple Log Service SDK untuk Java telah diinstal. Untuk informasi selengkapnya, lihat Instal Java SDK.

  • Anda telah menulis log ke Logstore. Untuk informasi selengkapnya, lihat Ikhtisar pengumpulan data.

Perhatian

Pada contoh ini, endpoint publik Simple Log Service untuk wilayah China (Hangzhou) digunakan. Titik akhir: https://cn-hangzhou.log.aliyuncs.com.

Jika Anda ingin mengakses Simple Log Service dari layanan Alibaba Cloud lain yang berada di wilayah yang sama dengan proyek Anda, Anda dapat menggunakan endpoint internal Simple Log Service, yaitu https://cn-hangzhou-intranet.log.aliyuncs.com.

Untuk informasi selengkapnya mengenai wilayah dan titik akhir Simple Log Service yang didukung, lihat Titik akhir.

Log mentah

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

Kode contoh untuk membuat indeks

Anda dapat mengelola indeks dengan mudah melalui Konsol Simple Log Service. Untuk informasi selengkapnya, lihat Buat indeks.

Kode contoh berikut menunjukkan cara membuat indeks bernama testindex. Pada contoh ini, pengindeksan teks penuh diaktifkan, dan pengindeksan bidang diaktifkan untuk bidang request_method dan status. Contoh ini didasarkan pada log mentah.

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 {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Tentukan nama proyek.
        String projectName = "ali-test-project";
        // Tentukan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "https://cn-hangzhou.log.aliyuncs.com";
        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);
        try {
            // Definisikan indeks.
            // Sebelum membuat indeks, Anda harus merencanakan konfigurasi indeks teks penuh dan indeks bidang. Pada contoh ini, indeks teks penuh diaktifkan dan indeks bidang dibuat untuk bidang request_method dan 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;
        }
    }
}

Hasil yang diharapkan:

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

Kode contoh untuk memodifikasi indeks

Anda dapat mengelola indeks dengan mudah melalui Konsol Simple Log Service. Untuk informasi selengkapnya, lihat Buat indeks.

Kode contoh berikut menunjukkan cara memodifikasi indeks. Pada contoh ini, pengindeksan teks penuh diaktifkan, pengindeksan bidang diaktifkan untuk bidang request_method dan status, serta caseSensitive diatur ke True untuk bidang request_method. Contoh ini didasarkan pada log mentah.

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 {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Tentukan nama proyek.
        String projectName = "ali-test-project";
        // Tentukan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "https://cn-hangzhou.log.aliyuncs.com";
        // Buat client Simple Log Service.
        Client client = new Client(host, accessId, accessKey);
        try {
            // Perbarui indeks.
            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;
        }
    }
}

Hasil yang diharapkan:

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

Kode contoh untuk mengkueri indeks

Kode berikut menunjukkan cara mengkueri konfigurasi indeks dari Logstore tertentu.

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 {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Tentukan nama proyek.
        String projectName = "ali-test-project";
        // Tentukan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "https://cn-hangzhou.log.aliyuncs.com";
        // Buat client Simple Log Service.
        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();
            // Cetak konfigurasi indeks.
            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;
        }
    }
}

Hasil yang diharapkan:

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

Kode contoh untuk menghapus indeks

Kode berikut menunjukkan cara menghapus indeks dari Logstore tertentu.

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
public class DeleteIndex {
    public static void main(String[] args) throws LogException {
        // Contoh ini mengambil ID AccessKey dan Rahasia AccessKey dari variabel lingkungan.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Tentukan nama proyek.
        String projectName = "ali-test-project";
        // Tentukan nama Logstore.
        String logstoreName = "ali-test-logstore";
        // Tetapkan titik akhir Simple Log Service. Contoh ini menggunakan titik akhir wilayah China (Hangzhou). Ganti nilai ini dengan titik akhir yang sebenarnya.
        String host = "https://cn-hangzhou.log.aliyuncs.com";
        // Buat client Simple Log Service.
        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;
        }
    }
}

Hasil yang diharapkan:

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

Referensi