All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.openservices.log.sample.DataSample Maven / Gradle / Ivy

There is a newer version: 0.6.115
Show newest version
package com.aliyun.openservices.log.sample;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.*;
import com.aliyun.openservices.log.common.Consts.CursorMode;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.PullLogsRequest;
import com.aliyun.openservices.log.response.GetCursorResponse;
import com.aliyun.openservices.log.response.ListShardResponse;
import com.aliyun.openservices.log.response.PullLogsResponse;

import java.util.List;

class DataSample {
	private final String endPoint = "";
	private final String akId = "your_access_id";
	private final String ak = "your_access_key";
	private final Client client = new Client(endPoint, akId, ak);
	private final String project = "your_project_name";
	private final String logStore = "your_log_store";
	private final int defaultShardNum = 10;


	public void getCursor() {
		int shardId = 1;
		GetCursorResponse res;
		try {
			long fromTime = (int)(System.currentTimeMillis()/1000.0 - 3600);
			res = client.GetCursor(project, logStore, shardId, fromTime);
			System.out.println("Cursor:" + res.GetCursor());

			res = client.GetCursor(project, logStore, shardId, CursorMode.BEGIN);
			System.out.println("Cursor:" + res.GetCursor());

			res = client.GetCursor(project, logStore, shardId, CursorMode.END);
			System.out.println("shard_id:" + shardId + " Cursor:" + res.GetCursor());
		} catch (LogException e) {
			e.printStackTrace();
		}
	}

	public void pullLogs() {
		try {
			for (int i = 0; i < defaultShardNum; i++) {
				GetCursorResponse cursorRes = client.GetCursor(project,
						logStore, i, CursorMode.BEGIN);
				String cursor = cursorRes.GetCursor();
				for (int j = 0; j < 5; j++) {
                    PullLogsRequest request = new PullLogsRequest(project, logStore, i, 5, cursor);
					PullLogsResponse response = client.pullLogs(request);

					String next_cursor = response.getNextCursor();
					System.out.print("The Next cursor:" + next_cursor);

					List logGroups = response.getLogGroups();
					for (LogGroupData logGroup : logGroups) {
						FastLogGroup fastLogGroup = logGroup.GetFastLogGroup();
						System.out.println("Source:" + fastLogGroup.getSource());
						System.out.println("Topic:" + fastLogGroup.getTopic());
						for (FastLog log : fastLogGroup.getLogs()) {
							System.out.println("LogTime:" + log.getTime());
							List contents = log.getContents();
							for (FastLogContent content : contents) {
								System.out.println(content.getKey() + ":" + content.getValue());
							}
						}
					}

					if (cursor.equals(next_cursor)) {
						break;
					}
					cursor = next_cursor;
				}
			}
		} catch (LogException e) {
			e.printStackTrace();
		}
	}

	public void listShard() {
		try {
			ListShardResponse res = client.ListShard(project, logStore);
			System.out.println("RequestId:" + res.GetRequestId());
			Shard shard = res.GetShards().get(0);
			System.out.println("ShardId:" + shard.GetShardId());
		} catch (LogException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// ------------------------Data API------------------------
		DataSample sample = new DataSample();
		// ------------------------Shard------------------------
		sample.listShard();

		sample.getCursor();
		sample.pullLogs();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy