检索时间线
以下示例用于查询指定时序表中度量名称为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());
}
}
}