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 <base64-encoded-credentials>' \
-H 'X-Greptime-Timeout: 120s' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'sql=show tables' \
http://localhost:4000/v1/sql
Hints
GreptimeDB 支持在 HTTP 请求中使用 x-greptime-hints 请求头来传递影响请求行为的键值对。
这些 hints 主要用于在数据写入时自动建表的场景下设置表选项。
x-greptime-hints 格式为逗号分隔的 key=value 对:
x-greptime-hints: key1=value1, key2=value2
你也可以使用 x-greptime-hint-<key> 为每个请求头传递一个 hint:
x-greptime-hint-key1: value1
x-greptime-hint-key2: value2
支持的 hints:
| Hint | 类型 | 默认值 | 说明 |
|---|---|---|---|
auto_create_table | Boolean | true | 插入数据时,如果表不存在是否自动创建。 |
ttl | 时间字符串 | 无 | 设置表的数据过期时间,例如 7d、24h。过期数据将被自动清理。 |
append_mode | Boolean | false | 启用表的 append-only 模式,该模式禁用按主键去重,支持重复行。对于 InfluxDB 行协议写入,显式设置 append_mode=true hint 时,会使用 append_mode = 'true' 和 merge_mode = 'last_row' 创建表。 |
merge_mode | String | 无 | 设置表的 merge 模式,例如 last_non_null、last_row。对于通过 InfluxDB 行协议自动创建的表,该 hint 优先于 influxdb.default_merge_mode 配置;该配置默认值为 last_non_null。启用 append_mode 时,仅允许使用 last_row。 |
physical_table | String | 无 | 指定 metric 引擎的物理表名。 |
query.enable_remote_dynamic_filter_pushdown | Boolean | true | 为 SQL 查询启用远程动态过滤下推。设置为 false 可为当前请求关闭 Frontend 到 Datanode 的动态过滤传播。请参阅远程动态过滤下推。 |
skip_wal | Boolean | false | 跳过表的 WAL(Write-Ahead Log)写入。 |
sst_format | String | 无 | 设置表的 SST(Sorted String Table)文件格式。可选值:flat、primary_key。 |
trace_table_partitions | Int | None | 自定义 Trace 表的默认分区数(16)。设置为 0 或 1 时禁用分区。 |