diff --git a/zh_CN/development-guide/multi-language-examples.md b/zh_CN/development-guide/multi-language-examples.md index 0290191..c016de5 100644 --- a/zh_CN/development-guide/multi-language-examples.md +++ b/zh_CN/development-guide/multi-language-examples.md @@ -1,8 +1,10 @@ # 多语言接入示例 我们提供以下语言的示例,展示如何使用基于 HTTP/HTTPS 的 REST API 与 Datalayers 进行交互: - +目前提供以下示例: * Python +* Go +* Shell ::: code-group @@ -257,4 +259,36 @@ func main() { } ``` +```Shell [Shell] +// 创建数据库 +curl -u"admin:public" -X POST \ +http://127.0.0.1:8361/api/v1/sql \ +-H 'Content-Type: application/binary' \ +-d 'create database demo' + +// 创建表 +curl -u"admin:public" -X POST \ +http://127.0.0.1:8361/api/v1/sql?db=demo \ +-H 'Content-Type: application/binary' \ +-d 'CREATE TABLE sensor_info ( + ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + sn INT32 NOT NULL, + speed int, + longitude float, + latitude float, + timestamp KEY (ts)) PARTITION BY HASH(sn) PARTITIONS 2 ENGINE=TimeSeries;' + +// 写入数据 +curl -u"admin:public" -X POST \ +http://127.0.0.1:8361/api/v1/sql?db=demo \ +-H 'Content-Type: application/binary' \ +-d 'INSERT INTO sensor_info(sn, speed, longitude, latitude) VALUES(1, 120, 104.07, 30.59),(2, 120, 104.07, 30.59)' + +// 查询数据 +curl -u"admin:public" -X POST \ +http://127.0.0.1:8361/api/v1/sql?db=demo \ +-H 'Content-Type: application/binary' \ +-d 'SELECT * FROM sensor_info' +``` + ::: diff --git a/zh_CN/development-guide/query-with-restapi.md b/zh_CN/development-guide/query-with-restapi.md index 0a48ea1..9f207a5 100644 --- a/zh_CN/development-guide/query-with-restapi.md +++ b/zh_CN/development-guide/query-with-restapi.md @@ -11,9 +11,7 @@ http://127.0.0.1:8361/api/v1/sql?db= \ -d '' ``` -## 示例 - -### 查询数据 +## 查询数据示例 执行请求: @@ -21,109 +19,23 @@ http://127.0.0.1:8361/api/v1/sql?db= \ curl -u"admin:public" -X POST \ http://127.0.0.1:8361/api/v1/sql?db=demo \ -H 'Content-Type: application/binary' \ --d 'SELECT * FROM events WHERE user_id=1' +-d 'SELECT * FROM sensor_info' ``` 返回值: ```json { - "result": { - "columns": [ - "ts", - "user_id", - "message" - ], - "types": [ - "Timestamp(Millisecond, Asia/Shanghai)", - "UInt64", - "Utf8" - ], - "values": [ - [ - "2024-09-02T20:59:18.180+08:00", - "1", - "Click" - ] - ] - } + "result": { + "columns": ["ts", "sn", "speed", "longitude", "latitude"], + "types": ["TIMESTAMP(3)", "INT32", "INT32", "REAL", "REAL"], + "values": [ + ["2024-09-22T17:30:56.349+08:00", 1, 120, 104.07, 30.59], + ["2024-09-22T17:30:56.349+08:00", 2, 120, 104.07, 30.59] + ] + } } ``` 其中,`result`表示这是一条查询的结果,`columns` 为列名,`types` 为列类型,`values` 为查询到的行数组。需要注意的是,类型中时间戳的表示为`Timestamp(TimeUnit, Timezone)`,当某列类型是时区未被指定的时间戳时,Timezone为None。例如`Timestamp(Millisecond, None)`。 -## 编程语言示例 - -::: code-group - -```go -package main - -import ( - "bytes" - "encoding/base64" - "fmt" - "io" - "net/http" -) - -func main() { - - username := "admin" - password := "public" - - auth := username + ":" + password - authHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) - - sql := "select * from sensor_info limit 10" - - url := "http://127.0.0.1:8361/api/v1/sql?db=demo" - req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(sql))) - if err != nil { - fmt.Println("Error creating request:", err) - return - } - - req.Header.Set("Content-Type", "application/binary") - req.Header.Set("Authorization", authHeader) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error sending request:", err) - return - } - defer resp.Body.Close() - - body, err := io.ReadAll(resp.Body) - if err != nil { - fmt.Println("Error reading response:", err) - return - } - - fmt.Println("Response Status:", resp.Status) - fmt.Println("Response Body:", string(body)) -} -``` - -```java [JAVA] -import type { UserConfig } from 'vitepress' - -const config: UserConfig = { - // ... -} - -export default config -``` - -```rust [Rust] -import type { UserConfig } from 'vitepress' - -const config: UserConfig = { - // ... -} - -export default config -``` - -:::