檢索時間軸
以下樣本用於查詢指定時序表中度量名稱為cpu,標籤中含有os標籤且標籤首碼為Ubuntu
的所有時間軸。
private static void queryTimeseriesMeta(TimeseriesClient client) {
// 填寫時序表名稱。
String tableName = "<TIME_SERIES_TABLE>";
QueryTimeseriesMetaRequest queryTimeseriesMetaRequest = new QueryTimeseriesMetaRequest(tableName);
// 查詢度量名稱為cpu,標籤中含有os標籤且首碼為"Ubuntu"的所有時間軸。即measurement_name="cpu" and have_prefix(os, "Ubuntu")。
CompositeMetaQueryCondition compositeMetaQueryCondition = new CompositeMetaQueryCondition(MetaQueryCompositeOperator.OP_AND);
compositeMetaQueryCondition.addSubCondition(new MeasurementMetaQueryCondition(MetaQuerySingleOperator.OP_EQUAL, "cpu"));
compositeMetaQueryCondition.addSubCondition(new TagMetaQueryCondition(MetaQuerySingleOperator.OP_PREFIX, "os", "Ubuntu"));
queryTimeseriesMetaRequest.setCondition(compositeMetaQueryCondition);
queryTimeseriesMetaRequest.setGetTotalHits(true);
// 限制單次請求返回的時間軸中繼資料條數。
queryTimeseriesMetaRequest.setLimit(100);
// 發起查詢。
QueryTimeseriesMetaResponse queryTimeseriesMetaResponse = client.queryTimeseriesMeta(queryTimeseriesMetaRequest);
// 列印合格時間軸的總條數。
System.out.println(queryTimeseriesMetaResponse.getTotalHits());
// 儲存請求結果。
List<TimeseriesMeta> timeseriesMetas = new ArrayList<TimeseriesMeta>();
timeseriesMetas.addAll(queryTimeseriesMetaResponse.getTimeseriesMetas());
// 如果有NextToken,可以繼續發起請求擷取。
while (queryTimeseriesMetaResponse.getNextToken() != null) {
queryTimeseriesMetaRequest.setNextToken(queryTimeseriesMetaResponse.getNextToken());
queryTimeseriesMetaResponse = client.queryTimeseriesMeta(queryTimeseriesMetaRequest);
timeseriesMetas.addAll(queryTimeseriesMetaResponse.getTimeseriesMetas());
// 限制總共擷取的條數。
if (timeseriesMetas.size() >= 1000) {
break;
}
}
System.out.println(timeseriesMetas.size());
for (TimeseriesMeta timeseriesMeta : timeseriesMetas) {
System.out.println(timeseriesMeta.getTimeseriesKey().getMeasurementName());
System.out.println(timeseriesMeta.getTimeseriesKey().getDataSource());
System.out.println(timeseriesMeta.getTimeseriesKey().getTags());
System.out.println(timeseriesMeta.getAttributes());
System.out.println(timeseriesMeta.getUpdateTimeInUs());
}
}
更新時間軸中繼資料
以下樣本用於更新指定時序表中時間軸的屬性資訊。
private static void updateTimeseriesMeta(TimeseriesClient client) {
List<TimeseriesMeta> timeseriesMetaList = new ArrayList<TimeseriesMeta>();
for (int i = 0; i < 10; i++) {
Map<String, String> tags = new HashMap<String, String>();
tags.put("region", "hangzhou");
tags.put("os", "Ubuntu16.04");
// 構造TimeseriesKey。
TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "host_" + i, tags);
TimeseriesMeta meta = new TimeseriesMeta(timeseriesKey);
// 設定時間軸的屬性值(attributes)。
Map<String, String> attrs = new HashMap<String, String>();
attrs.put("status", "online");
meta.setAttributes(attrs);
timeseriesMetaList.add(meta);
}
// 填寫時序表名稱。
String tableName = "<TIME_SERIES_TABLE>";
UpdateTimeseriesMetaRequest updateTimeseriesMetaRequest = new UpdateTimeseriesMetaRequest(tableName);
updateTimeseriesMetaRequest.setMetas(timeseriesMetaList);
UpdateTimeseriesMetaResponse updateTimeseriesMetaResponse = client.updateTimeseriesMeta(updateTimeseriesMetaRequest);
// 檢查是否全部成功。
if (!updateTimeseriesMetaResponse.isAllSuccess()) {
for (UpdateTimeseriesMetaResponse.FailedRowResult failedRowResult : updateTimeseriesMetaResponse.getFailedRows()) {
System.out.println(failedRowResult.getIndex());
System.out.println(failedRowResult.getError());
}
}
}
刪除時間軸中繼資料
以下樣本用於刪除指定時序表中的部分時間軸。
private static void deleteTimeseriesMeta(TimeseriesClient client) {
List<TimeseriesKey> timeseriesKeyList = new ArrayList<TimeseriesKey>();
for (int i = 0; i < 10; i++) {
Map<String, String> tags = new HashMap<String, String>();
tags.put("region", "hangzhou");
tags.put("os", "Ubuntu16.04");
// 構造TimeseriesKey。
TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "host_" + i, tags);
timeseriesKeyList.add(timeseriesKey);
}
// 填寫時序表名稱。
String tableName = "<TIME_SERIES_TABLE>";
DeleteTimeseriesMetaRequest deleteTimeseriesMetaRequest = new DeleteTimeseriesMetaRequest(tableName);
deleteTimeseriesMetaRequest.setTimeseriesKeys(timeseriesKeyList);
DeleteTimeseriesMetaResponse deleteTimeseriesMetaResponse = client.deleteTimeseriesMeta(deleteTimeseriesMetaRequest);
// 檢查是否全部成功。
if (!deleteTimeseriesMetaResponse.isAllSuccess()) {
for (DeleteTimeseriesMetaResponse.FailedRowResult failedRowResult : deleteTimeseriesMetaResponse.getFailedRows()) {
System.out.println(failedRowResult.getIndex());
System.out.println(failedRowResult.getError());
}
}
}