參考文檔:時(shí)間序列數(shù)據(jù)庫(kù) TSDB_時(shí)間序列數(shù)據(jù)庫(kù) TSDB-阿里云幫助中心
什么是時(shí)序數(shù)據(jù)庫(kù)
時(shí)序數(shù)據(jù)是隨時(shí)間不斷產(chǎn)生的一系列數(shù)據(jù),簡(jiǎn)單來(lái)說(shuō),就是帶時(shí)間戳的數(shù)據(jù)。數(shù)據(jù)可能來(lái)自服務(wù)器和應(yīng)用程序的指標(biāo)、物聯(lián)網(wǎng)傳感器的讀數(shù)、網(wǎng)站或應(yīng)用程序上的用戶交互或金融市場(chǎng)上的交易活動(dòng)等。
時(shí)序數(shù)據(jù)的主要數(shù)據(jù)屬性如下:
- 每個(gè)數(shù)據(jù)點(diǎn)都包含用于索引、聚合和采樣的時(shí)間戳;
- 寫多讀少,高頻寫入,寫入操作幾乎是順序添加,大多數(shù)時(shí)候數(shù)據(jù)到達(dá)后都以時(shí)間排序;
- 多用于數(shù)據(jù)的匯總視圖,數(shù)據(jù)匯總?cè)霂?kù),時(shí)間段查詢;
時(shí)序數(shù)據(jù)庫(kù)相關(guān)名詞解釋
- 度量 Metric:Metric 類似關(guān)系型數(shù)據(jù)庫(kù)里的表(Table),代表一系列同類數(shù)據(jù)集合。
- 標(biāo)簽 Tag:Tag 描述數(shù)據(jù)源的特征,通常不隨時(shí)間變化,數(shù)據(jù)庫(kù)內(nèi)部會(huì)自動(dòng)為 Tag 建立索引,支持根據(jù) Tag 來(lái)進(jìn)行多維檢索查詢,Tag 由 Key、Value 組成,兩者均為 String 類型;
- 時(shí)間戳 Timestamp:Timestamp 代表數(shù)據(jù)產(chǎn)生的時(shí)間點(diǎn),可以寫入時(shí)指定,也可由系統(tǒng)自動(dòng)生成;
- 量測(cè)值 Field:Field 描述數(shù)據(jù)源的量測(cè)指標(biāo),通常隨著時(shí)間不斷變化,且通常為可計(jì)算的數(shù)值;
- 數(shù)據(jù)點(diǎn) Data Point: 數(shù)據(jù)源在某個(gè)時(shí)間產(chǎn)生的某個(gè)量測(cè)指標(biāo)值(Field Value)稱為一個(gè)數(shù)據(jù)點(diǎn),數(shù)據(jù)庫(kù)查詢、寫入時(shí)按數(shù)據(jù)點(diǎn)數(shù)來(lái)作為統(tǒng)計(jì)指標(biāo);
- 時(shí)間線 Time Series :數(shù)據(jù)源的某一個(gè)指標(biāo)隨時(shí)間變化,形成時(shí)間線,Metric + Tags 組合確定一條時(shí)間線;針對(duì)時(shí)序數(shù)據(jù)的計(jì)算包括降采樣、聚合(sum、count、max、min等)、插值等都基于時(shí)間線維度進(jìn)行;
- 聚合( Aggregation):當(dāng)同一個(gè)度量(Metric)的查詢有多條時(shí)間線產(chǎn)生(多個(gè)指標(biāo)采集設(shè)備),那么為了將空間的多維數(shù)據(jù)展現(xiàn)為成同一條時(shí)間線,需要進(jìn)行合并計(jì)算。
- 降采樣(Downsampling):當(dāng)查詢的時(shí)間區(qū)間跨度較長(zhǎng)而原始數(shù)據(jù)時(shí)間精度較細(xì)時(shí),為了滿足業(yè)務(wù)需求的場(chǎng)景、提升查詢效率,就會(huì)降低數(shù)據(jù)的查詢展現(xiàn)精度,這就叫做降采樣,比如按秒采集一年的數(shù)據(jù),按照天級(jí)別查詢展現(xiàn)。
TSDB 技術(shù)支持與語(yǔ)法
參考文檔:調(diào)用SDK寫入數(shù)據(jù)_時(shí)間序列數(shù)據(jù)庫(kù) TSDB-阿里云幫助中心
客戶端創(chuàng)建
TSDBConfig config = TSDBConfig // 配置地址,第一個(gè)參數(shù)可以是 TSDB 的域名或 IP。第二個(gè)參數(shù)表示 TSDB 端口。 .address(host, port) // user和password 表示用于用戶認(rèn)證的用戶名和密碼。TSDB用戶可在實(shí)例管理控制臺(tái)創(chuàng)建。如果實(shí)例未啟用用戶鑒權(quán)功能,則創(chuàng)建Config對(duì)象時(shí)無(wú)需調(diào)用basicAuth()方法。 .basicAuth(tsdbUser, basicPwd) // 只讀開關(guān),默認(rèn)為 false。當(dāng) readonly 設(shè)置為 true 時(shí),異步寫開關(guān)會(huì)被關(guān)閉。 .readonly(false) // 網(wǎng)絡(luò)連接池大小,默認(rèn)為64。 .httpConnectionPool(connectPool) // HTTP 等待時(shí)間,單位為秒,默認(rèn)為90秒。 .httpConnectTimeout(timeOut) // HTTP連接存在時(shí)間長(zhǎng)度。單位為秒。默認(rèn)為0,即不生效,為長(zhǎng)連接。建議設(shè)置為一個(gè)合理值,服務(wù)端故障恢復(fù)后,客戶端通過(guò)重新建立連接可達(dá)到服務(wù)端負(fù)載均衡。 .httpConnectionLiveTime(liveTime) // IO 線程數(shù),默認(rèn)為1。 .ioThreadCount(1) // 異步寫開關(guān)。默認(rèn)為 true。推薦異步寫。 .asyncPut(true) // 異步寫相關(guān),客戶端緩沖隊(duì)列長(zhǎng)度,默認(rèn)為10000。 .batchPutBufferSize(batchPutBufferSize) // 異步寫相關(guān),緩沖隊(duì)列消費(fèi)線程數(shù),默認(rèn)為 1。 .batchPutConsumerThreadCount(batchPutConsumerThreadCount) // 異步寫相關(guān),每次批次提交給客戶端點(diǎn)的個(gè)數(shù),默認(rèn)為 500。 .batchPutSize(batchPutSize) // 異步寫相關(guān),每次等待最大時(shí)間限制,單位為 ms,默認(rèn)為 300。 .batchPutTimeLimit(batchPutTimeLimit) // 異步寫相關(guān),寫請(qǐng)求隊(duì)列數(shù),默認(rèn)等于連接池?cái)?shù)??筛鶕?jù)讀寫次數(shù)的比例進(jìn)行配置。 .putRequestLimit(putRequestLimit) //多值寫入相關(guān)配置 .multiFieldBatchPutBufferSize(multiFieldBatchPutBufferSize) .multiFieldBatchPutConsumerThreadCount(multiFieldBatchPutConsumerThreadCount) // 流量限制,設(shè)置每秒最大提交 Point 的個(gè)數(shù)。 .maxTPS(maxTps) // 多值寫入回調(diào)函數(shù) .listenMultiFieldBatchPut(multiFieldCallback) // 單值寫入回調(diào)函數(shù) .listenBatchPut(callback) .config(); // 創(chuàng)建客戶端 TSDB tsdb = TSDBClientFactory.connect(config):
寫入數(shù)據(jù)
// 構(gòu)造 Point Point point = Point.metric("test1") .tag("tagk1", "tagv1") .tag("tagk2", "tagv2") .tag("tagk3", "tagv3") .timestamp(timestamp) .value(123.456) .build();
帶回調(diào)可以獲取:成功數(shù)、返回?cái)?shù)和失敗原因,不要在回調(diào)方法中做耗時(shí)操作;
同步寫入
出于寫入性能的考慮,同步寫建議手動(dòng)將數(shù)據(jù)點(diǎn)打包成一批數(shù)據(jù),并且建議這批數(shù)據(jù)包含500~1000個(gè)數(shù)據(jù)點(diǎn)。此外,也建議多個(gè)線程并發(fā)進(jìn)行提交;
空返回:tsdb.putSync(points)
摘要返回:tsdb.putSync(points, SummaryResult.class);
詳情返回:tsdb.putSync(points, DetailsResult.class);
異步非阻塞寫入
空返回:tsdb.put(points)
摘要返回:tsdb.put(points, BatchPutSummaryCallback.class);
詳情返回:tsdb.put(points, BatchPutDetailsCallback.class)
查詢數(shù)據(jù):
同步查詢
List<QueryResult> result = tsdb.query(query); System.out.println("返回結(jié)果:" + result);
異步查詢
QueryCallback callback = new QueryCallback() { @Override public void response(Query input, List<QueryResult> result) { System.out.println("查詢參數(shù):" + input); System.out.println("返回結(jié)果:" + result); } }; tsdb.query(query, callback);
Query 查詢語(yǔ)法
語(yǔ)法如下
Query query = Query .timeRange(startTime, endTime) // 設(shè)置查詢時(shí)間條件 // 設(shè)置子查詢1 簡(jiǎn)單查詢 .sub(SubQuery.metric("hello").aggregator(Aggregator.AVG).tag("tagk1", "tagv1").build()) // 設(shè)置子查詢2 簡(jiǎn)單查詢 .sub( SubQuery subQuery = SubQuery .metric("test-metric") .aggregator(Aggregator.AVG) .downsample("60m-avg") .tag("tagk1", "tagv1") .tag("tagk2", "tagv2") .build(); ) // 設(shè)置子查詢3 復(fù)雜查詢 .sub( SubQuery subQuery = SubQuery .metric("test-metric") .aggregator(Aggregator.AVG) .downsample("60m-avg") .filter(Filter .filter(FilterType.LiteralOr, "", "") .build() ) .build(); ) .build();
timeRange
左閉右閉,即包含開始事件,也包含結(jié)束事件;
FilterType 類別
LiteralOr:等于查詢,或查詢,類似 SQL 里的 IN 查詢;
NotLiteralOr:等于查詢,或查詢,類似 SQL 里的 NOT IN 查詢;
Wildcard:模糊匹配,類似 SQL 里的 like 查詢;
Regexp:正則匹配;
aggregator 與 downsample
假設(shè)有以下數(shù)據(jù),共四條時(shí)間線;
時(shí)間線計(jì)算方式 = Metric + Tags, 即 Metric + orgCode + portId + inOut;
time |
數(shù)據(jù)組 |
Timestamp |
orgCode |
portId |
inOut |
value |
19:10 |
1 |
1675077000000 |
123456 |
16604 |
8 |
|
20:10 |
2 |
1675080600000 |
123456 | 16604 |
12 |
|
21:10 |
3 |
1675084200000 |
123456 | 16601 |
IN |
2 |
21:10 |
4 |
1675084200000 |
123456 | 16601 |
OUT |
8 |
22:10 |
5 |
1675087800000 |
123456 | 16605 |
4 |
|
23:10 |
6 |
1675091400000 |
123456 | 16606 |
OUT |
6 |
aggregator 聚合
用于控制返回時(shí)間線的多少,即 QueryResult 的條數(shù);
例如查詢:
{ "start": 1675077000000, "end": 1675091400000, "queries": [ { "metric": "xx", "aggregator": "none", "tags": { "orgCode": "123456" } } ] }
會(huì)返回 5 組時(shí)間線,即 1、2 一組,3、4、5、6 各一組,時(shí)間線計(jì)算方式如上圖所示;
{"tags": {"orgCode": "123456", "portId": "16604"},"dps": {1675077000000: 8.0, 1675080600000: 12.0}}, {"tags": {"orgCode": "123456","inOut": "IN", "portId": "16601"},"dps": {1675084200000: 2.0}}, {"tags": {"orgCode": "123456", "portId": "16605"},"dps": {1675087800000: 4.0}}, {"tags": {"orgCode": "123456","inOut": "OUT", "portId": "16606"},"dps": {1675091400000: 6.0}} {"tags": {"orgCode": "123456","portId": "16601", "inOut": "OUT"},"dps": {1675084200000: 8.0}},
aggregator 的作用則是:將除查詢 tag 外的其他時(shí)間線合并,當(dāng) Timestamp 相同時(shí),則會(huì)用 aggregator 指定的算法計(jì)算,例如:
{ "start": 1675077000000, "end": 1675091400000, "queries": [ { "metric": "xx", "aggregator": "sum", "tags": { "orgCode": "123456" } } ] }
返回 1 組時(shí)間線,因?yàn)?1、2、3、4、5 的 orgCode 都相同,會(huì)將 4 條時(shí)間合并,但 1675084200000 變成了 10,因?yàn)樵摃r(shí)間點(diǎn)有兩條數(shù)據(jù),只是 tag 不同罷了,兩條數(shù)據(jù)用了 sum:
{ "aggregateTags": [ "inOut", "portId" ], "dps": { 1675077000000: 8.0, 1675080600000: 12.0, 1675084200000: 10.0, 1675087800000: 4.0, 1675091400000: 6.0 }, "tags": { "orgCode": "123456" } }
downsample 降采樣
用于控制返回時(shí)間線中數(shù)據(jù)點(diǎn)的多少,即 dps 的數(shù);
格式:
[0all | (1)[ s | m | h | d ] ] - [ avg | sum | last | first | count | min | max .... ] [-none | null | zero ]
采樣區(qū)間 區(qū)間操作 補(bǔ)全策略
采樣區(qū)間計(jì)算方式:Timestamp - (Timestamp % Interval)
補(bǔ)全策略包括:
-
- none - 默認(rèn)行為,在序列化期間不輸出缺失值。
- nan - 當(dāng)序列中缺少所有值時(shí),在序列化輸出中輸出NaN ,計(jì)算方式與 none 有類似。
- null - 與NaN的行為相同,只是在序列化期間它會(huì)發(fā)出一個(gè)null,而不是一個(gè)NaN。
- zero - 缺少時(shí)間戳?xí)r替換為零。零值將合并到匯總結(jié)果中。
例如:
{ "start": 1675077000000, // 19:10 "end": 1675091400000, // 23:10 "queries": [ { "metric": "xx", "aggregator": "sum", "downsample": "2h-sum-none", "tags": { "orgCode": "123456" } } ] }
返回 2 小時(shí)一個(gè)匯總,即 18:00、20:00、22:00,關(guān)于區(qū)間的計(jì)算上面已有解釋:
{ "aggregateTags": [ "inOut", "portId" ], "dps": { 1675072800000: 8.0, 1675080000000: 14.0, 1675087200000: 10.0 }, "tags": { "orgCode": "123456" } }
注:此處的 dps 開始時(shí)間 1675072800000 是 2023-01-30 18:00:00,為什么不是查詢的開始時(shí)間,因?yàn)?tsdb 的采樣區(qū)間算法是 Timestamp - (Timestamp % Interval),即 2023-01-30 19:10:00 - (2023-01-30 19:10:00 % 2h)= 2023-01-30 18:00:00,所以當(dāng)使用采樣時(shí),往往計(jì)算的結(jié)果不準(zhǔn)確,原因在這里;
其他接口支持:
以下參考文檔:TSDB如何調(diào)用其他接口_時(shí)間序列數(shù)據(jù)庫(kù) TSDB-阿里云幫助中心
設(shè)置數(shù)據(jù)時(shí)效
tsdb.ttl(time);
查詢 Metric,TagKey,TagValue
參數(shù):(類型,模糊值,查詢條數(shù))
tsdb.suggest(Suggest.Metrics,"hel",10);
tsdb.suggest(Suggest.Tagk,"hel",10);
tsdb.suggest(Suggest.TagV,"hel",10);
TagKey 查 TagValue
參數(shù):(類型,模糊值,查詢條數(shù))
tsdb.dumpMeta("tagk1","tagv1",10);
刪除一段時(shí)間的數(shù)據(jù)
tsdb.deleteData("hello", startTime, nowTime);
刪除指定時(shí)間線
Timeline timeline =Timeline.metric("hello").tag("tagk1","tagv1").build();
tsdb.deleteMeta(timeline);
清空所有表文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-646282.html
tsdb.truncate()文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-646282.html
到了這里,關(guān)于時(shí)間序列數(shù)據(jù)庫(kù) (TSDB)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!