Skip to content

Commit

Permalink
refactor: use write object interface name (#24)
Browse files Browse the repository at this point in the history
* use write object interface name

* bump version
  • Loading branch information
yuanbohan authored Feb 27, 2024
1 parent 2da0829 commit a16fc70
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 68 deletions.
94 changes: 46 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ go get -u github.com/GreptimeTeam/greptimedb-ingester-go

```go
import greptime "github.com/GreptimeTeam/greptimedb-ingester-go"

```

### Example

#### Config
### Config

Initiate a Config for Client

Expand All @@ -33,53 +30,26 @@ cfg := greptime.NewConfig("<host>").
WithDatabase("<database>")
```

#### Client
### Client

```go
cli, err := greptime.NewClient(cfg)
```

#### Insert & StreamInsert
### Insert & StreamInsert

- you can Insert data into GreptimeDB via different style:
- [Table/Column/Row style](#with-schema-predefined)

- [Table style](#with-table)
- [ORM style](#with-struct-tag)

- streaming insert is to Send data into GreptimeDB without waiting for response.

##### Datatypes supported

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
#### With Table

you can define schema via Table and Column, and then AddRow to include the real data you want to write.

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

```go
import(
Expand All @@ -98,25 +68,25 @@ err := tbl.AddRow(2, "127.0.0.2", time.Now())
...
```

###### Write into GreptimeDB
##### Write into GreptimeDB

```go
resp, err := cli.Write(context.Background(), tbl)
```

###### Stream Write into GreptimeDB
##### Stream Write into GreptimeDB

```go
err := cli.StreamWrite(context.Background(), tbl)
...
affected, err := cli.CloseStream(ctx)
```

##### With Struct Tag
#### With Struct Tag

If you prefer ORM style, and define column-field relationship via struct field tag, you can try the following way.

###### Tag
##### Tag

- `greptime` is the struct tag key
- `tag`, `field`, `timestamp` is for [SemanticType][data-model], and the value is ignored
Expand All @@ -126,7 +96,7 @@ If you prefer ORM style, and define column-field relationship via struct field t

type supported is the same as described [Datatypes supported](#datatypes-supported), and case insensitive

###### define struct with tags
##### define struct with tags

```go
type Monitor struct {
Expand All @@ -141,7 +111,7 @@ func (Monitor) TableName() string {
}
```

###### instance your struct
##### instance your struct

```go
monitors := []Monitor{
Expand All @@ -158,21 +128,49 @@ monitors := []Monitor{
}
```

###### Create into GreptimeDB
##### WriteObject into GreptimeDB

```go
resp, err := cli.Create(context.Background(), monitors)
resp, err := cli.WriteObject(context.Background(), monitors)
```

###### Stream Create into GreptimeDB
##### Stream WriteObject into GreptimeDB

```go
err := cli.StreamCreate(context.Background(), monitors)
err := cli.StreamWriteObject(context.Background(), monitors)
...
affected, err := cli.CloseStream(ctx)
```

#### Query
## Datatypes supported

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

## Query

You can use ORM library like [gorm][gorm] with MySQL or PostgreSQL driver to [connect][connect] GreptimeDB and retrieve data from it.

Expand Down
16 changes: 8 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Client) Write(ctx context.Context, tables ...*table.Table) (*gpb.Grepti
return c.client.Handle(ctx, request_)
}

// Create is like [Write] to write the data into GreptimeDB, but schema is defined in the struct tag.
// WriteObject is like [Write] to write the data into GreptimeDB, but schema is defined in the struct tag.
//
// type Monitor struct {
// ID int64 `greptime:"tag;column:id;type:int64"`
Expand Down Expand Up @@ -108,9 +108,9 @@ func (c *Client) Write(ctx context.Context, tables ...*table.Table) (*gpb.Grepti
// },
// }
//
// resp, err := client.Create(context.Background(), monitors)
func (c *Client) Create(ctx context.Context, body any) (*gpb.GreptimeResponse, error) {
tbl, err := schema.Parse(body)
// resp, err := client.WriteObject(context.Background(), monitors)
func (c *Client) WriteObject(ctx context.Context, obj any) (*gpb.GreptimeResponse, error) {
tbl, err := schema.Parse(obj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (c *Client) StreamWrite(ctx context.Context, tables ...*table.Table) error
return c.stream.Send(request_)
}

// StreamCreate is like [StreamWrite] to send the data into GreptimeDB, but schema is defined in the struct tag.
// StreamWriteObject is like [StreamWrite] to send the data into GreptimeDB, but schema is defined in the struct tag.
//
// type monitor struct {
// ID int64 `greptime:"tag;column:id;type:int64"`
Expand Down Expand Up @@ -187,8 +187,8 @@ func (c *Client) StreamWrite(ctx context.Context, tables ...*table.Table) error
// },
// }
//
// resp, err := client.StreamCreate(context.Background(), monitors)
func (c *Client) StreamCreate(ctx context.Context, body any) error {
// resp, err := client.StreamWriteObject(context.Background(), monitors)
func (c *Client) StreamWriteObject(ctx context.Context, body any) error {
tbl, err := schema.Parse(body)
if err != nil {
return err
Expand All @@ -197,7 +197,7 @@ func (c *Client) StreamCreate(ctx context.Context, body any) error {
}

// CloseStream closes the stream. Once we’ve finished writing our client’s requests to the stream
// using client.StreamWrite or client.StreamCreate, we need to call client.CloseStream to let
// using client.StreamWrite or client.StreamWriteObject, we need to call client.CloseStream to let
// GreptimeDB know that we’ve finished writing and are expecting to receive a response.
func (c *Client) CloseStream(ctx context.Context) (*gpb.AffectedRows, error) {
resp, err := c.stream.CloseAndRecv()
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func TestCreateMonitors(t *testing.T) {
},
}

resp, err := cli.Create(context.Background(), monitors)
resp, err := cli.WriteObject(context.Background(), monitors)
assert.Nil(t, err)
assert.Zero(t, resp.GetHeader().GetStatus().GetStatusCode())
assert.Empty(t, resp.GetHeader().GetStatus().GetErrMsg())
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestStreamCreate(t *testing.T) {
},
}

err = cli.StreamCreate(context.Background(), monitors)
err = cli.StreamWriteObject(context.Background(), monitors)
assert.Nil(t, err)
affected, err := cli.CloseStream(context.Background())
assert.EqualValues(t, 2, affected.GetValue())
Expand Down
6 changes: 3 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ docker run --rm -p 4000-4003:4000-4003 \
--postgres-addr 0.0.0.0:4003
```

## Insert
## Examples

- [schema](schema/README.md)
- [tag](tag/README.md)
- [write table](table/README.md)
- [write object](object/README.md)
File renamed without changes.
12 changes: 6 additions & 6 deletions examples/tag/main.go → examples/object/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ func data() []Monitor {
}
}

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

func streamCreate() {
func streamWriteObject() {
ctx := context.Background()
if err := client.StreamCreate(ctx, data()); err != nil {
if err := client.StreamWriteObject(ctx, data()); err != nil {
log.Println(err)
}
affected, err := client.CloseStream(ctx)
Expand All @@ -94,7 +94,7 @@ func streamCreate() {
}

func main() {
create()
writeObject()
time.Sleep(time.Millisecond * 100)
streamCreate()
streamWriteObject()
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package greptime

const Version = "v0.2.0" // THIS MUST BE THE SAME AS THE VERSION in GitHub release
const Version = "v0.3.0" // THIS MUST BE THE SAME AS THE VERSION in GitHub release

0 comments on commit a16fc70

Please sign in to comment.