Przeglądaj źródła

InfluxDB基本使用

master
qdjjx 5 lat temu
rodzic
commit
e7b72aea05
1 zmienionych plików z 746 dodań i 0 usunięć
  1. 746
    0
      实践/后端/项目/11.InfluxDB的基本使用.md

+ 746
- 0
实践/后端/项目/11.InfluxDB的基本使用.md Wyświetl plik

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

Ładowanie…
Anuluj
Zapisz