鼎鼎知识库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11.InfluxDB的基本使用.md 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. # 基本概念
  2. InfluxDB基于行协议(line protocol),一个行代表这个point的数据。
  3. ```
  4. weather,location=us-midwest temperature=82 1465839830100400200
  5. 以上代表着:
  6. measurement,tag_set field_set timestamp
  7. weather就是measurement
  8. location=us-midwest就是tag_set, 是一组键值对
  9. temperature就是field_set,是一组键值对
  10. 1465839830100400200就是timestamp,即时间戳(016-06-13T17:43:50.1004002Z)
  11. 注意:
  12. --measurement和field_set以及field_set和timestamp之间都有一个空格
  13. --timestamp是Unix型纳秒级,如果不填,会默认使用服务器的纳秒级UTC时间戳.当使用服务器集群的时候,这些服务器集群的时间必须同步,否则会造成数据的不准确
  14. 举例:
  15. --weather,location=us-midwest,season=summer temperature=82 1465839830100400200
  16. --weather,location=us-midwest temperature=82,humidity=71 1465839830100400200
  17. ```
  18. **数据类型**
  19. ```
  20. 在tag_set中,tag的值是string类型,InfluxDB不能基于tag的string类型值进行运算,即不能把tag的值作为InfluxQL函数的参数
  21. 时间戳,timestamp是UNIX类型,最小时间戳-9223372036854775806,即1677-09-21T00:12:43.145224194Z。最大时间戳9223372036854775806,即2262-04-11T23:47:16.854775806Z。默认情况下时间戳的精度是纳秒,可以通过API更换时间戳的精度。
  22. Field值类型可以是float,integer, string, boolean。
  23. --weather,location=us-midwest temperature=82 1465839830100400200这里的82会被看作是float类型
  24. --weather,location=us-midwest temperature=82i 1465839830100400200这里的82会被看作是integer类型
  25. --weather,location=us-midwest temperature="too warm" 1465839830100400200这里的too warm会被看作是string类型
  26. --weather,location=us-midwest too_hot=true 1465839830100400200,这里的true就是boolean类型,表示true的可以是t,T, true, True, TRUE,表示false的可以是f,F, false, False, FALSE
  27. 在同一个分片shard中存储不同类型的field值会报错:
  28. --INSERT weather,location=us-midwest temperature=82 1465839830100400200
  29. --INSERT weather,location=us-midwest temperature=82i 1465839830100400300
  30. ERR:{"error":"field type conflict:input field\"temperature\" on measuremetn \"weather\" is type int64}
  31. 但是在不同的分片Shard中存储不同类型的field值不会报错:
  32. --INSERT weather,location=us-midwest temperature=82 1465839830100400200
  33. --INSERT weather,location=us-midwest temperature=82i 1465839830100400300
  34. ```
  35. **引号**
  36. ```
  37. 不要在时间戳上加双引号:
  38. --INSERT weather,location=us-midwest temperature=82 "1465839830100400200"
  39. ERR: {"error":"unable to parse 'weather,location=us-midwest temperature=82 \"1465839830100400200\"': bad timestamp"}
  40. 不要在字段field值上加单引号:
  41. --INSERT weather,location=us-midwest temperature='too warm'
  42. ERR: {"error":"unable to parse 'weather,location=us-midwest temperature='too warm'': invalid boolean"}
  43. 不要在tag的key,value,field的key上加单引号或双引号,这样虽然不会报错,但InfluxDB会把引号看作是measruements的一部分:
  44. --INSERT "weather",location=us-midwest temperature=87 1465839830100400200
  45. --SHOW MEASURMENTS
  46. --会列出"weather"
  47. --这样查询起来会麻烦:SELECT * FROM "\"weather\""
  48. 不要在filed值上加双引号,InfluxDB会看作是字符串类型:
  49. --INSERT weather,location=us-midwest temperatrue="82"
  50. ```
  51. **特殊字符Special Characters**
  52. ```
  53. ,通过\转义:
  54. weather,location=us\,midwest temperature=82 1465839830100400200
  55. =通过\转义:
  56. weather,location=us-midwest temp\=rature=82 1465839830100400200
  57. 空格通过\转义:
  58. weather,location\ place=us-midwest temperature=82 1465839830100400200
  59. measurement中的,通过\转义:
  60. wea\,ther,lication=us-midwest temperature=82 1465839830100400200
  61. measurement中的空格通过\转义:
  62. wea\ ther,location=us-midwest temperature=82 1465839830100400200
  63. 字段filed值中的双引号用\转义:
  64. weather,location=us-midwest temperature="too\"hot\"" 1465839830100400200
  65. /或\的表现:
  66. --weather,location=us-midwest temperature_str="too hot/cold" 1465839830100400201
  67. --weather,location=us-midwest temperature_str="too hot\cold" 1465839830100400202
  68. --weather,location=us-midwest temperature_str="too hot\\cold" 1465839830100400203
  69. --weather,location=us-midwest temperature_str="too hot\\\cold" 1465839830100400204
  70. --weather,location=us-midwest temperature_str="too hot\\\\\cold" 1465839830100400205
  71. --weather,location=us-midwest temperature_str="too hot\\\\\cold" 1465839830100400206
  72. > SELECT * FROM "wather"
  73. name:weather
  74. time location temperature_str
  75. 1465839830100400201 us-midwest too hot/cold
  76. 1465839830100400202 us-midwest too hot\cold
  77. 1465839830100400203 us-midwest too hot\cold 两个会去掉一个
  78. 1465839830100400204 us-midwest too hot\\cold 三个去掉一个
  79. 1465839830100400205 us-midwest too hot\\cold 四个去掉两个
  80. 1465839830100400206 us-midwest too hot\\\cold 5个去掉两个
  81. ```
  82. **关键字Keywords**
  83. ```
  84. time可以是database, measurement, retension plocy, subscription, user的名称,time不能作为tag或field的key
  85. ```
  86. **聚合aggregation**
  87. InfluxQL函数,对一组数据进行计算。
  88. ```
  89. ==COUNT()
  90. > SELECT COUNT("water_level") FROM "h2o_feet"
  91. 返回h2o_feet"这个measurement中water_level这个字段field值不为空的数量
  92. > SELECT COUNT(*) FROM "h2o_feet"
  93. 返回h2o_feet"这个measurement中所有字段字段field值不为空的数量
  94. > SELECT COUNT(/water/) FROM "h2o_feet"
  95. 返回h2o_feet"这个measurement中字段包含water并且值不为空的数量
  96. > SELECT COUNT("water_level") FROM "h2o_feet" WHERE time >= '2015-08-17T23:48:00Z' AND time <= '2015-08-18T00:54:00Z' GROUP BY time(12m),* fill(200) LIMIT 7 SLIMIT 1
  97. 时间范围,12分钟的时间间隔进行分组,没有值的用200填充,数据点个数最多为7,序列个数最多为1
  98. ```
  99. # InfluxQL-基本
  100. **连接和退出数据库**
  101. ```
  102. $ .\influx -precision rfc3339
  103. Connected to http://localhost:8086 version1.7.7
  104. InfluxDB shell version:1.7.1
  105. rfc3339的时间戳格式是:YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ
  106. $ exit
  107. ```
  108. **创建数据库**
  109. - 运行`influxd.exe`文件
  110. - 启动influx: `./influx -precision rfc3339`
  111. - 创建数据库
  112. ```
  113. $ CREATE DATABASE NOAA_water_database
  114. ```
  115. **下载测试数据并写入本地数据库**
  116. ```
  117. 下载数据:
  118. $ curl https://s3.amazonaws.com/noaa.water-database/NOAA_data.txt -o NOAA_data.txt
  119. 这样在目录中多了一个NOAA_data.txt文件
  120. 导入本地数据库:
  121. $ ./influx -import -path=NOAA_data.txt -precision=s -database=NOAA_water_database
  122. 这时会报错:unknown arguments: .txt -precision=s
  123. 在`influx.exe`文件所在目录,把`NOAA_data.txt`改成`NOAA_data`
  124. $ ./influx -import -path=NOAA_data -precision=s -database=NOAA_water_database
  125. 连接数据库:
  126. $ ./influx -precsion rfc3339 -database NOAA_water_database
  127. 查询所有的表,即measument:
  128. $ SHOW measurements
  129. ```
  130. # InfluxQL-Data exploration
  131. > 查询
  132. **统计某个非空值字段的数量**
  133. ```
  134. SELECT COUNT("water_level") FROM h2o_feet
  135. ```
  136. **选择前几个**
  137. ```
  138. SELECT * FROM h2o_feet LIMIT 5
  139. ```
  140. **查询所有fields和tags**
  141. ```
  142. SELECT * FROM "h2o_feet"
  143. ```
  144. **选择特定的tag和field**
  145. ```
  146. $ ./influx -precsion rfc3339
  147. $ USE NOAA_water_database
  148. $ SELECT "level description","location","water_level" FROM "h2o_feet"
  149. ```
  150. **选择tag和field,用类型区分**
  151. ```
  152. SELECT "level description"::field,"location"::tag,"water_level"::field FROM "h2o_feet"
  153. ```
  154. **选择所有的field**
  155. ```
  156. SELECT *::field FROM "h2o_feet"
  157. ```
  158. **field简单计算**
  159. ```
  160. SELECT ("water_level" * 2) + 4 from "h2o_feet"
  161. ```
  162. **从多个measurements中查询数据**
  163. ```
  164. select * from "h2o_feet","h2o_PH"
  165. ```
  166. **从多个measurements中查询数据,用上数据库名**
  167. ```
  168. select * from "NOAA_water_database"."autogen"."h2o_feet"
  169. ```
  170. **查询某个数据库中某个measuremnt的所有数据**
  171. ```
  172. select * from "NOAA_water_database".."h2o_feet"
  173. ```
  174. **查询与tag相关的数据必须至少带一个field**
  175. ```
  176. select "water_level","location" from "h2o_feet"
  177. ```
  178. > 过滤
  179. **Where语句语法**
  180. ```
  181. field支持的操作符:
  182. field_key <operator> ['string' | boolean | float | integer]
  183. = <> != > >= < <=
  184. tag支持的操作符:
  185. tag_key <operator> ['tag_value']
  186. = <> !=
  187. ```
  188. **根据字段值筛选**
  189. ```
  190. select * from "h2o_feet" where "water_level">8
  191. ```
  192. **根据某个字段的字符串值筛选**
  193. ```
  194. select * from "h2o_feet" where "level description" = 'below 3 feet'
  195. ```
  196. **根据某个计算筛选**
  197. ```
  198. select * from "h2o_feet" where "water_level" + 2 > 11.9
  199. ```
  200. **根据某个tag值筛选**
  201. ```
  202. select "water_level" from "h2o_feet" wehre "location" = 'santa_monica'
  203. ```
  204. **根据tag和field筛选**
  205. ```
  206. select "water_level" from "h2o_feet" where "location" <> 'santa_monica' adn (water_level < -0.59 OR water_level > 9.95)
  207. ```
  208. **根据timestamp筛选**
  209. ```
  210. select * from h2o_feet wehre time > now() -7d
  211. ```
  212. > 分组
  213. **根据tag分组**
  214. ```
  215. select MEAN(water_level) from h2o_feet group by location
  216. 根据location分组后,取每个分组中water_level字段的平均值
  217. ```
  218. **根据多个tag分组**
  219. ```
  220. select MEAN(index) from h2o_feet group by lcoation,randtag
  221. ```
  222. **根据所有tag分组**
  223. ```
  224. select MEAN(index) from h2o_feet group by *
  225. ```
  226. **根据时间间隔分组**
  227. ```
  228. SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m)
  229. ```
  230. **根据时间间隔和tag分组**
  231. ```
  232. SELECT COUNT("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m),"location"
  233. ```
  234. **根据时间间隔分组并移前**
  235. ```
  236. SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time <= '2015-08-18T00:54:00Z' GROUP BY time(18m,6m)
  237. ```
  238. **groupby和fill的结合**
  239. ```
  240. > SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m) fill(100)
  241. ```
  242. > INTO
  243. **在原来数据库基础上复制出一个新的数据库**
  244. 重命名一个数据库是不可能的,只能在原来数据库基础上创建一个新的数据库,用INTO语法。
  245. ```
  246. SELECT * INTO "copy_NOAA_water_database"."autogen".:MEASUREMENT FROM "NOAA_water_database"."autogen"./.*/ GROUP BY *
  247. :MEASUREMENT表示原先数据库的measuments都复制到新的数据库。
  248. autogen是数据保留策略,原先数据库和新的数据库都必须有,否则INTO语句无法执行。
  249. GROUP BY *很关键,意思是把NOAA_water_database数据库中所有measuments下的所有tag也复制到copy_NOAA_water_database数据库。如果不这样写,原先数据库中measuments下的tag会变成copy_NOAA_water_database下的字段。
  250. 具体步骤:
  251. --创建新的数据库:create database copy_NOAA_water_database
  252. --进入源数据库:use NOAA_water_database
  253. --使用INTO语句复制数据: SELECT * INTO "copy_NOAA_water_database"."autogen".:MEASUREMENT FROM "NOAA_water_database"."autogen"./.*/ GROUP BY *
  254. --进入新数据库:use copy_NOAA_water_database
  255. --查询新数据库的所有measurments:show measurements
  256. --查询新数据库是否有数据:select * from h2o_feet LIMIT 5
  257. ```
  258. **如果数据量很大,建议按measuement和时间范围,循序渐进地复制**
  259. ```
  260. SELECT *
  261. INTO <destination_database>.<retention_policy_name>.<measurement_name>
  262. FROM <source_database>.<retention_policy_name>.<measurement_name>
  263. WHERE time > now() - 100w and time < now() - 90w GROUP BY *
  264. SELECT *
  265. INTO <destination_database>.<retention_policy_name>.<measurement_name>
  266. FROM <source_database>.<retention_policy_name>.<measurement_name>}
  267. WHERE time > now() - 90w and time < now() - 80w GROUP BY *
  268. SELECT *
  269. INTO <destination_database>.<retention_policy_name>.<measurement_name>
  270. FROM <source_database>.<retention_policy_name>.<measurement_name>
  271. WHERE time > now() - 80w and time < now() - 70w GROUP BY *
  272. ```
  273. **把查询结果复制到measument中去**
  274. ```
  275. SELECT "water_level" INTO "h2o_feet_copy_1" FROM "h2o_feet" WHERE "location" = 'coyote_creek'
  276. ```
  277. > 排序
  278. ```
  279. 根据时间降序:
  280. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' ORDER BY time DESC
  281. 分组排序:
  282. SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY time(12m) ORDER BY time DESC
  283. ```
  284. >LIMIT和SLIMIT
  285. ```
  286. 限制point返回数量:
  287. SELECT "water_level","location" FROM "h2o_feet" LIMIT 3
  288. 限制series返回数量:
  289. SELECT "water_level" FROM "h2o_feet" GROUP BY * SLIMIT 1
  290. ```
  291. > OFFSET SOFFSET
  292. ```
  293. 显示point的第4,5,6条数据
  294. SELECT "water_level","location" FROM "h2o_feet" LIMIT 3 OFFSET 3
  295. 显示point的第1,2,3条数据
  296. SELECT "water_level","location" FROM "h2o_feet" LIMIT 3
  297. SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY *,time(12m) ORDER BY time DESC LIMIT 2 OFFSET 2 SLIMIT 1
  298. 显示serie的第2条数据
  299. SELECT "water_level" FROM "h2o_feet" GROUP BY * SLIMIT 1 SOFFSET 1
  300. ```
  301. > Time Zone
  302. ```
  303. 选择时区基准
  304. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:18:00Z' tz('America/Chicago')
  305. SELECT语句即使没有选择时间范围,也有默认时间范围:
  306. 1677-09-21 00:12:43.145224194 and 2262-04-11T23:47:16.854775806Z
  307. GROUP BY time()的时间范围是从过去到现在:
  308. 1677-09-21 00:12:43.145224194到现在
  309. 使用RFC3339的时间类型字符串:
  310. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00.000000000Z' AND time <= '2015-08-18T00:12:00Z'
  311. 使用RFC3339-like的时间类型字符串:
  312. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18' AND time <= '2015-08-18 00:12:00'
  313. 使用epoch时间戳:
  314. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= 1439856000000000000 AND time <= 1439856720000000000
  315. 使用second-precision epoch时间戳:
  316. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= 1439856000s AND time <= 1439856720s
  317. 在RFC3339-like的时间类型字符串上运行计算:
  318. SELECT "water_level" FROM "h2o_feet" WHERE time > '2015-09-18T21:24:00Z' + 6m
  319. 在epoch时间戳上运行计算:
  320. SELECT "water_level" FROM "h2o_feet" WHERE time > 24043524m - 6m
  321. ```
  322. > 相对时间
  323. ```
  324. 仅仅相对时间:
  325. SELECT "water_level" FROM "h2o_feet" WHERE time > now() - 1h
  326. 相对时间和绝对时间结合:
  327. SELECT "level description" FROM "h2o_feet" WHERE time > '2015-09-18T21:18:00Z' AND time < now() + 1000d
  328. ```
  329. > 正则表达式
  330. ```
  331. 选择tag或field中包含1:
  332. SELECT /l/ FROM "h2o_feet" LIMIT 1
  333. 选择所有包含temperature的measurment中的degrees的平均值
  334. SELECT MEAN("degrees") FROM /temperature/
  335. location这个tag包含m, water_level这个field大于3:
  336. SELECT MEAN(water_level) FROM "h2o_feet" WHERE "location" =~ /[m]/ AND "water_level" > 3
  337. location这个tag没有值:
  338. SELECT * FROM "h2o_feet" WHERE "location" !~ /./
  339. location这个tag有值:
  340. SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" =~ /./
  341. level description这个字段的值包含between
  342. SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND "level description" =~ /between/
  343. 分组时使用正则表达式:
  344. SELECT FIRST("index") FROM "h2o_quality" GROUP BY /l/
  345. ```
  346. > 数据类型
  347. ```
  348. 返回water_level这个字段的类型是float:
  349. SELECT "water_level"::float FROM "h2o_feet" LIMIT 4
  350. ```
  351. > 数据类型转换
  352. ```
  353. 把water_level的float类型的值转换成integer:
  354. SELECT "water_level"::integer FROM "h2o_feet" LIMIT 4
  355. 把water_level的float类型的值转换成string(不支持):
  356. SELECT "water_level"::string FROM "h2o_feet" LIMIT 4
  357. ```
  358. > 合并行为
  359. ```
  360. 默认把两个serie自动合并:
  361. SELECT MEAN("water_level") FROM "h2o_feet"
  362. 避免自动合并:
  363. SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'coyote_creek'
  364. 分别得到两个serie的数据:
  365. SELECT MEAN("water_level") FROM "h2o_feet" GROUP BY "location"
  366. ```
  367. > 多条语句
  368. ```
  369. SELECT MEAN("water_level") FROM "h2o_feet"; SELECT "water_level" FROM "h2o_feet" LIMIT 2
  370. ```
  371. > 子语句
  372. ```
  373. SELECT SUM("max") FROM (SELECT MAX("water_level") FROM "h2o_feet" GROUP BY "location")
  374. ```
  375. # InfluxQL-Schema exploration
  376. ```
  377. 展示所有数据库:
  378. SHOW DATABASES
  379. 展示数据库的数据保留策略:
  380. SHOW RETENTION POLICIES ON NOAA_water_database
  381. 展示某个数据库的所有时间序列:
  382. SHOW SERIES ON NOAA_water_database
  383. 展示某个数据库某个表符合条件的时间序列:
  384. SHOW SERIES ON NOAA_water_database FROM "h2o_quality" WHERE "location" = 'coyote_creek' LIMIT 2
  385. 展示某个数据库的所有表:
  386. SHOW MEASUREMENTS ON NOAA_water_database
  387. 展示某个数据库某个以h2o开头的表,randtag这个tag的值包含整型:
  388. SHOW MEASUREMENTS ON NOAA_water_database WITH MEASUREMENT =~ /h2o.*/ WHERE "randtag" =~ /\d/
  389. 展示某个数据库的所有tag的key:
  390. SHOW TAG KEYS ON "NOAA_water_database"
  391. 展示TAG的值:
  392. SHOW TAG VALUES ON "NOAA_water_database" WITH KEY = "randtag"
  393. 展示数据库字段的key:
  394. SHOW FIELD KEYS ON "NOAA_water_database"
  395. ```
  396. # InfluxQL-Data management
  397. ```
  398. 创建数据库使用默认配置:
  399. CREATE DATABASE "NOAA_water_database"
  400. 创建数据库自定义配置:
  401. CREATE DATABASE "NOAA_water_database" WITH DURATION 3d REPLICATION 1 SHARD DURATION 1H NAME "liquid"
  402. 删除数据库:
  403. DROP DATABASE "NOAA_water_database"
  404. 删除表中的时间序列:
  405. DROP SERIES FROM "h2o_feet"
  406. 根据tag值删除时间序列:
  407. DROP SERIES FROM "h2o_feet" WHERE "location" = 'santa_monica'
  408. 删除所有表记录:
  409. DELETE FROM "h2o_feet"
  410. 带条件的删除:
  411. DELETE FROM "h2o_quality" WHERE "randtag" = '3'
  412. DELETE WHERE "h2o_quality" WHERE time < '2016-01-01'
  413. 删除表:
  414. DROP MEASUREMENT "h2o_feet"
  415. 删除shard:
  416. DROOP SHARD 1
  417. 数据保留策略:DURATION最小1个小时,最大INF表示无穷;REPLICATION,决定了每个point在集群中有几份,默认是3份,为了确保数据及时响应给请求,这里的值最好小于等于集群中的数据节点。在单结点实例中REPLICATION的设置无效;SHARD DURATION设置Shard Group的时间范围,这里的值没有无线INF一说。默认情况下SHARD DURATION的值受RETENTION POLICY影响。SHARD DURATION的默认值是1小时。
  418. --CREATE RETENTION POLICY "one_day_only" ON "NOAA_water_database" DURATION 1d REPLICATION 1
  419. --把新的策略设置成默认策略:CREATE RETENTION POLICY "one_day_only" ON "NOAA_water_database" DURATION 23h60m REPLICATION 1 DEFAULT
  420. 创建并修改策略:
  421. --创建策略:CREATE RETENTION PPLICY "what_is_time" ON "NOAA_water_database" DURATION 2d REPLICATION 1
  422. --修改策略:ALTER RETENTION POLICY "what_is_time" ON "NOAA_water_database" DURAITON 3w SHARD DURATION 2H DEFAULT
  423. 删除策略:
  424. DROP RETENTION POLICY "what_is_time" ON "NOAA_water_database"
  425. ```
  426. # InfluxQL-Continuous Queries
  427. 自动或间隔运行并且保存在measurement中。
  428. **自动统计数据**:
  429. ```
  430. CREATE CONTINUOUS QUERY "cq_basic" ON "transporation"
  431. BEGIN
  432. SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h)
  433. END
  434. cq_basic是自动运行的query的名称,每小时从bus_data这个measurment中统计出来的数据保存到trasporation数据库中的average_passengers这个measurement中。
  435. select * from "average_passengers"
  436. ```
  437. **自动统计数据,并保存到不同的RETENTION POLICY上**:
  438. ```
  439. CREATE CONTINUOUS QUERY "cq_basic_rp" ON "transporation"
  440. BEGIN
  441. SELECT mean("passengers") INTO "transporation"."three_weeks"."average_passengers" FROM "bus_data" GROUP BY time(1h)
  442. SELECT * FROM "transporation"."three_weeks"."average_passengers"
  443. ```
  444. **自动统计数据,保存到不同的数据库**:
  445. ```
  446. CREATE CONTINUOUS QUERY "cq_basic_br" ON "transporation"
  447. BEGIN
  448. SELECT mean(*) INTO "downsampled_trasporation"."autogen".:MEASUREMENT FROM /.*/ GROUP BY time(30m)
  449. END
  450. ```
  451. **自动统计数据,延迟保存到另外的表**:
  452. ```
  453. CREATE CONTINUOUS QUERY "cq_basic_offset" ON "transporation"
  454. BEGIN
  455. SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h,15m)
  456. ```
  457. 自动统计数据,每隔1小时统计一次,然后每30分钟统计一次,即半点的时候统计一次,最终半点的数据会被下一个整点的数据替换掉。
  458. ```
  459. CREATE CONTINUOUS QUERY "cq_advanced_every" ON "transportation"
  460. RESAMPLE EVERY 30m
  461. BEGIN
  462. SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h)
  463. END
  464. ```
  465. 自动统计数据,每30分钟统计一次数据,统计前1个小时的数据。
  466. ```
  467. CREATE CONTINUOUS QUERY "cq_advanced_for" ON "transportation"
  468. RESAMPLE FOR 1h
  469. BEGIN
  470. SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(30m)
  471. END
  472. ```
  473. 自动统计for和every结合起来:
  474. ```
  475. CREATE CONTINUOUS QUERY "cq_advanced_every_for" ON "transportation"
  476. RESAMPLE EVERY 1h FOR 90m
  477. BEGIN
  478. SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(30m)
  479. END
  480. ```
  481. 自动统计,填上空值
  482. ```
  483. CREATE CONTINUOUS QUERY "cq_advanced_for_fill" ON "transportation"
  484. RESAMPLE FOR 2h
  485. BEGIN
  486. SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h) fill(1000)
  487. END
  488. ```
  489. 展示所有Continuous Query
  490. ```
  491. SHOW CONTINUOUS QUERIES
  492. ```
  493. 删除Continius Query
  494. ```
  495. DROP CONTINOUS QUERY "idle_hands" ON ""
  496. ```
  497. # InfluxQL-Functions
  498. - COUNT()
  499. - DISTNCT()
  500. - INTEGRAL()
  501. - MEAN()
  502. - MEDIAN()排好序的中位数
  503. - MODE()字段值中出现频率最高的值
  504. - SPREAD()字段值最大最小之差
  505. - STDDEV()字段值标准差
  506. - SUM()
  507. - BOTTOM()
  508. - FIRST()
  509. - LAST()
  510. - MAX()
  511. - MIN()
  512. - PERCENTILE()字段值某个百分位上的值
  513. - SAMPLE()随机样本
  514. - TOP()
  515. - ABS()
  516. - ACOS()
  517. - ASIN()
  518. - ATAN()
  519. - ATAN2()
  520. - CEL()
  521. - COS()
  522. - CUMULATIVE_SUM()
  523. - DERIVATIVE()变化率
  524. - DIFFERENCE()差值
  525. - ELAPSED()时间戳差值
  526. - EXP()指数
  527. - FLOOR()
  528. - LN()自然对数
  529. - LOG()
  530. - LOG2()
  531. - LOG10()
  532. - MOVING_AVERAGE()滚动窗口的平均值
  533. - NON_NEGATIVE_DERIVATIVE()非负变换率
  534. - NON_NEGATIVE_DIFFERENCE()非负差值
  535. - POW()
  536. - ROUND()
  537. - SIN()
  538. - SQRT()
  539. - TAN()
  540. # InfluxQL-Mathematical operations
  541. ```
  542. 加法:
  543. SELECT "A" + 5 FROM "add"
  544. 减法:
  545. SELECT "A" - "B" from ""
  546. 乘法:
  547. SELECT "A" * "B" * "C" from ""
  548. 除法:
  549. SELECT 10 / "A" FROM ""
  550. 取余:
  551. SELECT "B" % 2 FROM ""
  552. ```