|
9 | 9 | import com.aliyun.openservices.log.response.ListShardResponse; |
10 | 10 | import com.aliyun.openservices.log.response.PullLogsResponse; |
11 | 11 |
|
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Base64; |
12 | 14 | import java.util.List; |
13 | 15 | import java.util.stream.Collectors; |
14 | 16 |
|
15 | 17 | class PullLogsSample { |
16 | | - private final String endPoint = ""; |
17 | | - private final String akId = "your_access_id"; |
18 | | - private final String ak = "your_access_key"; |
19 | | - private final Client client = new Client(endPoint, akId, ak); |
20 | | - private final String project = "your_project_name"; |
21 | | - private final String logStore = "your_log_store"; |
22 | | - |
23 | | - public static void main(String[] args) { |
24 | | - // ------------------------Data API------------------------ |
25 | | - PullLogsSample sample = new PullLogsSample(); |
26 | | - // ------------------------Shard------------------------ |
27 | | - List<Integer> shardIds = sample.listShard(); |
28 | | - |
29 | | - if (shardIds != null && shardIds.size() > 0) { |
30 | | - // choose a shard to getCursor and pullLogs |
31 | | - Integer shardId = shardIds.get(0); |
32 | | - sample.getCursor(shardId); |
33 | | - sample.pullLogs(shardId); |
34 | | - } |
35 | | - |
36 | | - } |
37 | | - |
38 | | - public void getCursor(int shardId) { |
39 | | - GetCursorResponse res; |
40 | | - try { |
41 | | - long fromTime = (int) (System.currentTimeMillis() / 1000.0 - 3600); |
42 | | - res = client.GetCursor(project, logStore, shardId, fromTime); |
43 | | - System.out.println("Cursor:" + res.GetCursor()); |
44 | | - |
45 | | - res = client.GetCursor(project, logStore, shardId, CursorMode.BEGIN); |
46 | | - System.out.println("Cursor:" + res.GetCursor()); |
47 | | - |
48 | | - res = client.GetCursor(project, logStore, shardId, CursorMode.END); |
49 | | - System.out.println("shard_id:" + shardId + " Cursor:" + res.GetCursor()); |
50 | | - } catch (LogException e) { |
51 | | - e.printStackTrace(); |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - public void pullLogs(int shardId) { |
56 | | - try { |
57 | | - GetCursorResponse cursorRes = client.GetCursor(project, |
58 | | - logStore, shardId, CursorMode.BEGIN); |
59 | | - String cursor = cursorRes.GetCursor(); |
60 | | - int iteration = 100; |
61 | | - |
62 | | - for (int i = 0; i < iteration; i++) { |
63 | | - PullLogsRequest request = new PullLogsRequest(project, logStore, shardId, 1000, cursor); |
64 | | - PullLogsResponse response = client.pullLogs(request); |
65 | | - |
66 | | - String next_cursor = response.getNextCursor(); |
67 | | - System.out.print("The Next cursor:" + next_cursor); |
68 | | - |
69 | | - List<LogGroupData> logGroups = response.getLogGroups(); |
70 | | - for (LogGroupData logGroup : logGroups) { |
71 | | - FastLogGroup fastLogGroup = logGroup.GetFastLogGroup(); |
72 | | - System.out.println("Source:" + fastLogGroup.getSource()); |
73 | | - System.out.println("Topic:" + fastLogGroup.getTopic()); |
74 | | - for (FastLog log : fastLogGroup.getLogs()) { |
75 | | - System.out.println("LogTime:" + log.getTime()); |
76 | | - List<FastLogContent> contents = log.getContents(); |
77 | | - for (FastLogContent content : contents) { |
78 | | - System.out.println(content.getKey() + ":" + content.getValue()); |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - if (cursor.equals(next_cursor)) { |
84 | | - break; |
85 | | - } |
86 | | - cursor = next_cursor; |
87 | | - } |
88 | | - |
89 | | - } catch (LogException e) { |
90 | | - e.printStackTrace(); |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - public List<Integer> listShard() { |
95 | | - try { |
96 | | - ListShardResponse res = client.ListShard(project, logStore); |
97 | | - System.out.println("RequestId:" + res.GetRequestId()); |
98 | | - return res.GetShards().stream().map(Shard::getShardId).collect(Collectors.toList()); |
99 | | - } catch (LogException e) { |
100 | | - e.printStackTrace(); |
101 | | - return null; |
102 | | - } |
103 | | - } |
| 18 | + private final String endPoint = ""; |
| 19 | + private final String akId = "your_access_id"; |
| 20 | + private final String ak = "your_access_key"; |
| 21 | + private final Client client = new Client(endPoint, akId, ak); |
| 22 | + private final String project = "your_project_name"; |
| 23 | + private final String logStore = "your_log_store"; |
| 24 | + |
| 25 | + public static void main(String[] args) { |
| 26 | + // ------------------------Data API------------------------ |
| 27 | + PullLogsSample sample = new PullLogsSample(); |
| 28 | + // ------------------------Shard------------------------ |
| 29 | + List<Integer> shardIds = sample.listShard(); |
| 30 | + |
| 31 | + if (shardIds != null && shardIds.size() > 0) { |
| 32 | + // choose a shard to getCursor and pullLogs |
| 33 | + Integer shardId = shardIds.get(0); |
| 34 | + sample.getCursor(shardId); |
| 35 | + sample.pullLogs(shardId); |
| 36 | + sample.pullLogsWithID(shardId); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public void getCursor(int shardId) { |
| 41 | + GetCursorResponse res; |
| 42 | + try { |
| 43 | + long fromTime = (int) (System.currentTimeMillis() / 1000.0 - 3600); |
| 44 | + res = client.GetCursor(project, logStore, shardId, fromTime); |
| 45 | + System.out.println("Cursor:" + res.GetCursor()); |
| 46 | + |
| 47 | + res = client.GetCursor(project, logStore, shardId, CursorMode.BEGIN); |
| 48 | + System.out.println("Cursor:" + res.GetCursor()); |
| 49 | + |
| 50 | + res = client.GetCursor(project, logStore, shardId, CursorMode.END); |
| 51 | + System.out.println("shard_id:" + shardId + " Cursor:" + res.GetCursor()); |
| 52 | + } catch (LogException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public void pullLogs(int shardId) { |
| 58 | + try { |
| 59 | + GetCursorResponse cursorRes = client.GetCursor(project, |
| 60 | + logStore, shardId, CursorMode.BEGIN); |
| 61 | + String cursor = cursorRes.GetCursor(); |
| 62 | + int iteration = 100; |
| 63 | + |
| 64 | + for (int i = 0; i < iteration; i++) { |
| 65 | + PullLogsRequest request = new PullLogsRequest(project, logStore, shardId, 1000, cursor); |
| 66 | + PullLogsResponse response = client.pullLogs(request); |
| 67 | + String next_cursor = response.getNextCursor(); |
| 68 | + System.out.print("The Next cursor:" + next_cursor); |
| 69 | + |
| 70 | + List<LogGroupData> logGroups = response.getLogGroups(); |
| 71 | + for (LogGroupData logGroup : logGroups) { |
| 72 | + FastLogGroup fastLogGroup = logGroup.getFastLogGroup(); |
| 73 | + System.out.println("Source:" + fastLogGroup.getSource()); |
| 74 | + System.out.println("Topic:" + fastLogGroup.getTopic()); |
| 75 | + for (FastLog log : fastLogGroup.getLogs()) { |
| 76 | + System.out.println("LogTime:" + log.getTime()); |
| 77 | + List<FastLogContent> contents = log.getContents(); |
| 78 | + for (FastLogContent content : contents) { |
| 79 | + System.out.println(content.getKey() + ":" + content.getValue()); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (cursor.equals(next_cursor)) { |
| 85 | + break; |
| 86 | + } |
| 87 | + cursor = next_cursor; |
| 88 | + } |
| 89 | + |
| 90 | + } catch (LogException e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public List<Integer> listShard() { |
| 96 | + try { |
| 97 | + ListShardResponse res = client.ListShard(project, logStore); |
| 98 | + System.out.println("RequestId:" + res.GetRequestId()); |
| 99 | + return res.GetShards().stream().map(Shard::getShardId).collect(Collectors.toList()); |
| 100 | + } catch (LogException e) { |
| 101 | + e.printStackTrace(); |
| 102 | + return null; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static String encodeCursor(long offset) { |
| 107 | + byte[] cursorAsBytes = Base64.getEncoder().encode(String.valueOf(offset).getBytes(StandardCharsets.UTF_8)); |
| 108 | + return new String(cursorAsBytes, StandardCharsets.UTF_8); |
| 109 | + } |
| 110 | + |
| 111 | + public void pullLogsWithID(int shardId) { |
| 112 | + try { |
| 113 | + GetCursorResponse cursorRes = client.GetCursor(project, |
| 114 | + logStore, shardId, CursorMode.BEGIN); |
| 115 | + String cursor = cursorRes.GetCursor(); |
| 116 | + PullLogsRequest request = new PullLogsRequest(project, logStore, shardId, 1000, cursor); |
| 117 | + PullLogsResponse response = client.pullLogs(request); |
| 118 | + |
| 119 | + String next_cursor = response.getNextCursor(); |
| 120 | + System.out.print("The Next cursor:" + next_cursor); |
| 121 | + List<LogGroupData> logGroups = response.getLogGroups(); |
| 122 | + if (logGroups.isEmpty()) { |
| 123 | + return; |
| 124 | + } |
| 125 | + long offset = Long.parseLong(response.getReadLastCursor()) - logGroups.size() + 1; |
| 126 | + String lastCursor = encodeCursor(offset); |
| 127 | + String seqNoPrefix = shardId + "|"; |
| 128 | + for (LogGroupData logGroup : logGroups) { |
| 129 | + FastLogGroup fastLogGroup = logGroup.getFastLogGroup(); |
| 130 | + List<FastLog> logs = fastLogGroup.getLogs(); |
| 131 | + for (int lIdx = 0; lIdx < logs.size(); lIdx++) { |
| 132 | + FastLog log = logs.get(lIdx); |
| 133 | + System.out.println("LogTime:" + log.getTime()); |
| 134 | + List<FastLogContent> contents = log.getContents(); |
| 135 | + for (FastLogContent content : contents) { |
| 136 | + System.out.println(content.getKey() + ":" + content.getValue()); |
| 137 | + } |
| 138 | + String logId = seqNoPrefix + lastCursor + "|" + lIdx; |
| 139 | + System.out.println("LogId:" + logId); |
| 140 | + } |
| 141 | + lastCursor = encodeCursor(++offset); |
| 142 | + } |
| 143 | + } catch (LogException e) { |
| 144 | + e.printStackTrace(); |
| 145 | + } |
| 146 | + } |
104 | 147 | } |
0 commit comments