Tous les produits
Search
Centre de documentation

Simple Log Service:Utiliser le SDK Java pour gérer les Logstores

Dernière mise à jour :Aug 18, 2026

Un Logstore est l'unité de collecte, de stockage et d'interrogation des données dans Simple Log Service. Chaque Logstore appartient à un projet, qui peut en contenir plusieurs. Le SDK Java permet de créer, mettre à jour, interroger et supprimer des Logstores.

Prérequis

  • Le SDK Simple Log Service pour Java est installé. Pour plus d'informations, consultez la rubrique Installer le SDK Java.

Précautions

Cet exemple utilise l'endpoint public Simple Log Service de la région Chine (Hangzhou) : https://cn-hangzhou.log.aliyuncs.com.

Pour accéder à Simple Log Service depuis d'autres services Alibaba Cloud situés dans la même région que votre projet, utilisez l'endpoint interne Simple Log Service : https://cn-hangzhou-intranet.log.aliyuncs.com.

Pour plus d'informations sur les régions et les endpoints pris en charge par Simple Log Service, consultez la rubrique Endpoint.

Créer un Logstore

L'exemple suivant crée un Logstore nommé 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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to create logstore");

            // Create a Logstore. Set the data retention period to 60 days and the number of shards to 2. Enable web tracking.
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            // Enable auto split for shards.
            logStore.setmAutoSplit(true);
            // Set the maximum number of shards for auto split to 64.
            logStore.setmMaxSplitShard(64);
            // Enable the feature of recording the public IP address.
            logStore.setAppendMeta(true);
            // Set the storage period of data in the hot tier of the Logstore to 30 days.
            logStore.setHotTTL(30);
            // Set the Logstore type to standard.
            logStore.setMode("standard");

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

Le résultat suivant s'affiche :

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

Mettre à jour un Logstore

L'exemple suivant met à jour la configuration d'un Logstore nommé 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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to update logstore");

            // Update the storage period of data in the hot tier of the Logstore to 45 days.
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            logStore.setHotTTL(45);

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

Le résultat suivant s'affiche :

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

Interroger tous les Logstores

L'exemple suivant répertorie tous les Logstores d'un projet.

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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

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

            // Query 10 Logstores.
            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;
        }
    }
}

Le résultat suivant s'affiche :

ready to list logstore
ali-test-logstore

Interroger un Logstore spécifique

L'exemple suivant interroge un Logstore spécifique.

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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to get logstore");

            // Query the specified 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;
        }
    }
}

Le résultat suivant s'affiche :

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

Supprimer un Logstore

L'exemple suivant supprime un Logstore nommé ali-test-logstore.

Important
  • La suppression d'un Logstore entraîne la suppression définitive et irréversible des données qu'il contient. Agissez avec prudence.

  • Avant de supprimer un Logstore, supprimez toutes les configurations Logtail qui lui sont associées.

  • Si LogShipper est activé pour le Logstore, arrêtez l'écriture de nouvelles données dans le Logstore avant sa suppression. Assurez-vous que toutes les données existantes du Logstore ont été livrées.

  • Si vous utilisez un compte Alibaba Cloud pour supprimer un Logstore et que vous recevez une erreur d'autorisations insuffisantes, soumettez un ticket pour supprimer le 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 {
        // In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // Specify the project name.
        String projectName = "ali-test-project";
        // Set the endpoint of Simple Log Service. In this example, the endpoint of the China (Hangzhou) region is used. Replace it with the actual endpoint.
        String host = "https://cn-hangzhou.log.aliyuncs.com";

        // Create a Simple Log Service client.
        Client client = new Client(host, accessId, accessKey);

        try {
            // Specify the Logstore name.
            String logStoreName = "ali-test-logstore";
            System.out.println("ready to delete logstore");

            // Delete the specified 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;
        }
    }
}

Le résultat suivant s'affiche :

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

Références