Browse Source

阿里时序数据库操作

master
qdjjx 5 years ago
parent
commit
ae0966450f
1 changed files with 216 additions and 0 deletions
  1. 216
    0
      实践/后端/项目/12.阿里云InfluxDB使用.md

+ 216
- 0
实践/后端/项目/12.阿里云InfluxDB使用.md View File

@@ -0,0 +1,216 @@
1
+> 连接数据库
2
+
3
+普通连接数据库:
4
+```
5
+./influx -ssl -username influxuser
6
+ -password user@influxDB -host ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com -port 3242
7
+```
8
+
9
+以json展示:
10
+```
11
+./influx -ssl -username influxuser -password user@influxDB -host ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com -port 3242 -format=json
12
+```
13
+
14
+json以美观的方式展示
15
+```
16
+./influx -ssl -username influxuser -password user@influxDB -host ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com -port 3242 -format=json -pretty 
17
+```
18
+
19
+> 使用数据库
20
+
21
+```
22
+use dingding_influx
23
+show measurements
24
+```
25
+
26
+> 新建measurement
27
+
28
+```
29
+创建名称是cpu的measurement:
30
+INSERT cpu,host=serverA,region=us_west value=0.64
31
+select host, region, value from cpu
32
+
33
+创建名称是temperature的measurement:
34
+INSERT temperature,machine=unit42,type=assembly external=25,internal=37
35
+select * from temperature
36
+```
37
+
38
+> 删除measurement
39
+
40
+删除measurement需要管理员权限
41
+```
42
+drop measurement cpu
43
+```
44
+
45
+> 通过Postman请求数据
46
+
47
+请求
48
+```
49
+POST https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/query?u=influxuser&p=user@influxDB
50
+Headers: Content-Type application/x-www-form-urlencoded
51
+Body:
52
+    db=dingding_influx
53
+    q=select host, region, value from cpu
54
+```
55
+
56
+请求成功返回
57
+```
58
+{
59
+    "results": [
60
+        {
61
+            "statement_id": 0,
62
+            "series": [
63
+                {
64
+                    "name": "cpu",
65
+                    "columns": [
66
+                        "time",
67
+                        "host",
68
+                        "region",
69
+                        "value"
70
+                    ],
71
+                    "values": [
72
+                        [
73
+                            "2019-07-26T08:58:21.438818256Z",
74
+                            "serverA",
75
+                            "us_west",
76
+                            0.64
77
+                        ]
78
+                    ]
79
+                }
80
+            ]
81
+        }
82
+    ]
83
+}
84
+```
85
+
86
+如果请求没有的measurement返回
87
+```
88
+{
89
+    "results": [
90
+        {
91
+            "statement_id": 0
92
+        }
93
+    ]
94
+}
95
+```
96
+
97
+如果用户名错误、密码错误、用户名和密码同时错误返回
98
+```
99
+{
100
+    "error": "authorization failed"
101
+}
102
+```
103
+
104
+如果数据库错误
105
+```
106
+{
107
+    "error": "error authorizing query: influxuser not authorized to execute statement 'SELECT host, region, value FROM cpu', requires READ on dingding_influ"
108
+}
109
+```
110
+
111
+如果请求meaasurement中没有的字段,比如`select hos, region, value from cpu`返回
112
+```
113
+{
114
+    "results": [
115
+        {
116
+            "statement_id": 0,
117
+            "series": [
118
+                {
119
+                    "name": "cpu",
120
+                    "columns": [
121
+                        "time",
122
+                        "hos",
123
+                        "region",
124
+                        "value"
125
+                    ],
126
+                    "values": [
127
+                        [
128
+                            "2019-07-26T08:58:21.438818256Z",
129
+                            null,
130
+                            "us_west",
131
+                            0.64
132
+                        ]
133
+                    ]
134
+                }
135
+            ]
136
+        }
137
+    ]
138
+}
139
+```
140
+
141
+如果q值为空返回
142
+```
143
+{
144
+    "error": "missing required parameter \"q\""
145
+}
146
+```
147
+
148
+**时间戳格式**
149
+
150
+默认数据的时间格式是UTC,编码是RFC3339, 例如`2015-08-04T19:05:14.318570484Z`, 如果想反悔Unix格式,使用`epoch`字段。
151
+
152
+```
153
+epoch=h,m,s,ms,u,ns
154
+```
155
+
156
+**分块Chunking**
157
+
158
+默认分块的数量是10000个数据据点,如果想改变分块大小,通过`chunked`和`chunk_size`设置。
159
+```
160
+chunked=true
161
+chunk_size=20000
162
+```
163
+
164
+
165
+> Postman写入数据
166
+
167
+内部是以二进制方式写入数据。阿里云时序数据库API默认的写入时间是在5秒之内,如果一次性写入过多的数据,写入时间超过5秒,就不保证所有数据都被写入。所以当数据点比较多,比如有5000个数据点,需要把数据拆分。
168
+
169
+单点写入数据点
170
+```
171
+POST https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/write?db=dingding_influx&u=influxuser&p=user@influxDB
172
+Body: cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000
173
+```
174
+
175
+返回类型是204(No Content)。
176
+
177
+多点写入数据点(没有成功)
178
+```
179
+POST https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/write?db=dingding_influx&u=influxuser&p=user@influxDB
180
+Body: 
181
+    cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
182
+    cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'
183
+```
184
+
185
+> 故障排除
186
+
187
+使用浏览器打开。
188
+```
189
+总览页:
190
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/pprof?u=influxuser&p=user@influxDB
191
+
192
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/pprof/goroutine?u=influxuser&p=user@influxDB
193
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/pprof/heap?u=influxuser&p=user@influxDB
194
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/pprof/mutex?u=influxuser&p=user@influxDB
195
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/pprof/threadcreate?u=influxuser&p=user@influxDB
196
+```
197
+
198
+
199
+> 跟踪请求
200
+
201
+```
202
+默认跟踪10秒内的请求:
203
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/requests?u=influxuser&p=user@influxDB
204
+
205
+跟踪1分钟内的请求:
206
+https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/debug/requests?seconds=60&u=influxuser&p=user@influxDB
207
+```
208
+
209
+> 运行时的统计信息
210
+
211
+使用/ping检查TSDB For InfluxDB®实例的状态和TSDB For InfluxDB®的版本。使用Postman请求。
212
+```
213
+GET https://ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com:3242/ping?u=influxuser&p=user@influxDB
214
+返回204 在Headers里面有版本信息
215
+```
216
+

Loading…
Cancel
Save