HTTP API
GreptimeDB 提供了 HTTP API 用于与数据库进行交互。如需查看完整的 API 端点列表,请查看 HTTP Endpoints。
Base URL
API Base URL 是 http(s)://{{host}}:{{port}}/。
- 对于在本地机器上运行的 GreptimeDB 实例,Base URL 是
http://localhost:4000/,默认端口配置为4000。你可以在配置文件中更改服务的 host 和 port。 - 对于 GreptimeCloud,Base URL 是
https://{{host}}/。你可以在 GreptimeCloud 控制台的 "Connection Information" 中找到 host。
在以下内容中,我们使用 http://{{API-host}}/ 作为 Base URL 来演示 API。
通用 Headers
鉴权
假设你已经正确设置了数据库鉴权,
GreptimeDB 支持 HTTP API 中内置的 Basic 鉴权机制。要设置鉴权,请按照以下步骤操作:
- 使用
<username:password>格式和Base64算法对用户名和密码进行编码。 - 将编码后的凭据附加到下列 HTTP 请求头之一中:
Authorization : Basic <base64-encoded-credentials>x-greptime-auth : Basic <base64-encoded-credentials>
以下是一个示例。如果要使用用户名 greptime_user 和密码 greptime_pwd 连接到 GreptimeDB,请使用以下命令:
curl -X POST \
-H 'Authorization: Basic Z3JlcHRpbWVfdXNlcjpncmVwdGltZV9wd2Q=' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'sql=show tables' \
http://localhost:4000/v1/sql
在此示例中,Z3JlcHRpbWVfdXNlcjpncmVwdGltZV9wd2Q= 表示 greptime_user:greptime_pwd 的 Base64 编码值。请确保用自己配置的用户名和密码替换它,并使用 Base64 进行编码。
InfluxDB 使用自己的鉴权格式,请参阅 InfluxDB 获取详细信息。
请 求超时设置
GreptimeDB 支持在 HTTP 请求中使用 X-Greptime-Timeout 请求头,用于指定数据库服务器中运行的请求超时时间。
例如,以下请求为查询设置了 120s 的超时时间:
curl -X POST \
-H 'Authorization: Basic {{authentication}}' \
-H 'X-Greptime-Timeout: 120s' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'sql=show tables' \
http://localhost:4000/v1/sql
Admin APIs
这些 API 在 GreptimeCloud 中无法使用。
检查数据库健康状态
你可以使用 /health 端点检查 GreptimeDB 服务器的健康状况。
有关更多信息,请参阅检查数据库健康状态。
检查数据库状态
你可以使用 /status 端点检查 GreptimeDB 服务器的状态。
curl http://{{API-host}}/status
例如:
curl http://localhost:4000/status
输出包含数据库版本和源代码信息,类似如下:
{
"source_time": "2024-11-08T06:34:49Z",
"commit": "0e0c4faf0d784f25fed8f26e7000f1f869c88587",
"branch": "main",
"rustc_version": "rustc 1.84.0-nightly (e92993dbb 2024-10-18)",
"hostname": "local",
"version": "0.9.5"
}
获取 GreptimeDB 服务器配置
你可以使用 /config 端点获取 GreptimeDB 服务器的 TOML 配置。
curl http://{{API-host}}/config
例如:
curl http://localhost:4000/config
输出包含 GreptimeDB 服务器的配置信息。
enable_telemetry = true
user_provider = "static_user_provider:file:user"
init_regions_in_background = false
init_regions_parallelism = 16
[http]
addr = "127.0.0.1:4000"
timeout = "30s"
body_limit = "64MiB"
is_strict_mode = false
# ...
POST SQL 语句
要通过 HTTP API 向 GreptimeDB 服务器提交 SQL 语句,请使用以下格式:
curl -X POST \
-H 'Authorization: Basic {{authentication}}' \
-H 'X-Greptime-Timeout: {{time precision}}' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'sql={{SQL-statement}}' \
http://{{API-host}}/v1/sql
Headers
AuthorizationX-Greptime-TimeoutContent-Type:application/x-www-form-urlencoded.X-Greptime-Timezone: 当前 SQL 查询的时区。可选。请参阅时区。
Query string parameters
db: 数据库名称。可选。如果未提供,将使用默认数据库public。format: 输出格式。可选。 除了默认的 JSON 格式外,HTTP API 还允许你通过提供format查询参数来自定义输出格式,值如下:influxdb_v1: influxdb 查询 API 兼容格式。附加参数:epoch:[ns,u,µ,ms,s,m,h],返回指定精度的时间戳
csv: 逗号分隔值输出arrow: Arrow IPC 格式。附加参数:compression:zstd或lz4,默认:无压缩
table: 控制台输出的 ASCII 表格格式
Body
sql: SQL 语句。必填。
响应
响应是一个 JSON 对象,包含以下字段:
output: SQL 执行结果,请参阅下面的示例以了解详细信息。execution_time_ms: 语句的执行时间(毫秒)。
示例
INSERT 语句
例如,要向数据库 public 的 monitor 表中插入数据,请使用以下命令:
curl -X POST \
-H 'Authorization: Basic {{authorization if exists}}' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'sql=INSERT INTO monitor VALUES ("127.0.0.1", 1667446797450, 0.1, 0.4), ("127.0.0.2", 1667446798450, 0.2, 0.3), ("127.0.0.1", 1667446798450, 0.5, 0.2)' \
http://localhost:4000/v1/sql?db=public
Response 包含受影响的行数:
{"output":[{"affectedrows":3}],"execution_time_ms":11}
SELECT 语句
你还可以使用 HTTP API 执行其他 SQL 语句。例如,从 monitor 表中查询数据:
curl -X POST \
-H 'Authorization: Basic {{authorization if exists}}' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d "sql=SELECT * FROM monitor" \
http://localhost:4000/v1/sql?db=public
Response 包含 JSON 格式的查询数据:
{
"output": [
{
"records": {
"schema": {
"column_schemas": [
{
"name": "host",
"data_type": "String"
},
{
"name": "ts",
"data_type": "TimestampMillisecond"
},
{
"name": "cpu",
"data_type": "Float64"
},
{
"name": "memory",
"data_type": "Float64"
}
]
},
"rows": [
[
"127.0.0.1",
1720728000000,
0.5,
0.1
]
],
"total_rows": 1
}
}
],
"execution_time_ms": 7
}
Response 包含以下字段:
output: 执行结果。records: 查询结果。schema: 结果的 schema,包括每列的 schema。rows: 查询结果的行,每行是一个包含 schema 中对应列值的数组。
execution_time_ms: 语句的执行时间(毫秒)。
时区
GreptimeDB 支持 HTTP 请求中的 X-Greptime-Timezone header。
它用于为当前 SQL 查询指定时区。
例如,以下请求使用时区 +1:00 进行查询:
curl -X POST \
-H 'Authorization: Basic {{authentication}}' \
-H 'X-Greptime-Timezone: +1:00' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'sql=SHOW VARIABLES time_zone;' \
http://localhost:4000/v1/sql
查询后的结果为:
{
"output": [
{
"records": {
"schema": {
"column_schemas": [
{
"name": "TIME_ZONE",
"data_type": "String"
}
]
},
"rows": [
[
"+01:00"
]
]
}
}
],
"execution_time_ms": 27
}