Write Data

CnosDBAbout 3 minAbout 849 words

Wirte Data

CnosDB supports a variety of write modes, and some examples are provided below. Contains common programming languages such as Java, Python, Rust, Golang, C++.

Insert One Record

Syntax

  • use api /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>"
    
  • use api /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>"
    

    Notice that the data to be inserted should be in the format of Line Protocolopen in new window.

Example

  • use api /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);"
    
  • use api /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"
    

Insert Multiple Records

Syntax

  • use api /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>"
    
  • use api /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>"
    

    Notice that the data to be inserted should be in the format of Line Protocolopen in new window.

Example

  • use api /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);"
    
  • use api /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"
    

Load Data

Syntax

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>

Example

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

Use Programming Language

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)
    }

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

    // add Accept header
    req.Header.Set("Accept", "application/json")

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

    // print response
    fmt.Println("Status Code:", resp.StatusCode)
    fmt.Println("Response Body:")
    buf := new(bytes.Buffer)
    buf.ReadFrom(resp.Body)
    fmt.Println(buf.String())
}

When using the api /api/v1/write, just replace the api address wth /api/v1/write and replace <your SQL statement> with <your data>.

Related Content: