-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Supports gRPC write hints (#48)
* feat: Supports gRPC write hints * chore: change table name * refactor: move to the context package * refactor: refactor the context package * refactor: init table * Update context/context.go Co-authored-by: zyy17 <[email protected]> --------- Co-authored-by: zyy17 <[email protected]>
- Loading branch information
1 parent
fa4d6c0
commit 5a19d5e
Showing
6 changed files
with
256 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package context | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
const ( | ||
hintPrefix = "x-greptime-hint-" | ||
) | ||
|
||
type Hint struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
type Option func(ctx context.Context) context.Context | ||
|
||
func New(parent context.Context, opts ...Option) context.Context { | ||
ctx := parent | ||
for _, opt := range opts { | ||
ctx = opt(ctx) | ||
} | ||
return ctx | ||
} | ||
|
||
func WithHints(hints []*Hint) Option { | ||
return func(ctx context.Context) context.Context { | ||
md := metadata.New(nil) | ||
for _, hint := range hints { | ||
md.Append(hintPrefix+hint.Key, hint.Value) | ||
} | ||
return metadata.NewOutgoingContext(ctx, md) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Write Hints | ||
|
||
## Insert | ||
|
||
```go | ||
go run main.go | ||
``` | ||
|
||
Output: | ||
|
||
```log | ||
2024/09/24 01:16:03 create table, name: 'monitor_table_with_hints' | ||
2024/09/24 01:16:03 affected rows: 3 | ||
``` | ||
|
||
## 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> select * from monitor_table_with_hints; | ||
+------+-------+-------------+----------------------------+ | ||
| id | host | temperature | timestamp | | ||
+------+-------+-------------+----------------------------+ | ||
| 1 | hello | 1.1 | 2024-09-23 17:16:03.476898 | | ||
| 2 | hello | 2.2 | 2024-09-23 17:16:03.476898 | | ||
| 3 | hello | 3.3 | 2024-09-23 17:16:03.476898 | | ||
+------+-------+-------------+----------------------------+ | ||
3 rows in set (0.03 sec) | ||
``` | ||
You can view hints using `show create table` command: | ||
```mysql | ||
mysql> show create table monitor_table_with_hints; | ||
+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| Table | Create Table | | ||
+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| monitor_table_with_hints | CREATE TABLE IF NOT EXISTS `monitor_table_with_hints` ( | ||
`id` BIGINT NULL, | ||
`host` STRING NULL, | ||
`temperature` DOUBLE NULL, | ||
`timestamp` TIMESTAMP(6) NOT NULL, | ||
TIME INDEX (`timestamp`), | ||
PRIMARY KEY (`id`) | ||
) | ||
|
||
ENGINE=mito | ||
WITH( | ||
append_mode = 'false', | ||
merge_mode = 'last_non_null', | ||
ttl = '3day' | ||
) | | ||
+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
1 row in set (0.02 sec) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2024 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
greptime "github.com/GreptimeTeam/greptimedb-ingester-go" | ||
|
||
ingesterContext "github.com/GreptimeTeam/greptimedb-ingester-go/context" | ||
"github.com/GreptimeTeam/greptimedb-ingester-go/table" | ||
"github.com/GreptimeTeam/greptimedb-ingester-go/table/types" | ||
) | ||
|
||
const ( | ||
// The GreptimeDB address. | ||
host = "127.0.0.1" | ||
|
||
// The database name. | ||
database = "public" | ||
) | ||
|
||
type client struct { | ||
client *greptime.Client | ||
} | ||
|
||
func newClient() (*client, error) { | ||
cfg := greptime.NewConfig(host).WithDatabase(database) | ||
gtClient, err := greptime.NewClient(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &client{ | ||
client: gtClient, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func initData() (*table.Table, error) { | ||
time1 := time.Now() | ||
time2 := time.Now() | ||
time3 := time.Now() | ||
|
||
itbl, err := table.New("monitor_table_with_hints") | ||
if err != nil { | ||
return nil, err | ||
} | ||
// add column at first. This is to define the schema of the table. | ||
if err := itbl.AddTagColumn("id", types.INT64); err != nil { | ||
return nil, err | ||
} | ||
if err := itbl.AddFieldColumn("host", types.STRING); err != nil { | ||
return nil, err | ||
} | ||
if err := itbl.AddFieldColumn("temperature", types.FLOAT); err != nil { | ||
return nil, err | ||
} | ||
if err := itbl.AddTimestampColumn("timestamp", types.TIMESTAMP_MICROSECOND); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := itbl.AddRow(1, "hello", 1.1, time1); err != nil { | ||
return nil, err | ||
} | ||
if err := itbl.AddRow(2, "hello", 2.2, time2); err != nil { | ||
return nil, err | ||
} | ||
if err := itbl.AddRow(3, "hello", 3.3, time3); err != nil { | ||
return nil, err | ||
} | ||
|
||
return itbl, nil | ||
} | ||
|
||
func (c client) write(data *table.Table) error { | ||
hints := []*ingesterContext.Hint{ | ||
{ | ||
Key: "ttl", | ||
Value: "3d", | ||
}, | ||
{ | ||
Key: "merge_mode", | ||
Value: "last_non_null", | ||
}, | ||
{ | ||
Key: "append_mode", | ||
Value: "false", | ||
}, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) | ||
defer cancel() | ||
|
||
resp, err := c.client.Write(ingesterContext.New(ctx, ingesterContext.WithHints(hints)), data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tableName, err := data.GetName() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("create table, name: '%s'", tableName) | ||
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue()) | ||
return nil | ||
} | ||
|
||
func main() { | ||
data, err := initData() | ||
if err != nil { | ||
log.Fatalf("failed to init data: %v:", err) | ||
} | ||
|
||
c, err := newClient() | ||
if err != nil { | ||
log.Fatalf("failed to new client: %v:", err) | ||
} | ||
|
||
if err = c.write(data); err != nil { | ||
log.Fatalf("failed to write data: %v:", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters