Skip to content

Commit

Permalink
docs: add http example (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinbo3 authored Sep 22, 2024
1 parent 246a505 commit c113952
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 99 deletions.
36 changes: 35 additions & 1 deletion zh_CN/development-guide/multi-language-examples.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# 多语言接入示例

我们提供以下语言的示例,展示如何使用基于 HTTP/HTTPS 的 REST API 与 Datalayers 进行交互:

目前提供以下示例:
* Python
* Go
* Shell

::: code-group

Expand Down Expand Up @@ -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'
```

:::
108 changes: 10 additions & 98 deletions zh_CN/development-guide/query-with-restapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,119 +11,31 @@ http://127.0.0.1:8361/api/v1/sql?db=<database_name> \
-d '<SQL STATEMENT>'
```

## 示例

### 查询数据
## 查询数据示例

执行请求:

```shell
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
```

:::

0 comments on commit c113952

Please sign in to comment.