All Products
Search
Document Center

Simple Log Service:Gunakan Java SDK untuk mengelola Logstore

Last Updated:Jun 23, 2026

Logstore adalah unit pengumpulan data, penyimpanan, dan kueri di Simple Log Service. Setiap Logstore termasuk dalam satu Proyek, dan satu Proyek dapat berisi beberapa Logstore. Anda dapat menggunakan Java SDK untuk membuat, memperbarui, mengkueri, dan menghapus Logstore.

Prasyarat

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

Peringatan

Pada contoh ini, titik akhir publik Simple Log Service untuk wilayah China (Hangzhou) digunakan, yaitu 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, gunakan titik akhir internal Simple Log Service, yaitu https://cn-hangzhou-intranet.log.aliyuncs.com.

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

Buat Logstore

Contoh berikut membuat Logstore bernama ali-test-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 {
        // Pada contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh 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";
        // Tetapkan titik akhir Simple Log Service. Pada contoh ini, titik akhir wilayah China (Hangzhou) digunakan. Ganti 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 {
            // Tentukan nama Logstore.
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to create logstore");

            // Buat Logstore. Atur periode retensi data menjadi 60 hari dan jumlah shard menjadi 2. Aktifkan pelacakan web.
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            // Aktifkan pemisahan otomatis untuk shard.
            logStore.setmAutoSplit(true);
            // Tetapkan jumlah maksimum shard untuk pemisahan otomatis menjadi 64.
            logStore.setmMaxSplitShard(64);
            // Aktifkan fitur pencatatan alamat IP publik.
            logStore.setAppendMeta(true);
            // Tetapkan periode penyimpanan data di tier hot Logstore menjadi 30 hari.
            logStore.setHotTTL(30);
            // Tetapkan tipe Logstore menjadi standard.
            logStore.setMode("standard");

            CreateLogStoreRequest request = new CreateLogStoreRequest(projectName, logStore);
            // Buat 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;
        }
    }
}

Output berikut dikembalikan:

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

Perbarui Logstore

Contoh berikut memperbarui konfigurasi Logstore bernama ali-test-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 {
        // Pada contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh 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";
        // Tetapkan titik akhir Simple Log Service. Pada contoh ini, titik akhir wilayah China (Hangzhou) digunakan. Ganti 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 {
            // Tentukan nama Logstore.
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to update logstore");

            // Perbarui periode penyimpanan data di tier hot Logstore menjadi 45 hari.
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            logStore.setHotTTL(45);

            UpdateLogStoreRequest request = new UpdateLogStoreRequest(projectName, logStore);
            // Perbarui 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;
        }
    }
}

Output berikut dikembalikan:

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

Kueri semua Logstore

Contoh berikut mencantumkan semua Logstore dalam suatu Proyek.

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 {
        // Pada contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh 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";
        // Tetapkan titik akhir Simple Log Service. Pada contoh ini, titik akhir wilayah China (Hangzhou) digunakan. Ganti 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 list logstore");

            // Kueri 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;
        }
    }
}

Output berikut dikembalikan:

ready to list logstore
ali-test-logstore

Kueri Logstore tertentu

Contoh berikut mengkueri Logstore tertentu.

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 {
        // Pada contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh 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";
        // Tetapkan titik akhir Simple Log Service. Pada contoh ini, titik akhir wilayah China (Hangzhou) digunakan. Ganti 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 {
            // Tentukan nama Logstore.
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to get logstore");

            // Kueri Logstore yang ditentukan.
            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;
        }
    }
}

Output berikut dikembalikan:

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

Hapus Logstore

Contoh berikut menghapus Logstore bernama ali-test-logstore.

Penting
  • Setelah Logstore dihapus, data di dalamnya akan dihapus secara permanen dan tidak dapat dipulihkan. Lakukan dengan hati-hati.

  • Sebelum menghapus Logstore, hapus semua konfigurasi Logtail yang terkait dengannya.

  • Jika LogShipper diaktifkan untuk Logstore tersebut, hentikan penulisan data baru ke Logstore sebelum menghapusnya. Pastikan semua data yang ada di Logstore telah dikirimkan.

  • Jika Anda menggunakan Akun Alibaba Cloud untuk menghapus Logstore dan menerima kesalahan izin tidak mencukupi, ajukan ticket untuk menghapus Logstore tersebut.

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

public class DeleteLogstore {

    public static void main(String[] args) throws LogException {
        // Pada contoh ini, ID AccessKey dan Rahasia AccessKey diperoleh 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";
        // Tetapkan titik akhir Simple Log Service. Pada contoh ini, titik akhir wilayah China (Hangzhou) digunakan. Ganti 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 {
            // Tentukan nama Logstore.
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to delete logstore");

            // Hapus Logstore yang ditentukan.
            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;
        }
    }
}

Output berikut dikembalikan:

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

Referensi