All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Java to manage a Logstore

最終更新日:Oct 26, 2023

A Logstore in Simple Log Service is a unit that is used to collect, store, and query data. Each Logstore belongs to a project. You can create multiple Logstores in a project. This topic describes how to create, modify, query, and delete a Logstore by using Simple Log Service SDK for Java and provides sample code.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Java is installed. For more information, see Install Simple Log Service SDK for Java.

Usage notes

In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is https://cn-hangzhou.log.aliyuncs.com. If you want to access Simple Log Service by using other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com. For more information about the supported regions and endpoints of Simple Log Service, see Endpoints.

Sample code that is used to create a Logstore

The following sample code provides an example on how to create a Logstore named 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");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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 {
            // The name of the Logstore. 
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to create logstore");

            // Create a Logstore whose data retention period is 60 days and number of shards is 2, and enable the web tracking feature. 
            LogStore logStore = new LogStore(logstoreName, 60, 2, true);
            // Enable the automatic sharding feature. 
            logStore.setmAutoSplit(true);
            // Set the maximum number of shards into which existing shards can be automatically split to 64. 
            logStore.setmMaxSplitShard(64);
            // Record public IP addresses. 
            logStore.setAppendMeta(true);
            // Set the data retention period of the hot storage tier in the Logstore to 30 days. 
            logStore.setHotTTL(30);
            // Set the Logstore type to Standard. 
            logStore.setMode("standard");

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

The following response shows the expected output:

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

Sample code that is used to modify a Logstore

The following sample code provides an example on how to modify the configurations of the ali-test-logstore 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");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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 {
            // The name of the Logstore. 
            String logstoreName = "ali-test-logstore";
            System.out.println("ready to update logstore");

            // Change the data retention period of the hot storage tier in 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;
        }
    }
}

The following response shows the expected output:

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

Sample code that is used to query all Logstores

The following sample code provides an example on how to query all Logstores in a specified project:

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");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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;
        }
    }
}

The following response shows the expected output:

ready to list logstore
ali-test-logstore

Sample code that is used to query a specific Logstore

The following sample code provides an example on how to query the information about a specific Logstore:

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");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value 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 {
            // The name of the Logstore. 
            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;
        }
    }
}

The following response shows the expected output:

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

Sample code that is used to delete a Logstore

The following sample code provides an example on how to delete the ali-test-logstore Logstore.

Important
  • After you delete a Logstore, all data in the Logstore is deleted and cannot be restored. Proceed with caution.

  • Before you can delete a Logstore, you must delete all Logtail configurations that are associated with the Logstore.

  • If the log shipping feature is enabled for the Logstore, we recommend that you stop writing data to the Logstore and make sure that all data in the Logstore is shipped before you delete the Logstore.

  • If you are not authorized to delete a Logstore with your Alibaba Cloud account, submit a ticket to delete the 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");
        // The name of the project. 
        String projectName = "ali-test-project";
        // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
        String host = "https://cn-hangzhou.log.aliyuncs.com";

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

        try {
            // The name of the Logstore. 
            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;
        }
    }
}

The following response shows the expected output:

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

References