Timestream提供了数据点和元数据的查询接口。
元数据查询
元数据查询有两种方式:
- 时间线元数据检索,可以从Name、Tags、Attributes以及lastUpdateTime(数据最近更新时间)等维度进行过滤查询,并且支持翻页查询。
- 对指定时间线元数据进行精确查询。
示例代码
- 多维度组合查询,只返回identifier。
// 多条件组合查询,这里只是部分查询方式的示例,更多使用方式请参考SDK类方法
Filter filter = and(
Name.equal("cpu"), // name查询条件,name等于cpu
Tag.equal("cluster", "AY45"), // tag中cluster字段为AY45X
Tag.notEqual("role", "Server"), // tag中role字段为OTSServer
or( // attribute中status字段为Online或者Broken
Attribute.equal("status", "Online"),
Attribute.equal("status", "Broken")
),
Attribute.notIn("machine", new String[]{"m1", "m2"}), // attribute中machine是在[m1, m2]中
Attribute.prefix("machine", "m"), // attribute中machine的前缀是m
LastUpdateTime.in(TimeRange.range(1000, 2000, TimeUnit.MILLISECONDS)), //时间线的最近更新时间为[1000, 2000)
Attribute.inGeoBoundingBox("loc", "123,456", "234,567") // loc在矩阵范围内
);
TimestreamMetaTable metaTable = db.metaTable();
TimestreamMetaIterator iter1 = db.metaTable()
.filter(filter)
.identifierOnly() // 只返回identifer
.fetchAll();
System.out.print(iterator.getTotalCount()); //获取命中的时间线条数
while (iter1.hasNext()) {
System.out.print(iterator.next().toString());
}
- 指定Name。翻页查询会返回完整的时间线元数据。
// 查询name为cpu的所有时间线
Filter filter = Name.equal("cpu");
TimestreamMetaTable metaTable = db.metaTable();
Iterator<TimestreamMeta> iterator = metaTable.filter(filter)
.offset(2) // 设置offset为2
.fetchAll();
- 查询指定的Attributes。
// 查询name为cpu的所有时间线
Filter filter = Name.equal("cpu");
TimestreamMetaTable metaTable = db.metaTable();
Iterator<TimestreamMeta> iterator = metaTable.filter(filter)
.selectAttributes("machine", "status")
.fetchAll();
- 精确查询单条时间线元数据。
TimestreamIdentifier identifier = new TimestreamIdentifier.Builder("cpu")
.addTag("cluster", "AY45")
.build();
TimestreamMetaTable metaTable = db.metaTable();
TimestreamMeta meta = metaTable.get(identifier).fetch();
数据查询
数据查询目前支持单条时间线的某个时间范围以及准确时间点的数据查询。
示例代码
- 查询某个时间范围内的所有数据。
TimestreamIdentifier identifier = new TimestreamIdentifier.Builder("cpu")
.addTag("cluster", "AY45")
.build();
TimestreamDataTable dataTable = db.dataTable("data_table");
Iterator<Point> iter = dataTable.get(identifier)
.timeRange(TimeRange.range(0, 10000, TimeUnit.MILLISECONDS)) //查询[0, 10000)范围内的数据
.fetchAll();
- 指定列名查询某个时间范围内的所有数据。
TimestreamIdentifier identifier = new TimestreamIdentifier.Builder("cpu")
.addTag("cluster", "AY45")
.build();
TimestreamDataTable dataTable = db.dataTable("data_table");
Iterator<Point> iter = dataTable.get(identifier)
.select("load1", "load5", "load15", "error") // 查询load1,load5, load15, error这四列
.timeRange(TimeRange.range(0, 10000, TimeUnit.MILLISECONDS)) //查询[0, 10000)范围内的数据
.fetchAll();
- 查询某个时间点的数据。
TimestreamIdentifier identifier = new TimestreamIdentifier.Builder("cpu")
.addTag("cluster", "AY45")
.build();
TimestreamDataTable dataTable = db.dataTable("data_table");
Iterator<Point> iter = dataTable.get(identifier)
.select("load1", "load5", "load15", "error") // 查询load1,load5, load15, error这四列
.timestamp(10000, TimeUnit.SECONDS) // 查询时间为10000的数据点
.fetchAll();