02-rest-api.mdx 10.4 KB
Newer Older
D
dingbo 已提交
1 2 3 4
---
title: REST API
---

5
To support the development of various types of platforms, TDengine provides an API that conforms to the REST principle, namely REST API. To minimize the learning cost, different from the other database REST APIs, TDengine directly requests the SQL command contained in the request BODY through HTTP POST to operate the database and only requires a URL.
D
dingbo 已提交
6

7
Note: One difference from the native connector is that the REST interface is stateless, so the `USE db_name` command has no effect. All references to table names and super table names need to specify the database name prefix. (Since version 2.2.0.0, it is supported to specify db_name in RESTful URL. If the database name prefix is not specified in the SQL command, the `db_name` specified in the URL will be used. Since version 2.4.0.0, REST service is provided by taosAdapter by default. And it requires that the `db_name` must be specified in the URL.)
D
dingbo 已提交
8

9
## Installation
D
dingbo 已提交
10

11
The REST interface does not rely on any TDengine native library, so the client application does not need to install any TDengine libraries. The client application's development language supports the HTTP protocol is enough.
D
dingbo 已提交
12

13
## Verification
D
dingbo 已提交
14

15
If the TDengine server is already installed, it can be verified as follows:
D
dingbo 已提交
16

17
The following is an Ubuntu environment using the `curl` tool (to confirm that it is installed) to verify that the REST interface is working.
D
dingbo 已提交
18

19
The following example lists all databases, replacing `h1.taosdata.com` and `6041` (the default port) with the actual running TDengine service FQDN and port number.
D
dingbo 已提交
20 21 22 23 24 25

```html
curl -H 'Authorization: Basic cm9vdDp0YW9zZGF0YQ==' -d 'show databases;'
h1.taosdata.com:6041/rest/sql
```

26
The following return value results indicate that the verification passed.
D
dingbo 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

```json
{
  "status": "succ",
  "head": [
    "name",
    "created_time",
    "ntables",
    "vgroups",
    "replica",
    "quorum",
    "days",
    "keep1,keep2,keep(D)",
    "cache(MB)",
    "blocks",
    "minrows",
    "maxrows",
    "wallevel",
    "fsync",
    "comp",
    "precision",
    "status"
  ],
  "data": [
    [
      "log",
      "2020-09-02 17:23:00.039",
      4,
      1,
      1,
      1,
      10,
      "30,30,30",
      1,
      3,
      100,
      4096,
      1,
      3000,
      2,
      "us",
      "ready"
    ]
  ],
  "rows": 1
}
```

75
## HTTP request URL format
D
dingbo 已提交
76 77 78 79 80

```
http://<fqdn>:<port>/rest/sql/[db_name]
```

81
Parameter Description:
D
dingbo 已提交
82

83 84
- fqnd: FQDN or IP address of any host in the cluster
- port: httpPort configuration item in the configuration file, default is 6041
85
- db_name: Optional parameter that specifies the default database name for the executed SQL command. (supported since version 2.2.0.0)
D
dingbo 已提交
86

87
For example, `http://h1.taos.com:6041/rest/sql/test` is a URL to `h1.taos.com:6041` and sets the default database name to `test`.
D
dingbo 已提交
88

89
TDengine supports both Basic authentication and custom authentication mechanisms, and subsequent versions will provide a standard secure digital signature mechanism for authentication.
D
dingbo 已提交
90

91
- The custom authentication information is as follows (Let's introduce token later)
D
dingbo 已提交
92 93 94 95 96

  ```
  Authorization: Taosd <TOKEN>
  ```

97
- Basic authentication information is shown below
D
dingbo 已提交
98 99 100 101 102

  ```
  Authorization: Basic <TOKEN>
  ```

103
The HTTP request's BODY is a complete SQL command, and the data table in the SQL statement should be provided with a database prefix, e.g., `\<db_name>.\<tb_name>`. If the table name does not have a database prefix and the database name is not specified in the URL, the system will response an error because the HTTP module is a simple forwarder and has no awareness of the current DB.
D
dingbo 已提交
104

105
Use `curl` to initiate an HTTP request with a custom authentication method, with the following syntax.
D
dingbo 已提交
106 107 108 109 110

```bash
curl -H 'Authorization: Basic <TOKEN>' -d '<SQL>' <ip>:<PORT>/rest/sql/[db_name]
```

111
Or
D
dingbo 已提交
112 113 114 115 116

```bash
curl -u username:password -d '<SQL>' <ip>:<PORT>/rest/sql/[db_name]
```

117
where `TOKEN` is the string after Base64 encoding of `{username}:{password}`, e.g. `root:taosdata` is encoded as `cm9vdDp0YW9zZGF0YQ==`.
D
dingbo 已提交
118

119
## HTTP Return Format
D
dingbo 已提交
120

121
The return result is in JSON format, as follows:
D
dingbo 已提交
122 123 124 125

```json
{
    "status": "succ",
126 127
    "head": ["ts", "current", ...],
    "column_meta": [["ts",9,8],["current",6,4], ...],
D
dingbo 已提交
128
    "data": [
129 130
        ["2018-10-03 14:38:05.000", 10.3, ...],
        ["2018-10-03 14:38:15.000", 12.6, ...]
D
dingbo 已提交
131 132 133 134 135
    ],
    "rows": 2
}
```

136
Description:
D
dingbo 已提交
137

138
- status: tell if the operation result is success or failure.
139
- head: the definition of the table, or just one column "affected_rows" if no result set is returned. (As of version 2.0.17.0, it is recommended not to rely on the head return value to determine the data column type but rather use column_meta. In later versions, the head item may be removed from the return value.)
140 141 142
- column_meta: this item is added to the return value to indicate the data type of each column in the data with version 2.0.17.0 and later versions. Each column is described by three values: column name, column type, and type length. For example, `["current",6,4]` means that the column name is "current", the column type is 6, which is the float type, and the type length is 4, which is the float type with 4 bytes. If the column type is binary or nchar, the type length indicates the maximum length of content stored in the column, not the length of the specific data in this return value. When the column type is nchar, the type length indicates the number of Unicode characters that can be saved, not bytes.
- data: The exact data returned, presented row by row, or just [[affected_rows]] if no result set is returned. The order of the data columns in each row of data is the same as that of the data columns described in column_meta.
- rows: Indicates how many rows of data there are.
D
dingbo 已提交
143

144
The column types in column_meta are described as follows:
D
dingbo 已提交
145

146 147 148 149 150 151 152 153 154 155
- 1:BOOL
- 2:TINYINT
- 3:SMALLINT
- 4:INT
- 5:BIGINT
- 6:FLOAT
- 7:DOUBLE
- 8:BINARY
- 9:TIMESTAMP
- 10:NCHAR
D
dingbo 已提交
156

157
## Custom Authorization Code
D
dingbo 已提交
158

159
HTTP requests require an authorization code `<TOKEN>` for identification purposes. The administrator usually provides the authorization code, and it can be obtained simply by sending an ``HTTP GET`` request as follows:
D
dingbo 已提交
160 161 162 163 164

```bash
curl http://<fqnd>:<port>/rest/login/<username>/<password>
```

165
Where `fqdn` is the FQDN or IP address of the TDengine database. `port` is the port number of the TDengine service. `username` is the database username. `password` is the database password. The return value is in `JSON` format, and the meaning of each field is as follows.
D
dingbo 已提交
166

167
- status: flag bit of the request result
D
dingbo 已提交
168

169
- code: return value code
D
dingbo 已提交
170

171
- desc: authorization code
D
dingbo 已提交
172

173
Example of getting authorization code.
D
dingbo 已提交
174 175 176 177 178

```bash
curl http://192.168.0.1:6041/rest/login/root/taosdata
```

179
Response body:
D
dingbo 已提交
180 181 182 183 184 185 186 187 188

```json
{
  "status": "succ",
  "code": 0,
  "desc": "/KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04"
}
```

189
## For example
D
dingbo 已提交
190

191
- query all records from table d1001 of database demo
D
dingbo 已提交
192 193 194 195 196

  ```bash
  curl -H 'Authorization: Basic cm9vdDp0YW9zZGF0YQ==' -d 'select * from demo.d1001' 192.168.0.1:6041/rest/sql
  ```

197
  Response body:
D
dingbo 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

  ```json
  {
    "status": "succ",
    "head": ["ts", "current", "voltage", "phase"],
    "column_meta": [
      ["ts", 9, 8],
      ["current", 6, 4],
      ["voltage", 4, 4],
      ["phase", 6, 4]
    ],
    "data": [
      ["2018-10-03 14:38:05.000", 10.3, 219, 0.31],
      ["2018-10-03 14:38:15.000", 12.6, 218, 0.33]
    ],
    "rows": 2
  }
  ```

217
- Create database demo:
D
dingbo 已提交
218 219 220 221 222

  ```bash
  curl -H 'Authorization: Basic cm9vdDp0YW9zZGF0YQ==' -d 'create database demo' 192.168.0.1:6041/rest/sql
  ```

223
  Response body:
D
dingbo 已提交
224 225 226 227 228 229 230 231 232 233 234

  ```json
  {
    "status": "succ",
    "head": ["affected_rows"],
    "column_meta": [["affected_rows", 4, 4]],
    "data": [[1]],
    "rows": 1
  }
  ```

235
## Other Uses
D
dingbo 已提交
236

237
### Unix timestamps for result sets
D
dingbo 已提交
238

239
When the HTTP request URL uses `sqlt`, the timestamp of the returned result set will be in Unix timestamp format, for example:
D
dingbo 已提交
240 241 242 243 244

```bash
curl -H 'Authorization: Basic cm9vdDp0YW9zZGF0YQ==' -d 'select * from demo.d1001' 192.168.0.1:6041/rest/sqlt
```

245
Response body:
D
dingbo 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

```json
{
  "status": "succ",
  "head": ["ts", "current", "voltage", "phase"],
  "column_meta": [
    ["ts", 9, 8],
    ["current", 6, 4],
    ["voltage", 4, 4],
    ["phase", 6, 4]
  ],
  "data": [
    [1538548685000, 10.3, 219, 0.31],
    [1538548695000, 12.6, 218, 0.33]
  ],
  "rows": 2
}
```

265
### UTC format for the result set
D
dingbo 已提交
266

267
When the HTTP request URL uses `sqlutc`, the timestamp of the returned result set will be expressed as a UTC format, for example:
D
dingbo 已提交
268 269 270 271 272

```bash
  curl -H 'Authorization: Basic cm9vdDp0YW9zZGF0YQ==' -d 'select * from demo.t1' 192.168.0.1:6041/rest/sqlutc
```

273
Respones body:
D
dingbo 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

```json
{
  "status": "succ",
  "head": ["ts", "current", "voltage", "phase"],
  "column_meta": [
    ["ts", 9, 8],
    ["current", 6, 4],
    ["voltage", 4, 4],
    ["phase", 6, 4]
  ],
  "data": [
    ["2018-10-03T14:38:05.000+0800", 10.3, 219, 0.31],
    ["2018-10-03T14:38:15.000+0800", 12.6, 218, 0.33]
  ],
  "rows": 2
}
```

293
## Important configuration items
D
dingbo 已提交
294

295
Only some configuration parameters related to the RESTful interface are listed below. Please see the description in the configuration file for other system parameters.
D
dingbo 已提交
296

297 298 299 300 301 302
- The port number of the external RESTful service is bound to 6041 by default (the actual value is serverPort + 11, so it can be changed by modifying the setting of the serverPort parameter).
- httpMaxThreads: the number of threads to start, default is 2 (the default value is rounded down to half of the CPU cores with version 2.0.17.0 and later versions).
- restfulRowLimit: the maximum number of result sets (in JSON format) to return. The default value is 10240.
- httpEnableCompress: whether to support compression, the default is not supported. Currently, TDengine only supports the gzip compression format.
- httpDebugFlag: logging switch, default 131. 131: error and alarm messages only, 135: debug messages, 143: very detailed debug messages, default 131.
- httpDbNameMandatory: users must specify the default database name in the RESTful URL. The default is 0, which turns off this check. If set to 1, users must put a default database name in every RESTful URL. Otherwise, it will return an execution error and reject this SQL statement, regardless of whether the SQL statement executed at this time requires a specified database.
D
dingbo 已提交
303 304

:::note 
305
If you are using the REST API provided by taosd, you should write the above configuration in taosd's configuration file taos.cfg. If you use the REST API of taosAdapter, you need to refer to taosAdapter [corresponding configuration method](/reference/taosadapter/).
D
dingbo 已提交
306
:::