跳到主要内容
版本:2.4.x

写入数据

CnosDB 支持多种写入方式,以下提供一些示例。包含 Java,Python,Rust,Golang,C++ 等常用的编程语言。

单条数据写入

语法

  • 使用 /api/v1/sql 接口

    curl -i -u "<username>:<password>" -H "Accept: application/json" \
    -XPOST "http://<cnosdb_url>:<cnosdb_port>/api/v1/sql?db=<database_name>&pretty=true" \
    -d "<your SQL statement>"
  • 使用 /api/v1/write 接口

    curl -i -u "<username>:<password>" -H "Accept: application/json" \
    -XPOST "http://<cnosdb_url>:<cnosdb_port>/api/v1/write?db=<database_name>&pretty=true" \
    -d "<your data>"

    注意,插入的数据应该是符合Line Protocol 的格式。

示例

  • 使用 /api/v1/sql 接口

    curl -i -u "cnosdb:" -H "Accept: application/json" \
    -XPOST "http://127.0.0.1:8902/api/v1/sql?db=oceanic_station" \
    -d "INSERT INTO air (TIME, station, visibility, temperature, pressure)
    VALUES (1666165200290401000, 'XiaoMaiDao', 56, 69, 77);"
  • 使用 /api/v1/write 接口

    curl -i -u "cnosdb:" -H "Accept: application/json" \
    -XPOST "http://127.0.0.1:8902/api/v1/write?db=oceanic_station" \
    -d "air,station=XiaoMaiDao visibility=50,temperature=63,pressure=52 1642176000000000000"

多条数据写入

语法

  • 使用 /api/v1/sql 接口

    curl -i -u "<username>:<password>" -H "Accept: application/json" \
    -XPOST "http://<cnosdb_url>:<cnosdb_port>/api/v1/sql?db=<database_name>&pretty=true" \
    -d "<your SQL statement>"
  • 使用 /api/v1/write 接口

    curl -i -u "<username>:<password>" -H "Accept: application/json" \
    -XPOST "http://<cnosdb_url>:<cnosdb_port>/api/v1/write?db=<database_name>&pretty=true" \
    -d "<your data>
    <your data>"

    注意,插入的数据应该是符合Line Protocol 的格式。

示例

  • 使用 /api/v1/sql 接口

    curl -i -u "cnosdb:" -H "Accept: application/json" \
    -XPOST "http://127.0.0.1:8902/api/v1/sql?db=oceanic_station" \
    -d "INSERT INTO air (TIME, station, visibility, temperature, pressure)
    VALUES ('2022-10-19 05:40:00', 'XiaoMaiDao', 55, 68, 76), ('2022-10-19 04:40:00', 'XiaoMaiDao', 55, 68, 76);"
  • 使用 /api/v1/write 接口

    curl -i -u "cnosdb:" -H "Accept: application/json" \
    -XPOST "http://127.0.0.1:8902/api/v1/write?db=oceanic_station" \
    -d "air,station=XiaoMaiDao visibility=50,temperature=63,pressure=52 1642176000000000000
    air,station=XiaoMaiDao visibility=50,temperature=63,pressure=52 1642176000000000000"

批量数据写入

语法

curl -i -u "<username>:<password>" -H "Accept: application/json" \
-XPOST "http://<cnosdb_url>:<cnosdb_port>/api/v1/write?db=<database_name>&pretty=true" \
--data-binary @<data_file_path>

示例

curl -o oceanic_station.txt https://dl.cnosdb.com/sample/oceanic_station.txt &&
curl -i -u "cnosdb:" -H "Accept: application/json" \
-XPOST "http://127.0.0.1:8902/api/v1/write?db=oceanic_station" \
--data-binary @./oceanic_station.txt

使用编程语言写入

package main

import (
"fmt"
"net/http"
"bytes"
"encoding/base64"
)

func main() {
username := "<username>"
password := "<password>"
url := "http://<cnosdb_url>:<cnosdb_port>/api/v1/sql?db=<database_name>&pretty=true"
sqlStatement := "<your SQL statement>"

client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBufferString(sqlStatement))
if err != nil {
panic(err)
}

// 添加 Authorization 头部
authStr := username + ":" + password
encodedAuth := base64.StdEncoding.EncodeToString([]byte(authStr))
req.Header.Set("Authorization", "Basic "+encodedAuth)

// 添加 Accept 头部
req.Header.Set("Accept", "application/json")

resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

// 输出响应的状态码和响应体
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Response Body:")
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
fmt.Println(buf.String())
}

当使用 /api/v1/write 接口时,只需要替换接口地址,并将 <your SQL statement> 替换为 <your data>