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

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

There is a newer version: 0.6.3
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;
import java.util.stream.Collectors;

class PullLogsSample {
	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";

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

		if (shardIds != null && shardIds.size() > 0) {
			// choose a shard to getCursor and pullLogs
			Integer shardId = shardIds.get(0);
			sample.getCursor(shardId);
			sample.pullLogs(shardId);
		}

	}

	public void getCursor(int shardId) {
		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(int shardId) {
		try {
			GetCursorResponse cursorRes = client.GetCursor(project,
					logStore, shardId, CursorMode.BEGIN);
			String cursor = cursorRes.GetCursor();
			int iteration = 100;

			for (int i = 0; i < iteration; i++) {
				PullLogsRequest request = new PullLogsRequest(project, logStore, shardId, 1000, 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 List listShard() {
		try {
			ListShardResponse res = client.ListShard(project, logStore);
			System.out.println("RequestId:" + res.GetRequestId());
			return res.GetShards().stream().map(Shard::getShardId).collect(Collectors.toList());
		} catch (LogException e) {
			e.printStackTrace();
			return null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy