Skip to content

Commit

Permalink
docs: add JSON type for the gRPC SDKs (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicecui authored Nov 13, 2024
1 parent 6cea097 commit c984a1a
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 6 deletions.
3 changes: 1 addition & 2 deletions docs/reference/sql/data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ Output:

:::warning Limitation

1. The gRPC protocol does not support inserting JSON data through automatic table creation. However, you can manually create a table and insert JSON data by defining the JSON column as a String type.
2. The insertion of JSON data using MySQL protocol prepared statements is not supported.
The insertion of JSON data using MySQL protocol prepared statements is not supported.

:::

Expand Down
55 changes: 55 additions & 0 deletions docs/user-guide/ingest-data/for-iot/grpc-sdks/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,61 @@ affected, err := cli.CloseStream(ctx)

</div>

<div id="ingester-json-type">

In the [low-level API](#low-level-api),
you can specify the column type as `types.JSON` using the `AddFieldColumn` method to add a JSON column.
Then, use a `struct` or `map` to insert JSON data.

```go
sensorReadings, err := table.New("sensor_readings")
// The code for creating other columns is omitted
// ...
// specify the column type as JSON
sensorReadings.AddFieldColumn("attributes", types.JSON)

// Use struct to insert JSON data
type Attributes struct {
Location string `json:"location"`
Action string `json:"action"`
}
attributes := Attributes{ Location: "factory-1" }
sensorReadings.AddRow(<other-column-values>... , attributes)

// The following code for writing data is omitted
// ...
```

In the [high-level API](#high-level-api), you can specify the column type as JSON using the `greptime:"field;column:details;type:json"` tag.

```go
type SensorReadings struct {
// The code for creating other columns is omitted
// ...
// specify the column type as JSON
Attributes string `greptime:"field;column:details;type:json"`
// ...
}

// Use struct to insert JSON data
type Attributes struct {
Location string `json:"location"`
Action string `json:"action"`
}
attributes := Attributes{ Action: "running" }
sensor := SensorReadings{
// ...
Attributes: attributes,
}

// The following code for writing data is omitted
// ...
```

For the executable code for inserting JSON data, please refer to the [example](https://github.com/GreptimeTeam/greptimedb-ingester-go/tree/main/examples/jsondata) in the SDK repository.

</div>

<div id="ingester-lib-reference">

- [API Documentation](https://pkg.go.dev/github.com/GreptimeTeam/greptimedb-ingester-go)
Expand Down
51 changes: 51 additions & 0 deletions docs/user-guide/ingest-data/for-iot/grpc-sdks/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,57 @@ LOG.info("Write result: {}", result);

</div>

<div id="ingester-json-type">

In the [low-level API](#low-level-api),
you can specify the column type as `DataType.Json` using the `addField` method to add a JSON column.
Then use map to insert JSON data.

```java
// Construct the table schema for sensor_readings
TableSchema sensorReadings = TableSchema.newBuilder("sensor_readings")
// The code for creating other columns is omitted
// ...
// specify the column type as JSON
.addField("attributes", DataType.Json)
.build();

// ...
// Use map to insert JSON data
Map<String, Object> attr = new HashMap<>();
attr.put("location", "factory-1");
sensorReadings.addRow(<other-column-values>... , attr);

// The following code for writing data is omitted
// ...
```

In the [high-level API](#high-level-api), you can specify the column type as `DataType.Json` within the POJO object.

```java
@Metric(name = "sensor_readings")
public class Sensor {
// The code for creating other columns is omitted
// ...
// specify the column type as JSON
@Column(name = "attributes", dataType = DataType.Json)
private Map<String, Object> attributes;
// ...
}

Sensor sensor = new Sensor();
// ...
// Use map to insert JSON data
Map<String, Object> attr = new HashMap<>();
attr.put("action", "running");
sensor.setAttributes(attr);

// The following code for writing data is omitted
// ...
```

</div>

<div id="ingester-lib-debug-logs">

## Debug logs
Expand Down
11 changes: 10 additions & 1 deletion docs/user-guide/ingest-data/for-iot/grpc-sdks/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The following example shows how to set the username and password when using the
## Data model

Each row item in a table consists of three types of columns: `Tag`, `Timestamp`, and `Field`. For more information, see [Data Model](/user-guide/concepts/data-model.md).
The types of column values could be `String`, `Float`, `Int`, `Timestamp`, etc. For more information, see [Data Types](/reference/sql/data-types.md).
The types of column values could be `String`, `Float`, `Int`, `Timestamp`, `JSON` etc. For more information, see [Data Types](/reference/sql/data-types.md).

## Low-level API

Expand Down Expand Up @@ -88,6 +88,15 @@ Streaming insert is useful when you want to insert a large amount of data such a

<InjectContent id="high-level-style-update-data" content={props.children}/>

## Insert data in JSON type

GreptimeDB supports storing complex data structures using [JSON type data](/reference/sql/data-types.md#json-type).
With this ingester library, you can insert JSON data using string values.
For instance, if you have a table named `sensor_readings` and wish to add a JSON column named `attributes`,
refer to the following code snippet.

<InjectContent id="ingester-json-type" content={props.children}/>

<InjectContent id="ingester-lib-debug-logs" content={props.children}/>

## Ingester library reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ SELECT * FROM json_data;

:::warning 限制说明

1. gRPC 协议不支持通过自动建表写入 JSON 数据。不过,您可以手动创建表,并将 JSON 数据列设置为 String 类型进行写入。
2. 不支持通过 MySQL 协议预处理语句插入 JSON 数据。
不支持通过 MySQL 协议预处理语句插入 JSON 数据。

:::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,61 @@ affected, err := cli.CloseStream(ctx)

</div>

<div id="ingester-json-type">

[低层级 API](#低层级-api) 中,
你可以使用 `AddFieldColumn` 方法将列类型指定为 `types.JSON` 来添加 JSON 列。
然后使用 `struct``map` 插入 JSON 数据。

```go
sensorReadings, err := table.New("sensor_readings")
// 此处省略了创建其他列的代码
// ...
// 将列类型指定为 JSON
sensorReadings.AddFieldColumn("attributes", types.JSON)

// 使用 struct 插入 JSON 数据
type Attributes struct {
Location string `json:"location"`
Action string `json:"action"`
}
attributes := Attributes{ Location: "factory-1" }
sensorReadings.AddRow(<other-column-values>... , attributes)

// 以下省略了写入数据的代码
// ...
```

[高层级 API](#高层级-api) 中,你可以使用 `greptime:"field;column:details;type:json"` 标签将列类型指定为 JSON。

```go
type SensorReadings struct {
// 此处省略了创建其他列的代码
// ...
// 将列类型指定为 JSON
Attributes string `greptime:"field;column:details;type:json"`
// ...
}

// 使用 struct 插入 JSON 数据
type Attributes struct {
Location string `json:"location"`
Action string `json:"action"`
}
attributes := Attributes{ Action: "running" }
sensor := SensorReadings{
// ...
Attributes: attributes,
}

// 以下省略了写入数据的代码
// ...
```

请参考 SDK 仓库中的[示例](https://github.com/GreptimeTeam/greptimedb-ingester-go/tree/main/examples/jsondata) 获取插入 JSON 数据的可执行代码。

</div>

<div id="ingester-lib-reference">

- [API 文档](https://pkg.go.dev/github.com/GreptimeTeam/greptimedb-ingester-go)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,56 @@ LOG.info("Write result: {}", result);

</div>

<div id="ingester-json-type">

[低层级 API](#低层级-api) 中,
你可以使用 `addField` 方法将列类型指定为 `DataType.Json` 来添加 JSON 列,
然后使用 Map 对象添加 JSON 数据。

```java
// 为 sensor_readings 表构建表结构
TableSchema sensorReadings = TableSchema.newBuilder("sensor_readings")
// 此处省略了创建其他列的代码
// ...
// 将列类型指定为 JSON
.addField("attributes", DataType.Json)
.build();

// ...
// 使用 map 添加 JSON 数据
Map<String, Object> attr = new HashMap<>();
attr.put("location", "factory-1");
sensorReadings.addRow(<other-column-values>... , attr);

// 以下省略了写入数据的代码
// ...
```

[高层级 API](#高层级-api) 中,你可以在 POJO 对象中指定列类型为 `DataType.Json`

```java
@Metric(name = "sensor_readings")
public class Sensor {
// 此处省略了创建其他列的代码
// ...
// 将列类型指定为 JSON
@Column(name = "attributes", dataType = DataType.Json)
private Map<String, Object> attributes;
// ...
}

Sensor sensor = new Sensor();
// ...
// 使用 map 添加 JSON 数据
Map<String, Object> attr = new HashMap<>();
attr.put("action", "running");
sensor.setAttributes(attr);

// 以下省略了写入数据的代码
// ...
```

</div>

<div id="ingester-lib-debug-logs">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GreptimeDB 提供了用于高吞吐量数据写入的 ingester 库。
## 数据模型

表中的每条行数据包含三种类型的列:`Tag``Timestamp``Field`。更多信息请参考 [数据模型](/user-guide/concepts/data-model.md)
列值的类型可以是 `String``Float``Int``Timestamp` 等。更多信息请参考 [数据类型](/reference/sql/data-types.md)
列值的类型可以是 `String``Float``Int``JSON`, `Timestamp` 等。更多信息请参考 [数据类型](/reference/sql/data-types.md)

## 低层级 API

Expand Down Expand Up @@ -86,6 +86,15 @@ SDK 的高层级 API 使用 ORM 风格的对象写入数据,

<InjectContent id="high-level-style-update-data" content={props.children}/>

## 插入 JSON 类型的数据

GreptimeDB 支持使用 [JSON 类型数据](/reference/sql/data-types.md#json-类型) 存储复杂的数据结构。
使用此 ingester 库,你可以通过字符串值插入 JSON 数据。
假如你有一个名为 `sensor_readings` 的表,并希望添加一个名为 `attributes` 的 JSON 列,
请参考以下代码片段。

<InjectContent id="ingester-json-type" content={props.children}/>

<InjectContent id="ingester-lib-debug-logs" content={props.children}/>

## Ingester 库参考
Expand Down

0 comments on commit c984a1a

Please sign in to comment.