This topic describes how to get started with Log Service SDK for Java to perform common operations. For example, you can create a project, create a Logstore, write logs, and query logs.


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

Sample code

In this example, the file is created. The sample code in this file shows how to call the required API operations to create a project, create a Logstore, create indexes, write log data, and query log data. Example:
import com.aliyun.openservices.log.common.Index;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.LogStore;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetLogsResponse;
import com.aliyun.openservices.log.Client;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class SLSQuickStart {
    // Configures the required information, such as an AccessKey pair, endpoint, project name, and Logstore name. 
    // The AccessKey secret is used to access Log Service. For more information, see AccessKey pair. An Alibaba Cloud account has permissions to call all API operations. Therefore, security risks may arise if you use the AccessKey pair of an Alibaba Cloud account. We recommend that you log on as a RAM user that has permissions to call API operations or perform routine O&M. 
    static String accessId = "your_access_id"; 
    static String accessKey = "your_access_key";
    // The endpoint of Log Service. For more information, see Endpoints. 
    // In this example, the endpoint of the China (Hangzhou) region is used. Replace the value of the parameter with the actual endpoint. 
    static String host = ""; 
    // Creates Aliyun_Log_Client.  
    static Client client = new Client(host, accessId, accessKey);
    // The name of the project. 
    static String projectName = "aliyun-test-project";
    // The name of the Logstore. 
    static String logstoreName = "aliyun-test-logstore";
    // Creates a query request. 
    static String query = "*| select * from " + logstoreName;

    // Creates a project. 
     static void createProject() throws LogException, InterruptedException {
        String projectDescription = "project description";
        System.out.println("ready to create project");
        client.CreateProject(projectName, projectDescription);
        System.out.println(String.format("create project %s success",projectName));

    // Creates a Logstore. 
     static void createLogstore() throws LogException, InterruptedException {
        System.out.println("ready to create logstore");
        int ttl_in_day = 3; // The data retention period. If you set the data retention period to 3650, the data is permanently stored. Unit: days. 
        int shard_count = 10; // The number of shards. 
        LogStore store = new LogStore(logstoreName, ttl_in_day, shard_count);
        client.CreateLogStore(projectName, store);
        System.out.println(String.format("create logstore %s success",logstoreName));

    // Creates indexes for the Logstore. 
     static void createIndex() throws LogException, InterruptedException {
        System.out.println(String.format("ready to create index for %s", logstoreName));
        String logstoreIndex = "{\"line\": {\"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"chn\": false}, \"keys\": {\"dev\": {\"type\": \"text\", \"token\": [\",\", \" \", \"'\", \"\\\"\", \";\", \"=\", \"(\", \")\", \"[\", \"]\", \"{\", \"}\", \"?\", \"@\", \"&\", \"<\", \">\", \"/\", \":\", \"\\n\", \"\\t\", \"\\r\"], \"caseSensitive\": false, \"alias\": \"\", \"doc_value\": true, \"chn\": false}, \"id\": {\"type\": \"long\", \"alias\": \"\", \"doc_value\": true}}, \"log_reduce\": false, \"max_text_len\": 2048}";
        Index index = new Index();
        client.CreateIndex(projectName, logstoreName, index);
        System.out.println(String.format("create index for %s success",logstoreName));

    // Writes data to the Logstore. 
    // To improve the I/O efficiency of your system, we recommend that you do not use the following method to write data to Log Service. 
    // If you need to transfer a large amount of data at high concurrency, we recommend that you use the Alibaba Cloud Log Java Producer to write log data to Log Service. 
     static void pushLogs() throws LogException, InterruptedException {
        System.out.println(String.format("ready to push logs for %s",logstoreName));
        List<LogItem> logGroup = new ArrayList<LogItem>();
        for (int i = 0; i < 100; ++i) {
            LogItem logItem = new LogItem();
            logItem.PushBack("id", String.valueOf(i));
            logItem.PushBack("dev", "test_push");
        client.PutLogs(projectName, logstoreName, "", logGroup, "");
        System.out.println(String.format("push logs for %s success",logstoreName));

    // Executes an SQL statement to query log data. 
     static void queryLogs() throws LogException {
        System.out.println(String.format("ready to query logs from %s",logstoreName));
        // The fromTime and toTime variables specify the start time and end time of a time range within which you want to query log data. The values of the variables are UNIX timestamps. 
        int fromTime = (int) (System.currentTimeMillis()/1000 - 3600);
        int toTime = fromTime + 3600;
        GetLogsResponse getLogsResponse = client.GetLogs(projectName, logstoreName, fromTime, toTime, "", query);
        for (QueriedLog log : getLogsResponse.getLogs()) {
            for (LogContent mContent : log.mLogItem.mContents) {
                System.out.println(mContent.mKey + " : " + mContent.mValue);

    public static void main(String[] args) throws LogException, InterruptedException {
       // Creates a project. 
        // Creates a Logstore. 
        // Creates an index. 
        // Writes log data. 
        // Queries log data. 

For more information about sample code, see Alibaba Cloud Log Service SDK for Java.


In this example, the following result is returned:

ready to create project
create project aliyun-test-project success
ready to create logstore
create logstore aliyun-test-logstore success
ready to create index for aliyun-test-logstore
create index for aliyun-test-logstore success
ready to push logs for aliyun-test-logstore
push logs for aliyun-test-logstore success
ready to query logs from aliyun-test-logstore
dev : test_push
id : 0
dev : test_push
id : 1
dev : test_push
id : 2
dev : test_push
id : 3
dev : test_push
id : 4