From dd2c374c919b8f2e02fba048c8bbfd6c5d7c3059 Mon Sep 17 00:00:00 2001 From: liyang Date: Tue, 19 Nov 2024 16:31:11 +0800 Subject: [PATCH] refactor: modify example (#53) --- examples/healthcheck/main.go | 33 +++++++++++++---- examples/hints/main.go | 2 +- examples/jsondata/main.go | 8 ++-- examples/object/README.md | 28 +++++++------- examples/object/main.go | 63 ++++++++++++++++++++------------ examples/opentelemetry/README.md | 2 +- examples/opentelemetry/main.go | 8 ++-- examples/table/README.md | 24 ++++++------ examples/table/main.go | 63 ++++++++++++++++++++------------ 9 files changed, 141 insertions(+), 90 deletions(-) diff --git a/examples/healthcheck/main.go b/examples/healthcheck/main.go index 1af3ce9..f10dd50 100644 --- a/examples/healthcheck/main.go +++ b/examples/healthcheck/main.go @@ -21,22 +21,39 @@ import ( greptime "github.com/GreptimeTeam/greptimedb-ingester-go" ) -var ( - client *greptime.Client +const ( + // The GreptimeDB address. + host = "127.0.0.1" + + // The database name. + database = "public" ) -func init() { - cfg := greptime.NewConfig("127.0.0.1").WithDatabase("public") +type client struct { + client *greptime.Client +} - cli_, err := greptime.NewClient(cfg) +func newClient() (*client, error) { + cfg := greptime.NewConfig(host).WithDatabase(database) + gtClient, err := greptime.NewClient(cfg) if err != nil { - log.Panic(err) + return nil, err + } + + c := &client{ + client: gtClient, } - client = cli_ + + return c, nil } func main() { - _, err := client.HealthCheck(context.Background()) + c, err := newClient() + if err != nil { + log.Fatalf("failed to new client: %v:", err) + } + + _, err = c.client.HealthCheck(context.Background()) if err != nil { log.Println("failed to health check:", err) return diff --git a/examples/hints/main.go b/examples/hints/main.go index d2fe3a4..772e924 100644 --- a/examples/hints/main.go +++ b/examples/hints/main.go @@ -88,7 +88,7 @@ func initData() (*table.Table, error) { return itbl, nil } -func (c client) write(data *table.Table) error { +func (c *client) write(data *table.Table) error { hints := []*ingesterContext.Hint{ { Key: "ttl", diff --git a/examples/jsondata/main.go b/examples/jsondata/main.go index 6a3c41c..4371291 100644 --- a/examples/jsondata/main.go +++ b/examples/jsondata/main.go @@ -68,12 +68,12 @@ func main() { if err != nil { log.Fatalf("failed to init data: %v:", err) } - if err = c.write(data[0]); err != nil { + if err = c.write(data); err != nil { log.Fatalf("failed to write data: %v:", err) } } -func initData() ([]*table.Table, error) { +func initData() (*table.Table, error) { time1 := time.Now() itbl, err := table.New("json_data") @@ -99,10 +99,10 @@ func initData() ([]*table.Table, error) { return nil, err } - return []*table.Table{itbl}, nil + return itbl, nil } -func (c client) write(data *table.Table) error { +func (c *client) write(data *table.Table) error { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() resp, err := c.client.Write(ctx, data) diff --git a/examples/object/README.md b/examples/object/README.md index a459788..fa019ea 100644 --- a/examples/object/README.md +++ b/examples/object/README.md @@ -9,12 +9,12 @@ go run main.go Output: ```log -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 +2024/11/13 19:15:23 affected rows: 4 +2024/11/13 19:15:23 affected rows: 4 +2024/11/13 19:15:23 affected rows: 1 +2024/11/13 19:15:23 affected rows: 4 +2024/11/13 19:15:23 affected rows: 4 +2024/11/13 19:15:23 affected rows: 1 ``` ## Query @@ -22,18 +22,18 @@ Output: Your can using [MySQL Client](https://docs.greptime.com/user-guide/protocols/mysql) to query the data from GreptimeDB. ```shell -$ mysql -h 127.0.0.1 -P 4002 public +$ mysql -h 127.0.0.1 -P 4002 mysql> select * from monitors_with_tag; +------+-----------+--------+------+-------------+---------+----------------------------+ | id | host | memory | cpu | temperature | running | ts | +------+-----------+--------+------+-------------+---------+----------------------------+ -| 0 | 127.0.0.1 | 1 | 1.3 | -1 | 0 | 2024-08-14 09:20:04.475000 | -| 0 | 127.0.0.1 | 1 | 1.3 | -1 | 0 | 2024-08-14 09:20:04.664000 | -| 1 | 127.0.0.2 | 1 | 1.1 | -1 | 1 | 2024-08-14 09:20:04.475000 | -| 1 | 127.0.0.2 | 1 | 1.1 | -1 | 1 | 2024-08-14 09:20:04.664000 | -| 2 | 127.0.0.3 | 2 | 2 | -2 | 1 | 2024-08-14 09:20:04.475000 | -| 2 | 127.0.0.3 | 2 | 2 | -2 | 1 | 2024-08-14 09:20:04.664000 | +| 0 | 127.0.0.1 | 1 | 1.3 | -1 | 0 | 2024-11-13 11:15:23.420000 | +| 0 | 127.0.0.1 | 1 | 1.3 | -1 | 0 | 2024-11-13 11:15:23.637000 | +| 1 | 127.0.0.2 | 1 | 1.1 | -1 | 1 | 2024-11-13 11:15:23.420000 | +| 1 | 127.0.0.2 | 1 | 1.1 | -1 | 1 | 2024-11-13 11:15:23.637000 | +| 2 | 127.0.0.3 | 2 | 2 | -2 | 1 | 2024-11-13 11:15:23.420000 | +| 2 | 127.0.0.3 | 2 | 2 | -2 | 1 | 2024-11-13 11:15:23.637000 | +------+-----------+--------+------+-------------+---------+----------------------------+ -6 rows in set (0.03 sec) +6 rows in set (0.07 sec) ``` diff --git a/examples/object/main.go b/examples/object/main.go index 0f6b0d8..6057330 100644 --- a/examples/object/main.go +++ b/examples/object/main.go @@ -22,18 +22,30 @@ import ( greptime "github.com/GreptimeTeam/greptimedb-ingester-go" ) -var ( - client *greptime.Client +const ( + // The GreptimeDB address. + host = "127.0.0.1" + + // The database name. + database = "public" ) -func init() { - cfg := greptime.NewConfig("127.0.0.1").WithDatabase("public") +type client struct { + client *greptime.Client +} - cli_, err := greptime.NewClient(cfg) +func newClient() (*client, error) { + cfg := greptime.NewConfig(host).WithDatabase(database) + gtClient, err := greptime.NewClient(cfg) if err != nil { - log.Panic(err) + return nil, err + } + + c := &client{ + client: gtClient, } - client = cli_ + + return c, nil } type Monitor struct { @@ -89,50 +101,50 @@ func initData() []Monitor { } } -func writeObject(data []Monitor) { +func (c *client) writeObject(data []Monitor) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - resp, err := client.WriteObject(ctx, data) + resp, err := c.client.WriteObject(ctx, data) if err != nil { log.Fatal(err) } log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue()) } -func deleteObject(data []Monitor) { +func (c *client) deleteObject(data []Monitor) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - resp, err := client.DeleteObject(ctx, data) + resp, err := c.client.DeleteObject(ctx, data) if err != nil { log.Fatal(err) } log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue()) } -func streamWriteObject(data []Monitor) { +func (c *client) streamWriteObject(data []Monitor) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - if err := client.StreamWriteObject(ctx, data); err != nil { + if err := c.client.StreamWriteObject(ctx, data); err != nil { log.Println(err) } - affected, err := client.CloseStream(ctx) + affected, err := c.client.CloseStream(ctx) if err != nil { log.Fatalln(err) } log.Printf("affected rows: %d\n", affected.GetValue()) } -func streamDeleteObject(data []Monitor) { +func (c *client) streamDeleteObject(data []Monitor) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - if err := client.StreamDeleteObject(ctx, data); err != nil { + if err := c.client.StreamDeleteObject(ctx, data); err != nil { log.Println(err) } - affected, err := client.CloseStream(ctx) + affected, err := c.client.CloseStream(ctx) if err != nil { log.Fatalln(err) } @@ -140,23 +152,28 @@ func streamDeleteObject(data []Monitor) { } func main() { + c, err := newClient() + if err != nil { + log.Fatalf("failed to new client: %v:", err) + } + data := initData() // insert - writeObject(data) + c.writeObject(data) // update data[1].Cpu = 1.1 - writeObject(data) + c.writeObject(data) // delete - deleteObject(data[3:]) + c.deleteObject(data[3:]) time.Sleep(time.Millisecond * 100) data = initData() // stream insert - streamWriteObject(data) + c.streamWriteObject(data) data[1].Cpu = 1.1 // stream update - streamWriteObject(data) + c.streamWriteObject(data) // stream delete - streamDeleteObject(data[3:]) + c.streamDeleteObject(data[3:]) } diff --git a/examples/opentelemetry/README.md b/examples/opentelemetry/README.md index a356de7..6bb6717 100644 --- a/examples/opentelemetry/README.md +++ b/examples/opentelemetry/README.md @@ -22,7 +22,7 @@ docker run --rm --name jaeger \ Write the [Prometheus](https://prometheus.io/) configuration file and save it as **prometheus.yml**: ``` global: - scrape_interval: 10s + scrape_interval: 5s scrape_configs: - job_name: greptimedb-ingester-go diff --git a/examples/opentelemetry/main.go b/examples/opentelemetry/main.go index 6a50eae..efd227a 100644 --- a/examples/opentelemetry/main.go +++ b/examples/opentelemetry/main.go @@ -207,7 +207,7 @@ func initData() ([]*table.Table, error) { time2 := time.Now() time3 := time.Now() - itbl, err := table.New("monitors_with_schema") + itbl, err := table.New("monitors_with_otel") if err != nil { return nil, err } @@ -235,7 +235,7 @@ func initData() ([]*table.Table, error) { return nil, err } - utbl, err := table.New("monitors_with_schema") + utbl, err := table.New("monitors_with_otel") if err != nil { return nil, err } @@ -258,7 +258,7 @@ func initData() ([]*table.Table, error) { return nil, err } - dtbl, err := table.New("monitors_with_schema") + dtbl, err := table.New("monitors_with_otel") if err != nil { return nil, err } @@ -284,7 +284,7 @@ func initData() ([]*table.Table, error) { return []*table.Table{itbl, utbl, dtbl}, nil } -func (c client) write(data *table.Table) error { +func (c *client) write(data *table.Table) error { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() resp, err := c.client.Write(ctx, data) diff --git a/examples/table/README.md b/examples/table/README.md index fdbee52..5bef35b 100644 --- a/examples/table/README.md +++ b/examples/table/README.md @@ -9,12 +9,12 @@ go run main.go Output: ```log -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 +2024/11/13 19:39:37 affected rows: 3 +2024/11/13 19:39:37 affected rows: 1 +2024/11/13 19:39:37 affected rows: 1 +2024/11/13 19:39:37 affected rows: 3 +2024/11/13 19:39:37 affected rows: 1 +2024/11/13 19:39:37 affected rows: 1 ``` ## Query @@ -26,10 +26,12 @@ mysql> select * from monitors_with_schema; +------+-------+-------------+----------------------------+ | id | host | temperature | timestamp | +------+-------+-------------+----------------------------+ -| 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 | +| 1 | hello | 1.1 | 2024-11-13 11:39:37.237320 | +| 1 | hello | 1.1 | 2024-11-13 11:39:37.417887 | +| 2 | hello | 2.2 | 2024-11-13 11:39:37.237320 | +| 2 | hello | 2.2 | 2024-11-13 11:39:37.417887 | +| 3 | hello | 3.3 | 2024-11-13 11:39:37.237320 | +| 3 | hello | 3.3 | 2024-11-13 11:39:37.417887 | +------+-------+-------------+----------------------------+ -4 rows in set (0.01 sec) +6 rows in set (0.03 sec) ``` diff --git a/examples/table/main.go b/examples/table/main.go index 8c5d72b..4906163 100644 --- a/examples/table/main.go +++ b/examples/table/main.go @@ -28,20 +28,30 @@ const ( INSERT = 0 UPDATE = 1 DELETE = 2 -) -var ( - client *greptime.Client + // The GreptimeDB address. + host = "127.0.0.1" + + // The database name. + database = "public" ) -func init() { - cfg := greptime.NewConfig("127.0.0.1").WithDatabase("public") +type client struct { + client *greptime.Client +} - cli_, err := greptime.NewClient(cfg) +func newClient() (*client, error) { + cfg := greptime.NewConfig(host).WithDatabase(database) + gtClient, err := greptime.NewClient(cfg) if err != nil { - log.Panic(err) + return nil, err } - client = cli_ + + c := &client{ + client: gtClient, + } + + return c, nil } func initData() []*table.Table { @@ -127,46 +137,46 @@ func initData() []*table.Table { return []*table.Table{itbl, utbl, dtbl} } -func write(data *table.Table) { +func (c *client) write(data *table.Table) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - resp, err := client.Write(ctx, data) + resp, err := c.client.Write(ctx, data) if err != nil { log.Println(err) } log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue()) } -func delete(data *table.Table) { +func (c *client) delete(data *table.Table) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - resp, err := client.Delete(ctx, data) + resp, err := c.client.Delete(ctx, data) if err != nil { log.Println(err) } log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue()) } -func streamWrite(data *table.Table) { +func (c *client) 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 := c.client.StreamWrite(ctx, data); err != nil { log.Println(err) } - affected, err := client.CloseStream(ctx) + affected, err := c.client.CloseStream(ctx) if err != nil { log.Fatalln(err) } log.Printf("affected rows: %d\n", affected.GetValue()) } -func streamDelete(data *table.Table) { +func (c *client) streamDelete(data *table.Table) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() - if err := client.StreamDelete(ctx, data); err != nil { + if err := c.client.StreamDelete(ctx, data); err != nil { log.Println(err) } - affected, err := client.CloseStream(ctx) + affected, err := c.client.CloseStream(ctx) if err != nil { log.Fatalln(err) } @@ -174,22 +184,27 @@ func streamDelete(data *table.Table) { } func main() { + c, err := newClient() + if err != nil { + log.Fatalf("failed to new client: %v:", err) + } + data := initData() // insert - write(data[INSERT]) + c.write(data[INSERT]) // update - write(data[UPDATE]) + c.write(data[UPDATE]) // delete - delete(data[DELETE]) + c.delete(data[DELETE]) time.Sleep(time.Millisecond * 100) data = initData() // stream insert - streamWrite(data[INSERT]) + c.streamWrite(data[INSERT]) // stream update - streamWrite(data[UPDATE]) + c.streamWrite(data[UPDATE]) // stream delete - streamDelete(data[DELETE]) + c.streamDelete(data[DELETE]) }