Skip to main content
Version: 2.3.x

Query Data

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

HTTP API Query

Query Data

Syntax

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>"

Example

curl -i -u "cnosdb:" -H "Accept: application/json" \
-XPOST "http://127.0.0.1:8902/api/v1/sql?db=oceanic_station" \
-d "SELECT * FROM air LIMIT 10;"
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())
}