Skip to content

Commit

Permalink
docs: more docs (#21)
Browse files Browse the repository at this point in the history
* more docs

* lisence
  • Loading branch information
yuanbohan authored Feb 7, 2024
1 parent b3ab66a commit ddd87b4
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 69 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ header:
- '.gitignore'
- 'LICENSE'
- '*.md'
- 'examples/**/*.md'
- "*.yml"
- "*.yaml"
- 'go.mod'
Expand Down
81 changes: 32 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@

Provide API to insert data into GreptimeDB.

start GreptimeDB via Docker

```shell
docker run --rm -p 4000-4003:4000-4003 \
--name greptime greptime/greptimedb standalone start \
--http-addr 0.0.0.0:4000 \
--rpc-addr 0.0.0.0:4001 \
--mysql-addr 0.0.0.0:4002 \
--postgres-addr 0.0.0.0:4003
```

## Basic Example

- [schema](examples/schema/main.go)
Expand Down Expand Up @@ -67,46 +56,40 @@ stream, err := client.NewStreamClient(cfg)

#### Insert & StreamInsert

streaming insert is to Send data into GreptimeDB without waiting for response.
- you can Insert data into GreptimeDB via:
- [define schema](#with-schema-predefined)
- [define struct](#with-struct-tag)
- streaming insert is to Send data into GreptimeDB without waiting for response.

##### Datatypes supported

###### Go

- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64
- bool
- string
- []byte, []uint8, [N]byte, [N]uint8
- time.Time

###### GreptimeDB

- INT, INT8, INT16, INT32, INT64
- UINT, UINT8, UINT16, UINT32, UINT64
- FLOAT, FLOAT32, FLOAT64
- BOOL, BOOLEAN
- STRING
- BINARY, BYTES
- DATE // the day elapsed since January 1, 1970.
- DATETIME // the milliseconds elapsed since January 1, 1970.
- TIMESTAMP
- TIMESTAMP_SECOND
- TIMESTAMP_MILLISECOND
- TIMESTAMP_MICROSECOND
- TIMESTAMP_NANOSECOND

NOTE: the following are the same

- INT, INT64
- UINT, UINT64
- FLOAT, FLOAT64
- BOOL, BOOLEAN
- BINARY, BYTES
- TIMESTAMP, TIMESTAMP_MILLISECOND

##### With Schema predefined explicitly
The **GreptimeDB** column is for the datatypes supported in library, and the **Go** column is the matched Go type.

| GreptimeDB | Go | Description |
|----------------------------------|------------------|----------------------------------------|
| INT8 | int8 | |
| INT16 | int16 | |
| INT32 | int32 | |
| INT64, INT | int64 | |
| UINT8 | uint8 | |
| UINT16 | uint16 | |
| UINT32 | uint32 | |
| UINT64, UINT | uint64 | |
| FLOAT32 | float32 | |
| FLOAT64, FLOAT | float64 | |
| BOOLEAN, BOOL | bool | |
| STRING | string | |
| BINARY, BYTES | []byte | |
| DATE | Int or time.Time | the day elapsed since 1970-1-1 |
| DATETIME | Int or time.Time | the millisecond elapsed since 1970-1-1 |
| TIMESTAMP_SECOND | Int or time.Time | |
| TIMESTAMP_MILLISECOND, TIMESTAMP | Int or time.Time | |
| TIMESTAMP_MICROSECOND | Int or time.Time | |
| TIMESTAMP_NANOSECOND | Int or time.Time | |

NOTE: Int is for all of Integer and Unsigned Integer in Go

##### With Schema predefined

###### define table schema, and add rows

Expand Down Expand Up @@ -134,7 +117,7 @@ resp, err := cli.Write(context.Background(), tbl)
err := streamClient.Send(context.Background(), tbl)
```

##### With Schema defined in struct Tag
##### With Struct Tag

###### Tag

Expand Down
56 changes: 56 additions & 0 deletions examples/schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Insert data into GreptimeDB

## start GreptimeDB via Docker

```shell
docker run --rm -p 4000-4003:4000-4003 \
--name greptime greptime/greptimedb standalone start \
--http-addr 0.0.0.0:4000 \
--rpc-addr 0.0.0.0:4001 \
--mysql-addr 0.0.0.0:4002 \
--postgres-addr 0.0.0.0:4003
```

## Insert

```go
go run main.go
```

Output:

```log
2024/02/07 11:26:26 affected rows: 2
```

## Query

```shell
mysql -h 127.0.0.1 -P 4002 public
```

```shell
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.10-alpha-msql-proxy Greptime

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from monitors_with_schema;
+------+-------+-------------+----------------------------+
| id | host | temperature | timestamp |
+------+-------+-------------+----------------------------+
| 1 | hello | 1.1 | 2024-02-07 03:26:26.467898 |
| 2 | hello | 2.2 | 2024-02-07 03:26:26.467900 |
+------+-------+-------------+----------------------------+
2 rows in set (0.03 sec)
```
25 changes: 12 additions & 13 deletions examples/schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,56 +35,55 @@ func init() {

cli_, err := client.New(cfg)
if err != nil {
log.Fatal(err)
log.Panic(err)
}
cli = cli_

stream_, err := client.NewStreamClient(cfg)
if err != nil {
log.Fatal(err)
log.Panic(err)
}
stream = stream_
}

func main() {
tbl, err := table.New("monitors_with_schema")
if err != nil {
log.Fatal(err)
log.Println(err)
}

// add column at first. This is to define the schema of the table.
if err := tbl.AddTagColumn("id", types.INT64); err != nil {
log.Fatal(err)
log.Println(err)
}
if err := tbl.AddFieldColumn("host", types.STRING); err != nil {
log.Fatal(err)
log.Println(err)
}
if err := tbl.AddFieldColumn("temperature", types.FLOAT); err != nil {
log.Fatal(err)
log.Println(err)
}
if err := tbl.AddTimestampColumn("timestamp", types.TIMESTAMP_MICROSECOND); err != nil {
log.Fatal(err)
log.Println(err)
}

if err := tbl.AddRow(1, "hello", 1.1, time.Now()); err != nil {
log.Fatal(err)
log.Println(err)
}
if err := tbl.AddRow(2, "hello", 2.2, time.Now()); err != nil {
log.Fatal(err)
log.Println(err)
}

{ // client write data into GreptimeDB
resp, err := cli.Write(context.Background(), tbl)
if err != nil {
log.Fatal(err)
log.Println(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

{ // stream client send data into GreptimeDB
err := stream.Send(context.Background(), tbl)
if err != nil {
log.Fatal(err)
if err := stream.Send(context.Background(), tbl); err != nil {
log.Println(err)
}
}

Expand Down
56 changes: 56 additions & 0 deletions examples/tag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Insert data into GreptimeDB

## start GreptimeDB via Docker

```shell
docker run --rm -p 4000-4003:4000-4003 \
--name greptime greptime/greptimedb standalone start \
--http-addr 0.0.0.0:4000 \
--rpc-addr 0.0.0.0:4001 \
--mysql-addr 0.0.0.0:4002 \
--postgres-addr 0.0.0.0:4003
```

## Insert

```go
go run main.go
```

Output:

```log
2024/02/07 11:26:26 affected rows: 2
```

## Query

```shell
mysql -h 127.0.0.1 -P 4002 public
```

```shell
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.10-alpha-msql-proxy Greptime

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from monitors_with_tag;
+------+-----------+--------+------+-------------+---------+----------------------------+
| id | host | memory | cpu | temperature | running | ts |
+------+-----------+--------+------+-------------+---------+----------------------------+
| 1 | 127.0.0.1 | 1 | 1 | -1 | 1 | 2024-02-07 03:29:31.625000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-02-07 03:29:31.625000 |
+------+-----------+--------+------+-------------+---------+----------------------------+
2 rows in set (0.01 sec)
```
12 changes: 5 additions & 7 deletions examples/tag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func init() {

cli_, err := client.New(cfg)
if err != nil {
log.Fatal(err)
log.Panic(err)
}
cli = cli_

stream_, err := client.NewStreamClient(cfg)
if err != nil {
log.Fatal(err)
log.Panic(err)
}
stream = stream_
}
Expand All @@ -59,7 +59,6 @@ func (Monitor) TableName() string {
}

func main() {

monitors := []Monitor{
{
ID: 1,
Expand All @@ -82,17 +81,16 @@ func main() {
}

{ // client write data into GreptimeDB
_, err := cli.Create(context.Background(), monitors)
resp, err := cli.Create(context.Background(), monitors)
if err != nil {
log.Fatal(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

{ // stream client send data into GreptimeDB
err := stream.Create(context.Background(), monitors)
if err != nil {
if err := stream.Create(context.Background(), monitors); err != nil {
log.Fatal(err)
}
}

}

0 comments on commit ddd87b4

Please sign in to comment.