Skip to content

Commit

Permalink
refactor: modify example (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviderli614 authored Nov 19, 2024
1 parent c59df61 commit dd2c374
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 90 deletions.
33 changes: 25 additions & 8 deletions examples/healthcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/hints/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions examples/jsondata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions examples/object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ 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

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)
```
63 changes: 40 additions & 23 deletions examples/object/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -89,74 +101,79 @@ 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)
}
log.Printf("affected rows: %d\n", affected.GetValue())
}

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:])
}
2 changes: 1 addition & 1 deletion examples/opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/opentelemetry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions examples/table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
```
Loading

0 comments on commit dd2c374

Please sign in to comment.