# 支持日志数据写入提升数据处理效率，Grafana 插件使交互更友好 | Greptime 双周精选

Source: https://greptime.cn/blogs/2024-06-19-biweekly

## 内容概述

作为一个成长中的开源项目，GreptimeDB 的进展离不开来自全球的社区贡献者们，感谢各位！

最近的内容更新如下：
- **支持日志导入**：使用 Elastic Ingest Pipelines 配置来处理和转换日志数据，将带有类型信息的数据通过 Schemaless 的方式导入到数据库表中

- **实现 `SHOW CREATE FLOW`**：通过支持 `SHOW CREATE FLOW`，GreptimeFlow 正在逐渐完善生态

- **简化 Parquet Writer**：通过去除 Arrow 与 OpenDAL/S3 之间的冗余 Writer，减少 6% 的 Parquet 文件写入耗时

## 社区贡献者名单

在过去的两周里，GreptimeDB 共合并了 55 个 PR，其中有 5 位独立贡献者，累计 6 个 PR 被成功合并，还有很多待合并的 PR。

祝贺以下各位在过去 2 周内成为我们活跃的贡献者：

- @[cjwcommuny](https://github.com/cjwcommuny) ([db#4117](https://github.com/GreptimeTeam/greptimedb/pull/4117))

- @[irenjj](https://github.com/irenjj) ([db#4040](https://github.com/GreptimeTeam/greptimedb/pull/4040))

- @[realtaobo](https://github.com/realtaobo) ([db#4088](https://github.com/GreptimeTeam/greptimedb/pull/4088))

- @[WL2O2O](https://github.com/WL2O2O) ([dashboard#433](https://github.com/GreptimeTeam/dashboard/pull/433))

- @[yuanbohan](https://github.com/yuanbohan) ([db#4121](https://github.com/GreptimeTeam/greptimedb/pull/4121) [db#4123](https://github.com/GreptimeTeam/greptimedb/pull/4123))

👏 感谢各位独立贡献者持续提交并合并了多个 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

🎉 热烈欢迎 @[cjwcommuny](https://github.com/cjwcommuny) 和 @[WL2O2O](https://github.com/WL2O2O) 作为新的贡献者加入社区并成功合并了首个 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

<img src="/zh/blogs/2024-06-19-biweekly/contributorpost.webp" alt="Contributor of GreptimeDB" style="width: 80%; margin: 0 auto"/>

## PR 亮点
### [db#4014](https://github.com/GreptimeTeam/greptimedb/pull/4014) 支持日志导入

本 PR 引入了对日志导入的支持。我们使用 Elastic Ingest Pipelines 语法来定义处理和转换行为，我们称之为 Pipelines。将 Pipeline 模型定义上传到数据库后，我们就可以使用该 Pipeline 将日志处理成结构化数据并插入表中。

举例说明，我们可以创建一个如下的 Pipeline：

```shell
curl -X "POST" "http://localhost:4000/v1/events/pipelines/test" \
     -H 'Content-Type: application/x-yaml' \
     -d 'processors:
  - date:
      field: time
      formats:
        - "%Y-%m-%d %H:%M:%S%.3f"
      ignore_missing: true

transform:
  - fields:
      - id1
      - id2
    type: int32
  - fields:
      - type
      - log
      - logger
    type: string
  - field: time
    type: time
    index: timestamp
'
```

该接口同样支持将 Pipeline 内容放到一个文件中并直接上传整个文件。现在一个名为 test 的 Pipeline 在 `greptime_private.pipelines` 表中就创建好了。然后我们可以在数据库中插入一些日志数据：

```shell
curl -X "POST" "http://localhost:4000/v1/events/logs?db=public&table=logs1&pipeline_name=test" \
     -H 'Content-Type: application/json' \
     -d '[
    {
      "id1": "2436",
      "id2": "2528",
      "logger": "INTERACT.MANAGER",
      "type": "I",
      "time": "2024-05-25 20:16:37.217",
      "log": "ClusterAdapter:enter sendTextDataToCluster\\n"
    }
  ]'
```

该日志数据是 JSON 格式的。新的 `/v1/events/logs` 接口会查找 `pipeline_name` 参数指定的 Pipeline 来处理 Payload 数据。可以注意到日志数据的字段和定义的 Pipeline 相互关联。最后一个名为 `logs1` 的表就创建好了，带有类型信息的数据被插入到了该表中。

```shell
mysql> show create table logs1;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                    |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| logs1 | CREATE TABLE IF NOT EXISTS `logs1` (
  `id1` INT NULL,
  `id2` INT NULL,
  `type` STRING NULL,
  `log` STRING NULL,
  `logger` STRING NULL,
  `time` TIMESTAMP(9) NOT NULL,
  TIME INDEX (`time`)
)

ENGINE=mito
WITH(
  append_mode = 'true'
) |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> select * from logs1;
+------+------+------+---------------------------------------------+------------------+----------------------------+
| id1  | id2  | type | log                                         | logger           | time                       |
+------+------+------+---------------------------------------------+------------------+----------------------------+
| 2436 | 2528 | I    | ClusterAdapter:enter sendTextDataToCluster
 | INTERACT.MANAGER | 2024-05-25 20:16:37.217000 |
+------+------+------+---------------------------------------------+------------------+----------------------------+
1 row in set (0.03 sec)
```

### [db#4112](https://github.com/GreptimeTeam/greptimedb/pull/4112) 简化 Parquet Writer

最初引入 `BufferedWriter` 是为了解决 Arrow 的 `Parquet Writer` 需要 `std::io::Write`，而 OpenDAL 仅提供实现 `tokio::io::AsyncWrite` 的异步 S3 Writer 的类型不匹配问题。现在 Arrow 直接提供了 `AsyncArrowWriter`，我们就可以移除这个用于同步转异步的 `BufferedWriter` 结构。

通过移除这些冗余结构和额外的代码路径，我们在写入 Parquet 文件时的时间消耗减少了 6%。

### [db#4040](https://github.com/GreptimeTeam/greptimedb/pull/4040) 实现 `SHOW CREATE FLOW`

现在，我们可以在创建 Flow 后使用 `SHOW CREATE FLOW`。例如，如果我们创建了如下 Flow：

```shell
mysql> CREATE FLOW IF NOT EXISTS my_flow
    -> SINK TO my_sink_table
    -> EXPIRE AFTER INTERVAL '1 hour'
    -> AS
    -> SELECT count(1) from monitor;
Query OK, 0 rows affected (0.04 sec)
```

我们可以使用 `SHOW CREATE FLOW my_flow` 来检查创建的语句：

```sql
mysql> show create flow my_flow;
+---------+-----------------------------------------------------------------------------------------------------------------------+
| Flow    | Create Flow                                                                                                           |
+---------+-----------------------------------------------------------------------------------------------------------------------+
| my_flow | CREATE OR REPLACE FLOW IF NOT EXISTS my_flow
SINK TO my_sink_table
EXPIRE AFTER 3600
AS SELECT count(1) FROM monitor |
+---------+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

### [db#4151](https://github.com/GreptimeTeam/greptimedb/pull/4151) GreptimeDB 基准测试指南

现在，用户可以使用修改后版本的 tsbs 按照我们提供的指南去进行基准测试。这份指南也可以用于任何支持 tsbs 的数据库进行基准测试，从而生成比较结果。温馨提示，请确认使用 Release profile 构建的执行文件来运行基准测试。

基准测试指南：
https://github.com/GreptimeTeam/greptimedb/blob/main/docs/benchmarks/tsbs/README.md

## Good First Issue

### [db#4157 修复 `information_schema.region_peers` 返回相同 `region_id` 的问题](https://github.com/GreptimeTeam/greptimedb/issues/4157)

在 `information_schema.region_peer`s 中存储了 `region_id` 的情况下，即使有多个 region peers，似乎只返回了一个 `region_id`。现在需要查明是否在组装 `information_schema.region_peers` 的返回值时存在 bug 并修复它。

关键词：Information Schema

难度：简单

## 关于 Greptime

[Greptime 格睿科技](https://greptime.cn/)专注于为可观测、物联网及车联网等领域提供实时、高效的数据存储和分析服务，帮助客户挖掘数据的深层价值。目前基于云原生的时序数据库 [GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 已经衍生出多款适合不同用户的解决方案，更多信息或 demo 展示请联系下方小助手（微信号：greptime）。

欢迎对开源感兴趣的朋友们参与贡献和讨论，从带有 good first issue 标签的 issue 开始你的开源之旅吧～期待在开源社群里遇见你！添加小助手微信即可加入“技术交流群”与志同道合的朋友们面对面交流哦~

Star us on GitHub Now: https://github.com/GreptimeTeam/greptimedb

官网：https://greptime.cn/

文档：https://docs.greptime.cn/

Twitter: https://twitter.com/Greptime

Slack: https://greptime.com/slack

LinkedIn: https://www.linkedin.com/company/greptime/
