RESTful Services

IoTDB’s RESTful services can be used for query, write, and management operations, using the OpenAPI standard to define interfaces and generate frameworks.

Enable RESTful Services

RESTful services are disabled by default.

  • Developer

    Find the IoTDBrestServiceConfig class under org.apache.iotdb.db.conf.rest in the sever module, and modify enableRestService=true.

  • User

    Find the conf/iotdb.rest.properties file under the IoTDB installation directory and set enable_rest_service to true to enable the module.

    1. enable_rest_service=true

Authentication

Except the liveness probe API /ping, RESTful services use the basic authentication. Each URL request needs to carry 'Authorization': 'Basic ' + base64.encode(username + ':' + password).

The username used in the following examples is: root, and password is: root.

And the authorization header is

  1. Authorization: Basic cm9vdDpyb2901
  • If a user authorized with incorrect username or password, the following error is returned:

    HTTP Status Code:401

    HTTP response body:

    1. {
    2. "code": 600,
    3. "message": "WRONG_LOGIN_PASSWORD_ERROR"
    4. }
  • If the Authorization header is missing,the following error is returned:

    HTTP Status Code:401

    HTTP response body:

    1. {
    2. "code": 603,
    3. "message": "UNINITIALIZED_AUTH_ERROR"
    4. }

Interface

ping

The /ping API can be used for service liveness probing.

Request method: GET

Request path: http://ipREST API V1 (Not Recommend) - 图1open in new window:port/ping

The user name used in the example is: root, password: root

Example request:

  1. $ curl http://127.0.0.1:18080/ping

Response status codes:

  • 200: The service is alive.
  • 503: The service cannot accept any requests now.

Response parameters:

parameter nameparameter typeparameter describe
codeintegerstatus code
messagestringmessage

Sample response:

  • With HTTP status code 200:

    1. {
    2. "code": 200,
    3. "message": "SUCCESS_STATUS"
    4. }
  • With HTTP status code 503:

    1. {
    2. "code": 500,
    3. "message": "thrift service is unavailable"
    4. }

/ping can be accessed without authorization.

query

The query interface can be used to handle data queries and metadata queries.

Request method: POST

Request header: application/json

Request path: http://ipREST API V1 (Not Recommend) - 图2open in new window:port/rest/v1/query

Parameter Description:

parameter nameparameter typerequiredparameter description
sqlstringyes
rowLimitintegernoThe maximum number of rows in the result set that can be returned by a query.
If this parameter is not set, the rest_query_default_row_size_limit of the configuration file will be used as the default value.
When the number of rows in the returned result set exceeds the limit, the status code 411 will be returned.

Response parameters:

parameter nameparameter typeparameter description
expressionsarrayArray of result set column names for data query, null for metadata query
columnNamesarrayArray of column names for metadata query result set, null for data query
timestampsarrayTimestamp column, null for metadata query
valuesarrayA two-dimensional array, the first dimension has the same length as the result set column name array, and the second dimension array represents a column of the result set

Examples:

Tip: Statements like select * from root.xx.** are not recommended because those statements may cause OOM.

Expression query

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select s3, s4, s3 + 1 from root.sg27 limit 2"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": [
  3. "root.sg27.s3",
  4. "root.sg27.s4",
  5. "root.sg27.s3 + 1"
  6. ],
  7. "columnNames": null,
  8. "timestamps": [
  9. 1635232143960,
  10. 1635232153960
  11. ],
  12. "values": [
  13. [
  14. 11,
  15. null
  16. ],
  17. [
  18. false,
  19. true
  20. ],
  21. [
  22. 12.0,
  23. null
  24. ]
  25. ]
  26. }

Show child paths

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show child paths root"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "child paths"
  5. ],
  6. "timestamps": null,
  7. "values": [
  8. [
  9. "root.sg27",
  10. "root.sg28"
  11. ]
  12. ]
  13. }

Show child nodes

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show child nodes root"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "child nodes"
  5. ],
  6. "timestamps": null,
  7. "values": [
  8. [
  9. "sg27",
  10. "sg28"
  11. ]
  12. ]
  13. }

Show all ttl

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show all ttl"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "database",
  5. "ttl"
  6. ],
  7. "timestamps": null,
  8. "values": [
  9. [
  10. "root.sg27",
  11. "root.sg28"
  12. ],
  13. [
  14. null,
  15. null
  16. ]
  17. ]
  18. }

Show ttl

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show ttl on root.sg27"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "database",
  5. "ttl"
  6. ],
  7. "timestamps": null,
  8. "values": [
  9. [
  10. "root.sg27"
  11. ],
  12. [
  13. null
  14. ]
  15. ]
  16. }

Show functions

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show functions"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "function name",
  5. "function type",
  6. "class name (UDF)"
  7. ],
  8. "timestamps": null,
  9. "values": [
  10. [
  11. "ABS",
  12. "ACOS",
  13. "ASIN",
  14. ...
  15. ],
  16. [
  17. "built-in UDTF",
  18. "built-in UDTF",
  19. "built-in UDTF",
  20. ...
  21. ],
  22. [
  23. "org.apache.iotdb.db.query.udf.builtin.UDTFAbs",
  24. "org.apache.iotdb.db.query.udf.builtin.UDTFAcos",
  25. "org.apache.iotdb.db.query.udf.builtin.UDTFAsin",
  26. ...
  27. ]
  28. ]
  29. }

Show timeseries

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show timeseries"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "timeseries",
  5. "alias",
  6. "database",
  7. "dataType",
  8. "encoding",
  9. "compression",
  10. "tags",
  11. "attributes"
  12. ],
  13. "timestamps": null,
  14. "values": [
  15. [
  16. "root.sg27.s3",
  17. "root.sg27.s4",
  18. "root.sg28.s3",
  19. "root.sg28.s4"
  20. ],
  21. [
  22. null,
  23. null,
  24. null,
  25. null
  26. ],
  27. [
  28. "root.sg27",
  29. "root.sg27",
  30. "root.sg28",
  31. "root.sg28"
  32. ],
  33. [
  34. "INT32",
  35. "BOOLEAN",
  36. "INT32",
  37. "BOOLEAN"
  38. ],
  39. [
  40. "RLE",
  41. "RLE",
  42. "RLE",
  43. "RLE"
  44. ],
  45. [
  46. "SNAPPY",
  47. "SNAPPY",
  48. "SNAPPY",
  49. "SNAPPY"
  50. ],
  51. [
  52. null,
  53. null,
  54. null,
  55. null
  56. ],
  57. [
  58. null,
  59. null,
  60. null,
  61. null
  62. ]
  63. ]
  64. }

Show latest timeseries

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show latest timeseries"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "timeseries",
  5. "alias",
  6. "database",
  7. "dataType",
  8. "encoding",
  9. "compression",
  10. "tags",
  11. "attributes"
  12. ],
  13. "timestamps": null,
  14. "values": [
  15. [
  16. "root.sg28.s4",
  17. "root.sg27.s4",
  18. "root.sg28.s3",
  19. "root.sg27.s3"
  20. ],
  21. [
  22. null,
  23. null,
  24. null,
  25. null
  26. ],
  27. [
  28. "root.sg28",
  29. "root.sg27",
  30. "root.sg28",
  31. "root.sg27"
  32. ],
  33. [
  34. "BOOLEAN",
  35. "BOOLEAN",
  36. "INT32",
  37. "INT32"
  38. ],
  39. [
  40. "RLE",
  41. "RLE",
  42. "RLE",
  43. "RLE"
  44. ],
  45. [
  46. "SNAPPY",
  47. "SNAPPY",
  48. "SNAPPY",
  49. "SNAPPY"
  50. ],
  51. [
  52. null,
  53. null,
  54. null,
  55. null
  56. ],
  57. [
  58. null,
  59. null,
  60. null,
  61. null
  62. ]
  63. ]
  64. }

Count timeseries

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"count timeseries root.**"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "count"
  5. ],
  6. "timestamps": null,
  7. "values": [
  8. [
  9. 4
  10. ]
  11. ]
  12. }

Count nodes

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"count nodes root.** level=2"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "count"
  5. ],
  6. "timestamps": null,
  7. "values": [
  8. [
  9. 4
  10. ]
  11. ]
  12. }

Show devices

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show devices"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "devices",
  5. "isAligned"
  6. ],
  7. "timestamps": null,
  8. "values": [
  9. [
  10. "root.sg27",
  11. "root.sg28"
  12. ],
  13. [
  14. "false",
  15. "false"
  16. ]
  17. ]
  18. }

Show devices with database

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"show devices with database"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "devices",
  5. "database",
  6. "isAligned"
  7. ],
  8. "timestamps": null,
  9. "values": [
  10. [
  11. "root.sg27",
  12. "root.sg28"
  13. ],
  14. [
  15. "root.sg27",
  16. "root.sg28"
  17. ],
  18. [
  19. "false",
  20. "false"
  21. ]
  22. ]
  23. }

List user

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"list user"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "user"
  5. ],
  6. "timestamps": null,
  7. "values": [
  8. [
  9. "root"
  10. ]
  11. ]
  12. }

Aggregation

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select count(*) from root.sg27"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": [
  3. "count(root.sg27.s3)",
  4. "count(root.sg27.s4)"
  5. ],
  6. "columnNames": null,
  7. "timestamps": [
  8. 0
  9. ],
  10. "values": [
  11. [
  12. 1
  13. ],
  14. [
  15. 2
  16. ]
  17. ]
  18. }

Group by level

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select count(*) from root.** group by level = 1"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "count(root.sg27.*)",
  5. "count(root.sg28.*)"
  6. ],
  7. "timestamps": null,
  8. "values": [
  9. [
  10. 3
  11. ],
  12. [
  13. 3
  14. ]
  15. ]
  16. }

Group by

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select count(*) from root.sg27 group by([1635232143960,1635232153960),1s)"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": [
  3. "count(root.sg27.s3)",
  4. "count(root.sg27.s4)"
  5. ],
  6. "columnNames": null,
  7. "timestamps": [
  8. 1635232143960,
  9. 1635232144960,
  10. 1635232145960,
  11. 1635232146960,
  12. 1635232147960,
  13. 1635232148960,
  14. 1635232149960,
  15. 1635232150960,
  16. 1635232151960,
  17. 1635232152960
  18. ],
  19. "values": [
  20. [
  21. 1,
  22. 0,
  23. 0,
  24. 0,
  25. 0,
  26. 0,
  27. 0,
  28. 0,
  29. 0,
  30. 0
  31. ],
  32. [
  33. 1,
  34. 0,
  35. 0,
  36. 0,
  37. 0,
  38. 0,
  39. 0,
  40. 0,
  41. 0,
  42. 0
  43. ]
  44. ]
  45. }

Last

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select last s3 from root.sg27"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "expressions": null,
  3. "columnNames": [
  4. "timeseries",
  5. "value",
  6. "dataType"
  7. ],
  8. "timestamps": [
  9. 1635232143960
  10. ],
  11. "values": [
  12. [
  13. "root.sg27.s3"
  14. ],
  15. [
  16. "11"
  17. ],
  18. [
  19. "INT32"
  20. ]
  21. ]
  22. }

Disable align

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select * from root.sg27 disable align"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "code": 407,
  3. "message": "disable align clauses are not supported."
  4. }

Align by device

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select count(s3) from root.sg27 align by device"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "code": 407,
  3. "message": "align by device clauses are not supported."
  4. }

Select into

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"select s3, s4 into root.sg29.s1, root.sg29.s2 from root.sg27"}' http://127.0.0.1:18080/rest/v1/query
  1. {
  2. "code": 407,
  3. "message": "select into clauses are not supported."
  4. }

nonQuery

Request method: POST

Request header: application/json

Request path: http://ipREST API V1 (Not Recommend) - 图3open in new window:port/rest/v1/nonQuery

Parameter Description:

parameter nameparameter typeparameter describe
sqlstringquery content

Example request:

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"sql":"CREATE DATABASE root.ln"}' http://127.0.0.1:18080/rest/v1/nonQuery

Response parameters:

parameter nameparameter typeparameter describe
codeintegerstatus code
messagestringmessage

Sample response:

  1. {
  2. "code": 200,
  3. "message": "SUCCESS_STATUS"
  4. }

insertTablet

Request method: POST

Request header: application/json

Request path: http://ipREST API V1 (Not Recommend) - 图4open in new window:port/rest/v1/insertTablet

Parameter Description:

parameter nameparameter typeis requiredparameter describe
timestampsarrayyesTime column
measurementsarrayyesThe name of the measuring point
dataTypesarrayyesThe data type
valuesarrayyesValue columns, the values in each column can be null
isAlignedbooleanyesWhether to align the timeseries
deviceIdstringyesDevice name

Example request:

  1. curl -H "Content-Type:application/json" -H "Authorization:Basic cm9vdDpyb290" -X POST --data '{"timestamps":[1635232143960,1635232153960],"measurements":["s3","s4"],"dataTypes":["INT32","BOOLEAN"],"values":[[11,null],[false,true]],"isAligned":false,"deviceId":"root.sg27"}' http://127.0.0.1:18080/rest/v1/insertTablet

Sample response:

parameter nameparameter typeparameter describe
codeintegerstatus code
messagestringmessage

Sample response:

  1. {
  2. "code": 200,
  3. "message": "SUCCESS_STATUS"
  4. }

Configuration

The configuration is located in ‘iotdb-rest.properties’.

  • Set ‘enable_rest_service’ to ‘true’ to enable the module, and ‘false’ to disable the module. By default, this value is’ false ‘.
  1. enable_rest_service=true
  • This parameter is valid only when ‘enable_REST_service =true’. Set ‘rest_service_port’ to a number (1025 to 65535) to customize the REST service socket port. By default, the value is 18080.
  1. rest_service_port=18080
  • Set ‘enable_swagger’ to ‘true’ to display rest service interface information through swagger, and ‘false’ to do not display the rest service interface information through the swagger. By default, this value is’ false ‘.
  1. enable_swagger=false
  • The maximum number of rows in the result set that can be returned by a query. When the number of rows in the returned result set exceeds the limit, the status code 411 is returned.
  1. rest_query_default_row_size_limit=10000
  • Expiration time for caching customer login information (used to speed up user authentication, in seconds, 8 hours by default)
  1. cache_expire=28800
  • Maximum number of users stored in the cache (default: 100)
  1. cache_max_num=100
  • Initial cache size (default: 10)
  1. cache_init_num=10
  • REST Service whether to enable SSL configuration, set ‘enable_https’ to’ true ‘to enable the module, and set’ false ‘to disable the module. By default, this value is’ false ‘.
  1. enable_https=false
  • keyStore location path (optional)
  1. key_store_path=
  • keyStore password (optional)
  1. key_store_pwd=
  • trustStore location path (optional)
  1. trust_store_path=
  • trustStore password (optional)
  1. trust_store_pwd=
  • SSL timeout period, in seconds
  1. idle_timeout=5000