Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add More Examples #32

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ docker run --rm -p 4000-4003:4000-4003 \

## Examples

- [write table](table/README.md)
- [write object](object/README.md)
- [table](table/README.md)
- [object](object/README.md)

## Query

Your can using [MySQL Client](https://docs.greptime.com/user-guide/clients/mysql) to query the data from GreptimeDB.
37 changes: 13 additions & 24 deletions examples/object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,29 @@ go run main.go
Output:

```log
2024/02/07 11:26:26 affected rows: 2
2024/03/23 22:36:06 affected rows: 3
2024/03/23 22:36:06 affected rows: 3
2024/03/23 22:36:06 affected rows: 1
2024/03/23 22:36:06 affected rows: 3
2024/03/23 22:36:06 affected rows: 3
2024/03/23 22:36:06 affected rows: 1
```

## Query

```shell
2024/02/18 11:14:54 affected rows: 2
2024/02/18 11:14:54 affected rows: 2
```
Your can using [MySQL Client](https://docs.greptime.com/user-guide/clients/mysql) to query the data from GreptimeDB.

```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 -h 127.0.0.1 -P 4002 public

mysql> select * from monitors_with_tag;
+------+-----------+--------+------+-------------+---------+----------------------------+
| id | host | memory | cpu | temperature | running | ts |
+------+-----------+--------+------+-------------+---------+----------------------------+
| 1 | 127.0.0.1 | 1 | 1 | -1 | 1 | 2024-02-18 03:14:54.116000 |
| 1 | 127.0.0.1 | 1 | 1 | -1 | 1 | 2024-02-18 03:14:54.242000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-02-18 03:14:54.116000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-02-18 03:14:54.242000 |
| 1 | 127.0.0.1 | 1 | 1.1 | -1 | 1 | 2024-03-23 14:36:06.591000 |
| 1 | 127.0.0.1 | 1 | 1.1 | -1 | 1 | 2024-03-23 14:36:06.732000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-03-23 14:36:06.591000 |
| 2 | 127.0.0.2 | 2 | 2 | -2 | 1 | 2024-03-23 14:36:06.732000 |
+------+-----------+--------+------+-------------+---------+----------------------------+
4 rows in set (0.01 sec)
4 rows in set (0.12 sec)
```
63 changes: 56 additions & 7 deletions examples/object/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (Monitor) TableName() string {
return "monitors_with_tag"
}

func data() []Monitor {
func initData() []Monitor {
return []Monitor{
{
ID: 1,
Expand All @@ -70,25 +70,58 @@ func data() []Monitor {
Ts: time.Now(),
Running: true,
},
{
ID: 3,
Host: "127.0.0.3",
Memory: 3,
Cpu: 3.0,
Temperature: -3,
Ts: time.Now(),
Running: true,
},
}
}
func writeObject(data []Monitor) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

func writeObject() {
resp, err := client.WriteObject(ctx, data)
if err != nil {
log.Fatal(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

func deleteObject(data []Monitor) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

resp, err := client.WriteObject(ctx, data())
resp, err := client.DeleteObject(ctx, data)
if err != nil {
log.Fatal(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

func streamWriteObject() {
func streamWriteObject(data []Monitor) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

if err := client.StreamWriteObject(ctx, data); err != nil {
log.Println(err)
}
affected, err := client.CloseStream(ctx)
if err != nil {
log.Fatalln(err)
}
log.Printf("affected rows: %d\n", affected.GetValue())
}

func streamDeleteObject(data []Monitor) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

if err := client.StreamWriteObject(ctx, data()); err != nil {
if err := client.StreamDeleteObject(ctx, data); err != nil {
log.Println(err)
}
affected, err := client.CloseStream(ctx)
Expand All @@ -99,7 +132,23 @@ func streamWriteObject() {
}

func main() {
writeObject()
data := initData()
// insert
writeObject(data)
// update
data[0].Cpu = 1.1
writeObject(data)
// delete
deleteObject(data[2:])

time.Sleep(time.Millisecond * 100)
streamWriteObject()

data = initData()
// stream insert
streamWriteObject(data)
data[0].Cpu = 1.1
// stream update
streamWriteObject(data)
// stream delete
streamDeleteObject(data[2:])
}
35 changes: 11 additions & 24 deletions examples/table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,27 @@ go run main.go
Output:

```log
2024/02/18 11:06:20 affected rows: 2
2024/02/18 11:06:20 affected rows: 2
2024/03/23 23:04:19 affected rows: 3
2024/03/23 23:04:19 affected rows: 1
2024/03/23 23:04:19 affected rows: 1
2024/03/23 23:04:19 affected rows: 3
2024/03/23 23:04:19 affected rows: 1
2024/03/23 23:04:19 affected rows: 1
```

## 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 -h 127.0.0.1 -P 4002 public

mysql> select * from monitors_with_schema;
+------+-------+-------------+----------------------------+
| id | host | temperature | timestamp |
+------+-------+-------------+----------------------------+
| 1 | hello | 1.1 | 2024-02-18 03:12:05.033351 |
| 1 | hello | 1.1 | 2024-02-18 03:12:05.158068 |
| 2 | hello | 2.2 | 2024-02-18 03:12:05.033352 |
| 2 | hello | 2.2 | 2024-02-18 03:12:05.158076 |
| 1 | hello | 1.2 | 2024-03-23 15:04:19.631482 |
| 1 | hello | 1.2 | 2024-03-23 15:04:19.740767 |
| 2 | hello | 2.2 | 2024-03-23 15:04:19.631482 |
| 2 | hello | 2.2 | 2024-03-23 15:04:19.740768 |
+------+-------+-------------+----------------------------+
4 rows in set (0.01 sec)
```
127 changes: 112 additions & 15 deletions examples/table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"github.com/GreptimeTeam/greptimedb-ingester-go/table/types"
)

const (
INSERT = 0
UPDATE = 1
DELETE = 2
)

var (
client *greptime.Client
)
Expand All @@ -38,50 +44,126 @@ func init() {
client = cli_
}

func data() *table.Table {
tbl, err := table.New("monitors_with_schema")
func initData() []*table.Table {

time1 := time.Now()
time2 := time.Now()
time3 := time.Now()

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

if err := itbl.AddRow(1, "hello", 1.1, time1); err != nil {
log.Println(err)
}
if err := itbl.AddRow(2, "hello", 2.2, time2); err != nil {
log.Println(err)
}
if err := itbl.AddRow(3, "hello", 3.3, time3); err != nil {
log.Println(err)
}

utbl, err := table.New("monitors_with_schema")
if err != nil {
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 {
if err := utbl.AddTagColumn("id", types.INT64); err != nil {
log.Println(err)
}
if err := tbl.AddFieldColumn("host", types.STRING); err != nil {
if err := utbl.AddFieldColumn("host", types.STRING); err != nil {
log.Println(err)
}
if err := tbl.AddFieldColumn("temperature", types.FLOAT); err != nil {
if err := utbl.AddFieldColumn("temperature", types.FLOAT); err != nil {
log.Println(err)
}
if err := tbl.AddTimestampColumn("timestamp", types.TIMESTAMP_MICROSECOND); err != nil {
if err := utbl.AddTimestampColumn("timestamp", types.TIMESTAMP_MICROSECOND); err != nil {
log.Println(err)
}

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

dtbl, err := table.New("monitors_with_schema")
if err != nil {
log.Println(err)
}

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

if err := dtbl.AddRow(3, "hello", 3.3, time3); err != nil {
log.Println(err)
}

return []*table.Table{itbl, utbl, dtbl}
}

func write(data *table.Table) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
resp, err := client.Write(ctx, data)
if err != nil {
log.Println(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

func write() {
func delete(data *table.Table) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
resp, err := client.Write(ctx, data())
resp, err := client.Delete(ctx, data)
if err != nil {
log.Println(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

func streamWrite() {
func streamWrite(data *table.Table) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
if err := client.StreamWrite(ctx, data()); err != nil {
if err := client.StreamWrite(ctx, data); err != nil {
log.Println(err)
}
affected, err := client.CloseStream(ctx)
if err != nil {
log.Fatalln(err)
}
log.Printf("affected rows: %d\n", affected.GetValue())
}

func streamDelete(data *table.Table) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
if err := client.StreamDelete(ctx, data); err != nil {
log.Println(err)
}
affected, err := client.CloseStream(ctx)
Expand All @@ -92,7 +174,22 @@ func streamWrite() {
}

func main() {
write()
data := initData()
// insert
write(data[INSERT])
// update
write(data[UPDATE])
// delete
delete(data[DELETE])

time.Sleep(time.Millisecond * 100)
streamWrite()

data = initData()
// stream insert
streamWrite(data[INSERT])
// stream update
streamWrite(data[UPDATE])
// stream delete
streamDelete(data[DELETE])

}
Loading