您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Apache IoTDB的SQL操作方法是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
存儲組操作
# 創(chuàng)建存儲組
IoTDB> set storage group to root.turbine
# 查詢存儲組
IoTDB> SHOW STORAGE GROUP
+-------------+
|storage group|
+-------------+
| root.turbine|
+-------------+
# 刪除存儲組
IoTDB> delete storage group root.turbine
創(chuàng)建時間序列
create timeseries root.turbine.d1.s1(temperature1) with datatype=FLOAT, encoding=GORILLA, compression=SNAPPY tags(unit=degree, owner=user1) attributes(description=mysensor1, location=BeiJing)create timeseries root.turbine.d1.s2(temperature2) with datatype=FLOAT, encoding=GORILLA, compression=SNAPPY tags(unit=degree, owner=user1) attributes(description=mysensor2, location=TianJin)create timeseries root.turbine.d2.s1(temperature1) with datatype=FLOAT, encoding=GORILLA, compression=SNAPPY tags(unit=degree, owner=user2) attributes(description=mysensor3, location=HeBei)
上邊注冊的序列可視化就是下邊這個圖了(手畫的。。目前沒可視化功能)
為了實際應(yīng)用中使用更方便,除了時間序列的路徑和編碼等基本信息外,我們增加了測點別名、標簽、屬性三個概念。標簽和屬性總大小在配置文件中 tag_attribute_total_size 設(shè)置。
別名:測點的別名,可以和測點名一樣用來讀寫,可以不設(shè)置。
標簽:key=value 形式,可以通過標簽反向查詢時間序列元數(shù)據(jù),比如,單位和擁有者,標簽會常駐內(nèi)存。目前只能給定一個 tag 查詢條件,可精確查詢和模糊查詢。
屬性:key=value 形式,只能根據(jù)時間序列路徑展示出屬性信息,如描述信息和位置。如果沒有反向查詢的需求,建議定義成屬性。
# 插入更新 別名、標簽、屬性ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(unit=Degree, owner=me) ATTRIBUTES(description=ha, newAttr=v1)# 刪除時間序列delete timeseries root.turbine.d2.s1
根據(jù)路徑和標簽查詢序列元數(shù)據(jù)
# 查詢所有時間序列數(shù)據(jù)
IoTDB> show timeseries
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
| timeseries| alias|storage group|dataType|encoding|compression|description|location|owner| unit|
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
|root.turbine.d1.s1|temperature1| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor1| BeiJing|user1|degree|
|root.turbine.d1.s2|temperature2| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor2| TianJin|user1|degree|
|root.turbine.d2.s1|temperature1| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor3| HeBei|user2|degree|
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
# 查詢 root.turbine.d1 前綴路徑下的時間序列
# 根據(jù) tag 精確查詢 owner 為 user1 的序列
IoTDB> show timeseries root.turbine.d1
IoTDB> show timeseries root.turbine where owner=user1
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
| timeseries| alias|storage group|dataType|encoding|compression|description|location|owner| unit|
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
|root.turbine.d1.s1|temperature1| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor1| BeiJing|user1|degree|
|root.turbine.d1.s2|temperature2| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor2| TianJin|user1|degree|
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
# 根據(jù) tag 模糊查詢 owner 的 value 中包含 'user' 的序列
IoTDB> show timeseries where owner contains 'user'
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
| timeseries| alias|storage group|dataType|encoding|compression|description|location|owner| unit|
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
|root.turbine.d1.s1|temperature1| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor1| BeiJing|user1|degree|
|root.turbine.d1.s2|temperature2| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor2| TianJin|user1|degree|
|root.turbine.d2.s1|temperature1| root.turbine| FLOAT| GORILLA| SNAPPY| mysensor3| HeBei|user2|degree|
+------------------+------------+-------------+--------+--------+-----------+-----------+--------+-----+------+
查看某個路徑的孩子節(jié)點
IoTDB> show child paths root.turbine
+---------------+
| child paths|
+---------------+
|root.turbine.d1|
|root.turbine.d2|
+---------------+
統(tǒng)計時間序列數(shù)量
# 統(tǒng)計所有時間序列數(shù)量
IoTDB> count timeseries
+-----+
|count|
+-----+
| 3|
+-----+
# 分組統(tǒng)計時間序列,root 為第 0 層
IoTDB> count timeseries group by level=2
+---------------+-----+
| column|count|
+---------------+-----+
|root.turbine.d1| 2|
|root.turbine.d2| 1|
+---------------+-----+
查詢所有設(shè)備
也就是查詢倒數(shù)第二層節(jié)點的路徑
IoTDB> show devices+---------------+| devices|+---------------+|root.turbine.d1||root.turbine.d2|+---------------+
DML 數(shù)據(jù)操作語言
參考文檔:
http://iotdb.apache.org/UserGuide/Master/Operation%20Manual/DML%20Data%20Manipulation%20Language.html
數(shù)據(jù)寫入
一次可以寫入一個設(shè)備、一個時間戳、多個測點的值。
insert into root.turbine.d1(timestamp,s1,s2) values(1,1,2);insert into root.turbine.d1(timestamp,s1,s2) values(2,1,2);insert into root.turbine.d1(timestamp,s1,s2) values(3,1,2);insert into root.turbine.d1(timestamp,s1,s2) values(4,1,2);insert into root.turbine.d1(timestamp,s1,s2) values(5,1,2);insert into root.turbine.d1(timestamp,s1,s2) values(6,1,2);insert into root.turbine.d1(timestamp,s1,s2) values(10,1,2);
數(shù)據(jù)刪除
目前只支持刪除一個時間點之前的數(shù)據(jù),之后會支持刪除任意一段時間的數(shù)據(jù)。
delete from root.turbine.d2.s1 where time <= 10
原始數(shù)據(jù)查詢
接下來就到各種查詢啦,最常用的是原始數(shù)據(jù)查詢。
IoTDB> select s1, s2 from root.turbine.d1+-----------------------------+------------------+------------------+| Time|root.turbine.d1.s1|root.turbine.d1.s2|+-----------------------------+------------------+------------------+|1970-01-01T08:00:00.001+08:00| 1.0| 2.0||1970-01-01T08:00:00.002+08:00| 1.0| 2.0||1970-01-01T08:00:00.003+08:00| 1.0| 2.0||1970-01-01T08:00:00.004+08:00| 1.0| 2.0||1970-01-01T08:00:00.005+08:00| 1.0| 2.0||1970-01-01T08:00:00.006+08:00| 1.0| 2.0||1970-01-01T08:00:00.010+08:00| 1.0| 2.0|+-----------------------------+------------------+------------------+
單點補空值查詢
傳感器采集的數(shù)據(jù)很多時間戳有偏差,時間戳精確查詢?nèi)菀撞椴坏綌?shù)據(jù),可以用 previous 或者 linear 方式補空值
IoTDB> select s1 from root.turbine.d1 where time = 8
+----+------------------+
|Time|root.turbine.d1.s1|
+----+------------------+
+----+------------------+
# 用前邊最近的值填過來
IoTDB> select s1 from root.turbine.d1 where time = 8 fill(float[previous])
+-----------------------------+------------------+
| Time|root.turbine.d1.s1|
+-----------------------------+------------------+
|1970-01-01T08:00:00.008+08:00| 1.0|
+-----------------------------+------------------+
# 如果想限制補值的范圍,超過這個范圍就不補了,可以再加個參數(shù),要帶單位
IoTDB> select s1 from root.turbine.d1 where time = 8 fill(float[previous,1ms])
+-----------------------------+------------------+
| Time|root.turbine.d1.s1|
+-----------------------------+------------------+
|1970-01-01T08:00:00.008+08:00| null|
+-----------------------------+------------------+
最新數(shù)據(jù)查詢
為了實時的可視化最新數(shù)據(jù),我們單獨做了一個最新數(shù)據(jù)點查詢功能。用 select last 關(guān)鍵字作為前綴,其他語法和原始數(shù)據(jù)一樣,不能加謂詞過濾。
IoTDB> select last * from root+-----------------------------+------------------+-----+| Time| timeseries|value|+-----------------------------+------------------+-----+|1970-01-01T08:00:00.010+08:00|root.turbine.d1.s1| 1.0||1970-01-01T08:00:00.010+08:00|root.turbine.d1.s2| 2.0|+-----------------------------+------------------+-----+
聚合查詢
統(tǒng)計時間序列的聚合值,我們目前把各個時間序列都當做獨立的序列看待,聚合也是分序列做。下個版本會加上聚合一個路徑下所有序列的功能。
IoTDB> select count(*) from root where time <= 10+-------------------------+-------------------------+-------------------------+|count(root.turbine.d1.s1)|count(root.turbine.d1.s2)|count(root.turbine.d2.s1)|+-------------------------+-------------------------+-------------------------+| 7| 7| 0|+-------------------------+-------------------------+-------------------------+
0.10.0 降頻聚合查詢
降頻聚合0.10的語法和0.9 的不一樣了。首先介紹 0.10.0 版本的降頻聚合查詢語法,先舉個例子,查一個序列今年5月份每天早上9點到12點的平均值,結(jié)果應(yīng)該類似這樣的:
5月1日 9點-12點:聚合值
5月2日 9點-12點:聚合值
...
5月31日 9點-12點:聚合值
為了實現(xiàn)這個靈活的查詢,需要一個滑動窗口,窗口從5月1日9點開始,長度是3小時,每次往前滑動24小時,滑到5月31日為止,每個窗口內(nèi)計算一個平均值。
因此我們主要設(shè)計了三個參數(shù):
(1)滑動窗口的起始和終止范圍,左閉右開區(qū)間:5月1日到31日
(2)滑動窗口的長度:3小時
(3)滑動步長:24小時
語句如下(我沒寫這么多數(shù)據(jù),目前查出來都是空):
select avg(s1) from root.turbine.d1 group by([2020-05-01T09:00:00, 2020-05-31T12:00:00), 3h, 24h)
再舉一個更簡單的例子:查5月份每天的平均值
這個例子里,滑動窗口的長度和滑動步長相等,就可以省掉第三個參數(shù)啦:
select avg(s1) from root.turbine.d1 group by([2020-05-01T00:00:00, 2020-06-01T00:00:00), 1d)
0.10.0 采樣補空值
0.10.0 新增的查詢功能,在 group by 查詢的基礎(chǔ)上,如果我們使用 last_value 聚合函數(shù),就是個采樣功能了,如果某個時間區(qū)間沒有值,也可以使用前值補空。
# 正常降采樣,沒數(shù)據(jù)的區(qū)間會填充 null
IoTDB> select last_value(s1) from root.turbine.d1 group by([1,10), 2ms)
+-----------------------------+------------------------------+
| Time|last_value(root.turbine.d1.s1)|
+-----------------------------+------------------------------+
|1970-01-01T08:00:00.001+08:00| 1.0|
|1970-01-01T08:00:00.003+08:00| 1.0|
|1970-01-01T08:00:00.005+08:00| 1.0|
|1970-01-01T08:00:00.007+08:00| null|
|1970-01-01T08:00:00.009+08:00| null|
+-----------------------------+------------------------------+
# 降采樣,如果某個區(qū)間沒值,可以用前一個聚合值補空,填充函數(shù)為 previous
IoTDB> select last_value(s1) from root.turbine.d1 group by([1,10), 2ms) fill(float[previous])
+-----------------------------+------------------------------+
| Time|last_value(root.turbine.d1.s1)|
+-----------------------------+------------------------------+
|1970-01-01T08:00:00.001+08:00| 1.0|
|1970-01-01T08:00:00.003+08:00| 1.0|
|1970-01-01T08:00:00.005+08:00| 1.0|
|1970-01-01T08:00:00.007+08:00| 1.0|
|1970-01-01T08:00:00.009+08:00| 1.0|
+-----------------------------+------------------------------+
此外,還支持另一種補空值方式,previousuntillast,使用前值補空,直到補到最新點的時間值為止,就不再補了,比如這里最新點時間戳是10,11和13這兩個點就不再補了。
IoTDB> select last_value(s1) from root.turbine.d1 group by((1,15], 2ms) fill(float[previousuntillast])+-----------------------------+------------------------------+| Time|last_value(root.turbine.d1.s1)|+-----------------------------+------------------------------+|1970-01-01T08:00:00.003+08:00| 1.0||1970-01-01T08:00:00.005+08:00| 1.0||1970-01-01T08:00:00.007+08:00| 1.0||1970-01-01T08:00:00.009+08:00| 1.0||1970-01-01T08:00:00.011+08:00| 1.0||1970-01-01T08:00:00.013+08:00| null||1970-01-01T08:00:00.015+08:00| null|+-----------------------------+------------------------------+
不知道大家注意到?jīng)],這句話區(qū)間是前開后閉,填出來的結(jié)果集也是用的閉區(qū)間的時間點。這樣就通過 group by fill 語句實現(xiàn)了采樣補空值查詢。
0.9.x 降頻聚合查詢
0.9 老版本的降頻聚合語法和 0.10 的不一樣。主要有這樣幾個參數(shù)
(1)分段間隔,把時間軸按這個長度分成一段一段的
(2)分割原點,從哪個點開始分,可以采用任意一段的端點,默認以 1970年1月1日0點0時0分0秒為切割原點,也就是時間戳的 0
(3)結(jié)果集的展示范圍
前兩個參數(shù)固定之后,時間軸的分段就確定了,之后第三個參數(shù)指定結(jié)果集。
比如,查詢5月每天的平均值
select avg(s1) from root.turbine.d1 group by (1d, 2020-05-01 00:00:00, [2020-05-01 00:00:00, 2020-05-31 23:59:59]);
按設(shè)備對齊查詢
通過上邊的例子我們可以看到,IoTDB 查詢的默認表結(jié)構(gòu)是【time,序列1,序列2,...,序列n】,所有序列會按照 time 對齊,如果存在某個序列在一個時間點不存在,會補空值,在做值過濾時候,這種表結(jié)構(gòu)的過濾也會很嚴格。
為了使得各個設(shè)備查詢時不互相影響,我們支持按 time 和設(shè)備對齊查詢,表結(jié)構(gòu)為【time,設(shè)備ID,測點1,測點2,...,測點n】,這種就和關(guān)系表結(jié)構(gòu)比較像了,只需要在查詢語句后加 align by device
IoTDB> select * from root align by device+-----------------------------+---------------+---+---+| Time| Device| s1| s2|+-----------------------------+---------------+---+---+|1970-01-01T08:00:00.001+08:00|root.turbine.d1|1.0|2.0||1970-01-01T08:00:00.002+08:00|root.turbine.d1|1.0|2.0||1970-01-01T08:00:00.003+08:00|root.turbine.d1|1.0|2.0||1970-01-01T08:00:00.004+08:00|root.turbine.d1|1.0|2.0||1970-01-01T08:00:00.005+08:00|root.turbine.d1|1.0|2.0||1970-01-01T08:00:00.006+08:00|root.turbine.d1|1.0|2.0||1970-01-01T08:00:00.010+08:00|root.turbine.d1|1.0|2.0|+-----------------------------+---------------+---+---+
“Apache IoTDB的SQL操作方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。