All Products
Document Center

SDK for Java

Last Updated: Apr 11, 2018

This topic provides a Java example.

Introduce the POM dependency


Start a Java program

public class LogstashForJavaDemo {

    public static void main(String[] args) throws LogException {
        DateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

         * endpoint: Area of the storage where the data is written. 
         * project and logstore: form the basic data storage unit.
         * accessKeyId and accessKeySecret: form the access key.
         * Note: Specify these parameters based on the actual situation.
        String endpoint = "";
        String project = "proj-arms-7dd6ecb06d21e02aed9eeb56b79e9f";
        String logstore = "logstore-56f96ec5546fb6555ef97dd057acb4e9";
        String accessKeyId = "xxx";
        String accessKeySecret = "yyy";

        int logGroupSize = 10;//The recommended value range is 100-2000. It defines the maximum size of data that can be sent in each batch.
        List<String> examples = new ArrayList<String>();
        examples.add("|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Men's wear&Region=Hangzhou&eventTeyp=1&Gender=1&Price=2140|");
        examples.add("|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Men's wear&Region=Shanghai&eventTeyp=3&Gender=1&Price=2917|");
        examples.add("|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Men's wear&Region=Hangzhou&eventTeyp=3&Gender=1&Price=7864|");
        examples.add("|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Women's wear&Region=Hangzhou&eventTeyp=5&Gender=0&Price=2983|");
        //Build a client instance.
        Client client = new Client(endpoint, accessKeyId, accessKeySecret);
        //Send 10 data packages with 10 logs successively.
        long currentTime = System.currentTimeMillis();
        String formatedTime = dateFormat.format(new Date(currentTime));
        for (int i = 0; i < 10; i++) {
            Vector<LogItem> logGroup = new Vector<LogItem>();
            for (int j = 0; j < logGroupSize; j++) {
                LogItem logItem = new LogItem();
                logItem.PushBack("content", formatedTime + examples.get(j % examples.size()) + UUID.randomUUID());
            PutLogsRequest req = new PutLogsRequest(project, logstore, "", "", logGroup);
        System.out.println("send data success");

Important parameter description

Parameter Description
endpoint Area where data is written (endpoint list)
accessKeyId Key ID for writing data
accessKeySecret Key passcode for writing data
project ID of the project to which data is written
logstore ID of the Logstore to which data is written


  • The accessKeyId and accessKeySecret issued by ARMS are not the Alibaba Cloud AK/SK, and must be obtained from ARMS. For more information, see SDK data source overview.
  • Project ID and Logstore ID jointly identify a unique data source.