Quick start

Last Updated: Sep 26, 2017

Follow the steps below to start using the Log Service Java SDK quickly.

Step 1 Create an Alibaba Cloud account

An Alibaba Cloud account is required for accessing the Log Service. If you do not have an Alibaba Cloud account, follow the steps below to create one.

  1. Visit the Alibaba Cloud websitewebsite and click Register.
  2. Follow on-screen prompts to complete registration and perform real-name authentication.

To better use Alibaba Cloud services, we suggest you complete real-name authentication as soon as possible; otherwise, some Alibaba Cloud services are not available for use.

Step 2 Obtain an Alibaba Cloud access key

Apply for an Alibaba Cloud access key before using the Log Service Java SDK.

Log on to the Alibaba Cloud key management pagekey management page. Select an SDK-specific access key (consisting of an access key ID and access key secret). If you do not have any, create one and ensure that the access key is “active”. For how to create an access key, refer to Create an access keyCreate an access key.

This access key will be used in the following steps. It must be kept confidential. You can refer to SDK configuration to learn more about how to use the access key in the SDK.

Step 3 Create a Log Service project and LogStore

Currently, the Log Service Java SDK does not support project and LogStore management. You need to create a LogStore on the console before using the SDK.

For details about how to create a project and LogStore, refer to Create a project and Create a LogStore.

NOTE:

  • Ensure that the same Alibaba Cloud account is used to obtain the access key and create the project and LogStore.
  • For details about Log Service projects and LogStores, refer to the Log Service core concepts.
  • A project name must be globally unique in the Log Service, and a LogStore name must be unique under the corresponding project.
  • The region of a project is unchangeable once the project is created. Project migration between different regions of Alibaba Cloud is not supported currently.

Install the Java development environment

Currently, the Log Service Java SDK supports the J2SE 6.0 Java runtime environment and later versions. You can download the installation package at the Java website and follow instructions to install the Java development environment.

Install the Log Service Java SDK

Install the Log Service Java SDK after you build the Java development environment. Currently, two installation methods are available.

Apache Maven is recommended for obtaining the latest SDK version. You can add the following configurations to your Maven project:

  1. <dependency>
  2. <groupId>com.google.protobuf</groupId>
  3. <artifactId>protobuf-java</artifactId>
  4. <version>2.5.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun.openservices</groupId>
  8. <artifactId>aliyun-log</artifactId>
  9. <version>0.6.7</version>
  10. <exclusions>
  11. <exclusion>
  12. <groupId>com.google.protobuf</groupId>
  13. <artifactId>protobuf-java</artifactId>
  14. </exclusion>
  15. </exclusions>
  16. </dependency>

You can download the Java SDK package and then directly reference the local package in your Java project.

  1. Click this link to download the Java SDK package. (Version updates are provided periodically. Use Maven to obtain the latest version.)
  2. Decompress the downloaded package to the specified directory. The Java SDK does not require installation.
  3. Add all .jar packages (including third-party dependent packages) in the SDK package to your Java project. (For detailed instructions, refer to the corresponding IDE document.)

Start a new Java project

Now you can start using the Java SDK. To interact with the Log Service and obtain the relevant output, run the following sample code in a text editor or Java IDE:

  1. package sdksample;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Vector;
  5. import java.util.Date;
  6. import com.aliyun.openservices.log.Client;
  7. import com.aliyun.openservices.log.common.*;
  8. import com.aliyun.openservices.log.exception.*;
  9. import com.aliyun.openservices.log.request.*;
  10. import com.aliyun.openservices.log.response.*;
  11. import com.aliyun.openservices.log.common.LogContent;
  12. import com.aliyun.openservices.log.common.LogGroupData;
  13. import com.aliyun.openservices.log.common.LogItem;
  14. import com.aliyun.openservices.log.common.Consts.CursorMode;
  15. public class sdksample {
  16. public static void main(String args[]) throws LogException,
  17. InterruptedException {
  18. String endpoint = "<log_service_endpoint>"; // Select the endpoint that matches the region of the project created above
  19. // Endpoint
  20. String accessKeyId = "<your_access_key_id>"; // Use your Alibaba Cloud access key ID
  21. String accessKeySecret = "<your_access_key_secret>"; // Use your Alibaba Cloud access key secret
  22. String project = "<project_name>"; // Name of the project created above
  23. String logstore = "<logstore_name>"; // Name of the LogStore created above
  24. //Construct a client instance
  25. Client client = new Client(endpoint, accessKeyId, accessKeySecret);
  26. //List the names of all LogStores under the current project
  27. int offset = 0;
  28. int size = 100;
  29. String logStoreSubName = "";
  30. ListLogStoresRequest req1 = new ListLogStoresRequest(project, offset,
  31. size, logStoreSubName);
  32. ArrayList<String> logStores = client.ListLogStores(req1).GetLogStores();
  33. System.out.println("ListLogs:" + logStores.toString() + "\n");
  34. //Write logs
  35. String topic = "";
  36. String source = "";
  37. // Send 10 packets consecutively, with each packet containing 10 logs
  38. for (int i = 0; i < 10; i++) {
  39. Vector<LogItem> logGroup = new Vector<LogItem>();
  40. for (int j = 0; j < 10; j++) {
  41. LogItem logItem = new LogItem(
  42. (int) (new Date().getTime() / 1000));
  43. logItem.PushBack("index", String.valueOf(i * 10 + j));
  44. logGroup.add(logItem);
  45. }
  46. PutLogsRequest req2 = new PutLogsRequest(project, logstore, topic,
  47. source, logGroup);
  48. client.PutLogs(req2);
  49. /*
  50. You can specify the shard to which data is sent by setting the shard hashkey. Then data is written to the shard that corresponds to the range with the hashkey. Refer to the following API:
  51. public PutLogsResponse PutLogs(
  52. String project,
  53. String logStore,
  54. String topic,
  55. List<LogItem> logItems,
  56. String source,
  57. String shardHash // Write data to the shard based on the hashkey, which may be MD5(ip) or MD5(id)
  58. ) throws LogException;
  59. */
  60. }
  61. // Read the data written to Shard 0 during the past minute.
  62. int shard_id = 0;
  63. long curTimeInSec = System.currentTimeMillis() / 1000;
  64. GetCursorResponse cursorRes = client.GetCursor(project, logstore,
  65. shard_id, curTimeInSec - 60);
  66. String beginCursor = cursorRes.GetCursor();
  67. cursorRes = client.GetCursor(project, logstore, shard_id,
  68. CursorMode.END);
  69. String endCursor = cursorRes.GetCursor();
  70. String curCursor = beginCursor;
  71. while (curCursor.equals(endCursor) == false) {
  72. int loggroup_count = 2; // Read two loggroups at a time
  73. BatchGetLogResponse logDataRes = client.BatchGetLog(project,
  74. logstore, shard_id, loggroup_count, curCursor, endCursor);
  75. List<LogGroupData> logGroups = logDataRes.GetLogGroups();
  76. for(LogGroupData logGroup: logGroups){
  77. FastLogGroup flg = logGroup.GetFastLogGroup();
  78. System.out.println(String.format("\tcategory\t:\t%s\n\tsource\t:\t%s\n\ttopic\t:\t%s\n\tmachineUUID\t:\t%s",
  79. flg.getCategory(), flg.getSource(), flg.getTopic(), flg.getMachineUUID()));
  80. System.out.println("Tags");
  81. for (int tagIdx = 0; tagIdx < flg.getLogTagsCount(); ++tagIdx) {
  82. FastLogTag logtag = flg.getLogTags(tagIdx);
  83. System.out.println(String.format("\t%s\t:\t%s", logtag.getKey(), logtag.getValue()));
  84. }
  85. for (int lIdx = 0; lIdx < flg.getLogsCount(); ++lIdx) {
  86. FastLog log = flg.getLogs(lIdx);
  87. System.out.println("--------\nLog: " + lIdx + ", time: " + log.getTime() + ", GetContentCount: " + log.getContentsCount());
  88. for (int cIdx = 0; cIdx < log.getContentsCount(); ++cIdx) {
  89. FastLogContent content = log.getContents(cIdx);
  90. System.out.println(content.getKey() + "\t:\t" + content.getValue());
  91. }
  92. }
  93. }
  94. String next_cursor = logDataRes.GetNextCursor();
  95. System.out.println("The Next cursor:" + next_cursor);
  96. curCursor = next_cursor;
  97. }
  98. // !!! Important: The next interface is called only when the indexing function is enabled.!!
  99. //Wait 1 minute until logs are queryable
  100. try {
  101. Thread.sleep(60 * 1000);
  102. } catch (InterruptedException e) {
  103. e.printStackTrace();
  104. }
  105. //Query log distribution
  106. String query = "index";
  107. int from = (int) (new Date().getTime() / 1000 - 300);
  108. int to = (int) (new Date().getTime() / 1000);
  109. GetHistogramsResponse res3 = null;
  110. while (true) {
  111. GetHistogramsRequest req3 = new GetHistogramsRequest(project,
  112. logstore, topic, query, from, to);
  113. res3 = client.GetHistograms(req3);
  114. if (res3 != null && res3.IsCompleted()) // If IsCompleted() returns "true", the query results are accurate; if "false" is returned, query is performed again.
  115. {
  116. break;
  117. }
  118. Thread.sleep(200);
  119. }
  120. System.out.println("Total count of logs is " + res3.GetTotalCount());
  121. for (Histogram ht : res3.GetHistograms()) {
  122. System.out.printf("from %d, to %d, count %d.\n", ht.GetFrom(),
  123. ht.GetTo(), ht.GetCount());
  124. }
  125. //Query log data
  126. long total_log_lines = res3.GetTotalCount();
  127. int log_offset = 0;
  128. int log_line = 10;
  129. while (log_offset <= total_log_lines) {
  130. GetLogsResponse res4 = null;
  131. // Read 10 lines of logs at a time for each log offset. If the read operation fails, it is retried three times at most.
  132. for (int retry_time = 0; retry_time < 3; retry_time++) {
  133. GetLogsRequest req4 = new GetLogsRequest(project, logstore,
  134. from, to, topic, query, log_offset, log_line, false);
  135. res4 = client.GetLogs(req4);
  136. if (res4 != null && res4.IsCompleted()) {
  137. break;
  138. }
  139. Thread.sleep(200);
  140. }
  141. System.out.println("Read log count:"
  142. + String.valueOf(res4.GetCount()));
  143. log_offset += log_line;
  144. }
  145. }
  146. }
Thank you! We've received your feedback.