### Build a Log Service Client
Source: https://github.com/aliyun/aliyun-log-java-sdk/blob/master/README_EN.md
Initializes the Log Service client with host, access ID, and access key. Optionally enables metric store URL for improved performance with metricstores.
```java
String accessId = "your_access_id";
String accessKey = "your_access_key";
String host = "your_endpoint";
Client client = new Client(host, accessId, accessKey);
// Notes on UseMetricStoreUrl:
// 1. The setting is global at the Client level. It auto-appends the hash key (METRICS_STORE_AUTO_HASH).
// Use it only when the destination Store is a Metricstore — for Metricstores with a large
// time-series cardinality it improves time-series query performance.
// 2. When both Logstore and Metricstore writes coexist, the caller must manually set HashKey
// to METRICS_STORE_AUTO_HASH to trigger the auto-hash write path for the Metricstore.
client.setUseMetricStoreUrl(true);
```
--------------------------------
### Create a Logstore
Source: https://github.com/aliyun/aliyun-log-java-sdk/blob/master/README_EN.md
Creates a new Logstore within a specified project. Configures the Logstore's time-to-live (TTL) and shard count.
```java
String project = "your_project_name";
String logstore = "your_logstore";
int ttl_in_day = 3;
int shard_count = 10;
LogStore store = new LogStore(logstore, ttl_in_day, shard_count);
CreateLogStoreResponse res = client.CreateLogStore(project, store);
```
--------------------------------
### Maven Dependency with Protobuf Exclusion
Source: https://github.com/aliyun/aliyun-log-java-sdk/blob/master/README_EN.md
Provides a Maven dependency configuration for the Aliyun Log SDK that excludes the default protobuf-java dependency to resolve conflicts.
```xml
com.aliyun.openservices
aliyun-log
0.6.97
jar-with-dependencies
com.google.protobuf
protobuf-java
```
--------------------------------
### Maven Dependency for Aliyun Log SDK
Source: https://github.com/aliyun/aliyun-log-java-sdk/blob/master/README_EN.md
Specifies the Maven dependency required to include the Aliyun Log Java SDK in your project.
```xml
com.aliyun.openservices
aliyun-log
0.6.97
```
--------------------------------
### Write Data to Logstore
Source: https://github.com/aliyun/aliyun-log-java-sdk/blob/master/README_EN.md
Writes multiple log items to a specified Logstore. Each log item includes a timestamp and key-value pairs. Includes error handling for LogException.
```java
int numLogGroup = 10;
/**
* Send numLogGroup log packets to Log Service. Each packet contains 2 log lines.
*/
for (int i = 0; i < numLogGroup; i++) {
List logGroup = new ArrayList();
LogItem logItem = new LogItem((int) (new Date().getTime() / 1000));
logItem.PushBack("level", "info");
logItem.PushBack("name", String.valueOf(i));
logItem.PushBack("message", "it's a test message");
logGroup.add(logItem);
LogItem logItem2 = new LogItem((int) (new Date().getTime() / 1000));
logItem2.PushBack("level", "error");
logItem2.PushBack("name", String.valueOf(i));
logItem2.PushBack("message", "it's a test message");
logGroup.add(logItem2);
try {
client.PutLogs(project, logStore, topic, logGroup, "");
} catch (LogException e) {
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
System.out.println("error requestId :" + e.GetRequestId());
throw e;
}
}
```
--------------------------------
### Read Data from Logstore
Source: https://github.com/aliyun/aliyun-log-java-sdk/blob/master/README_EN.md
Reads log data from a specific shard within a Logstore. It retrieves a cursor for the initial read and then iteratively fetches batches of logs using cursors.
```java
int shardId = 0; // Read data from shard 0 only
GetCursorResponse res;
try {
// Cursor for the first batch of logs received in the last hour
long fromTime = (int)(System.currentTimeMillis()/1000.0 - 3600);
res = client.GetCursor(project, logStore, shardId, fromTime);
System.out.println("shard_id:" + shardId + " Cursor:" + res.GetCursor());
} catch (LogException e) {
e.printStackTrace();
}
String cursor = res.GetCursor();
while(true) {
BatchGetLogResponse logDataRes = client.BatchGetLog(
project, logStore, shardId, 100, cursor);
// Data returned by the server
List logGroups = logDataRes.GetLogGroups();
String nextCursor = logDataRes.GetNextCursor(); // Cursor for the next read
System.out.print("The Next cursor:" + nextCursor);
if (cursor.equals(nextCursor)) {
break;
}
cursor = nextCursor;
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.