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.

Prerequisites

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

Sample code

In this example, the SLSQuickStart.java 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 = "cn-hangzhou.log.aliyuncs.com"; 
    // 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));
        TimeUnit.SECONDS.sleep(60*2);
    }

    // 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));
        TimeUnit.SECONDS.sleep(60);
    }

    // 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();
        index.FromJsonString(logstoreIndex);
        client.CreateIndex(projectName, logstoreName, index);
        System.out.println(String.format("create index for %s success",logstoreName));
        TimeUnit.SECONDS.sleep(60);
    }

    // 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");
            logGroup.add(logItem);
        }
        client.PutLogs(projectName, logstoreName, "", logGroup, "");
        System.out.println(String.format("push logs for %s success",logstoreName));
        TimeUnit.SECONDS.sleep(5);
    }

    // Executes an SQL statement to query log data. 
     static void getLogs() 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);
            }
            System.out.println("********************");
        }
    }

    public static void main(String[] args) throws LogException, InterruptedException {
       // Creates a project. 
        createProject();
        // Creates a Logstore. 
        createLogstore();
        // Creates an index. 
        createIndex();
        // Writes log data. 
        pushLogs();
        // Queries log data. 
        getLogs();
    }
}

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

Response

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
********************
........