edit-icon download-icon

SDK for Java

Last Updated: Apr 11, 2018

This topic provides a Java example.

Introduce the POM dependency

<dependency>
  <groupId>com.aliyun.openservices</groupId>
  <artifactId>aliyun-log</artifactId>
  <version>0.6.6</version>
</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 = "cn-hangzhou.log.aliyuncs.com";
        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=Houseware&Region=Shanghai&eventTeyp=3&Gender=0&Price=8305|");
        examples.add("|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Foods&Region=Shenzhen&eventTeyp=3&Gender=1&Price=7121|");
        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=Foods&Region=Shanghai&eventTeyp=1&Gender=1&Price=4285|");
        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|");
        examples.add("|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Foods&Region=Shenzhen&eventTeyp=5&Gender=1&Price=3201|");
        //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());
                logGroup.add(logItem);
            }
            PutLogsRequest req = new PutLogsRequest(project, logstore, "", "", logGroup);
            client.PutLogs(req);
        }
        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

Note:

  • 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.
Thank you! We've received your feedback.