diff --git a/home/docs/start/greptime-init.md b/home/docs/start/greptime-init.md
index d2b73d39068..f4a0ccbdde9 100644
--- a/home/docs/start/greptime-init.md
+++ b/home/docs/start/greptime-init.md
@@ -8,7 +8,7 @@ Apache HertzBeat (incubating)'s historical data storage relies on the time serie
> It is recommended to use VictoriaMetrics as metrics storage.
-GreptimeDB is an open-source time-series database with a special focus on scalability, analytical capabilities and efficiency.
+[GreptimeDB](https://github.com/GreptimeTeam/greptimedb) is an open-source time-series database with a special focus on scalability, analytical capabilities and efficiency.
It's designed to work on infrastructure of the cloud era, and users benefit from its elasticity and commodity storage.
@@ -26,21 +26,24 @@ It's designed to work on infrastructure of the cloud era, and users benefit from
2. Install GreptimeDB with Docker
```shell
-$ docker run -p 4000-4004:4000-4004 \
- -p 4242:4242 -v /opt/greptimedb:/tmp/greptimedb \
- --name greptime \
- greptime/greptimedb standalone start \
- --http-addr 0.0.0.0:4000 \
- --rpc-addr 0.0.0.0:4001 \
+$ docker run -p 127.0.0.1:4000-4003:4000-4003 \
+ -v "$(pwd)/greptimedb:/tmp/greptimedb" \
+ --name greptime --rm \
+ greptime/greptimedb:latest standalone start \
+ --http-addr 0.0.0.0:4000 \
+ --rpc-addr 0.0.0.0:4001 \
+ --mysql-addr 0.0.0.0:4002 \
+ --postgres-addr 0.0.0.0:4003
```
- `-v /opt/greptimedb:/tmp/greptimedb` is local persistent mount of greptimedb data directory. `/opt/greptimedb` should be replaced with the actual local directory.
+
+ `-v "$(pwd)/greptimedb:/tmp/greptimedb"` is local persistent mount of greptimedb data directory. `$(pwd)/greptimedb` should be replaced with the actual local directory, default is the `greptimedb` directory under the current directory.
use```$ docker ps``` to check if the database started successfully
### Configure the database connection in hertzbeat `application.yml` configuration file
1. Configure HertzBeat's configuration file
- Modify `hertzbeat/config/application.yml` configuration file
- Note⚠️The docker container way need to mount application.yml file locally, while you can use installation package way to unzip and modify `hertzbeat/config/application.yml`
+ Modify `hertzbeat/config/application.yml` configuration file [/script/application.yml](https://github.com/apache/hertzbeat/raw/master/script/application.yml)
+ Note⚠️The docker container way need to mount application.yml file locally, while you can use installation package way to unzip and modify `hertzbeat/config/application.yml`
Replace `warehouse.store.greptime` data source parameters, URL account and password.
```yaml
@@ -52,9 +55,15 @@ warehouse:
# enable greptime
greptime:
enabled: true
- endpoint: localhost:4001
+ grpc-endpoints: localhost:4001
+ url: jdbc:mysql://localhost:4002/hertzbeat?connectionTimeZone=Asia/Shanghai&forceConnectionTimeZoneToSession=true
+ driver-class-name: com.mysql.cj.jdbc.Driver
+ username: greptime
+ password: greptime
```
+The default database is `hertzbeat` in the `url`.
+
2. Restart HertzBeat
### FAQ
diff --git a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md
index 5b3279b1f40..ab99977867e 100644
--- a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md
+++ b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md
@@ -1,14 +1,14 @@
---
id: greptime-init
-title: 依赖时序数据库服务GreptimeDB安装初始化(可选)
-sidebar_label: 指标数据存储GreptimeDB
+title: 依赖时序数据库服务 GreptimeDB 安装初始化(可选)
+sidebar_label: 指标数据存储 GreptimeDB
---
Apache HertzBeat (incubating) 的历史数据存储依赖时序数据库,任选其一安装初始化即可,也可不安装(注意⚠️但强烈建议生产环境配置)
> 我们推荐使用并长期支持 VictoriaMetrics 作为存储。
-GreptimeDB is an open-source time-series database with a special focus on scalability, analytical capabilities and efficiency.
+[GreptimeDB](https://github.com/GreptimeTeam/greptimedb) is an open-source time-series database with a special focus on scalability, analytical capabilities and efficiency.
It's designed to work on infrastructure of the cloud era, and users benefit from its elasticity and commodity storage.
**⚠️ 若不配置时序数据库,则只会留最近一小时历史数据**
@@ -25,21 +25,24 @@ It's designed to work on infrastructure of the cloud era, and users benefit from
2. Docker安装GreptimeDB
```shell
-$ docker run -p 4000-4004:4000-4004 \
- -p 4242:4242 -v /opt/greptimedb:/tmp/greptimedb \
- --name greptime \
- greptime/greptimedb standalone start \
- --http-addr 0.0.0.0:4000 \
- --rpc-addr 0.0.0.0:4001
+$ docker run -p 127.0.0.1:4000-4003:4000-4003 \
+ -v "$(pwd)/greptimedb:/tmp/greptimedb" \
+ --name greptime --rm \
+ greptime/greptimedb:latest standalone start \
+ --http-addr 0.0.0.0:4000 \
+ --rpc-addr 0.0.0.0:4001 \
+ --mysql-addr 0.0.0.0:4002 \
+ --postgres-addr 0.0.0.0:4003
```
- `-v /opt/greptimedb:/tmp/greptimedb` 为greptimedb数据目录本地持久化挂载,需将`/opt/greptimedb`替换为实际本地存在的目录
+ `-v "$(pwd)/greptimedb:/tmp/greptimedb` 为 greptimedb 数据目录本地持久化挂载,需将 `$(pwd)/greptimedb` 替换为实际本地存在的目录,默认使用执行命令的当前目录下的 `greptimedb` 目录作为数据目录。
+
使用```$ docker ps```查看数据库是否启动成功
### 在hertzbeat的`application.yml`配置文件配置此数据库连接
1. 配置HertzBeat的配置文件
- 修改位于 `hertzbeat/config/application.yml` 的配置文件
+ 修改位于 `hertzbeat/config/application.yml` 的配置文件 [/script/application.yml](https://github.com/apache/hertzbeat/raw/master/script/application.yml)
注意⚠️docker容器方式需要将application.yml文件挂载到主机本地,安装包方式解压修改位于 `hertzbeat/config/application.yml` 即可
**修改里面的`warehouse.store.jpa.enabled`参数为`false`, 配置里面的`warehouse.store.greptime`数据源参数,URL账户密码,并启用`enabled`为`true`**
@@ -52,9 +55,15 @@ warehouse:
enabled: false
greptime:
enabled: true
- endpoint: localhost:4001
+ grpc-endpoints: localhost:4001
+ url: jdbc:mysql://localhost:4002/hertzbeat?connectionTimeZone=Asia/Shanghai&forceConnectionTimeZoneToSession=true
+ driver-class-name: com.mysql.cj.jdbc.Driver
+ username: greptime
+ password: greptime
```
+默认数据库是 URL 中配置的 `hertzbeat` 。
+
2. 重启 HertzBeat
### 常见问题
diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml
index e086e4952de..053008a8154 100644
--- a/manager/src/main/resources/application.yml
+++ b/manager/src/main/resources/application.yml
@@ -141,7 +141,11 @@ warehouse:
password: taosdata
greptime:
enabled: false
- endpoint: localhost:4001
+ grpc-endpoints: localhost:4001
+ url: jdbc:mysql://localhost:4002/hertzbeat?connectionTimeZone=Asia/Shanghai&forceConnectionTimeZoneToSession=true
+ driver-class-name: com.mysql.cj.jdbc.Driver
+ username: greptime
+ password: greptime
iot-db:
enabled: false
host: 127.0.0.1
diff --git a/warehouse/pom.xml b/warehouse/pom.xml
index c89770e3755..0db9386496d 100644
--- a/warehouse/pom.xml
+++ b/warehouse/pom.xml
@@ -29,7 +29,8 @@
2.23
3.0.5
3.0.0
- 0.4.0
+ 0.7.3
+ 8.0.33
4.0.0
@@ -81,10 +82,10 @@
influxdb-java
${influxdb.version}
-
+
io.greptime
- greptimedb-protocol
+ ingester-all
${greptimedb.version}
@@ -105,28 +106,11 @@
+
- io.greptime
- greptimedb-grpc
- ${greptimedb.version}
-
-
- io.grpc
- grpc-all
-
-
- com.google.code.gson
- gson
-
-
- com.google.guava
- guava
-
-
- com.google.protobuf
- protobuf-java
-
-
+ mysql
+ mysql-connector-java
+ ${mysql-jdbcdriver.version}
diff --git a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GrepTimeDbDataStorage.java b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GrepTimeDbDataStorage.java
deleted file mode 100644
index 4109cf025e9..00000000000
--- a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GrepTimeDbDataStorage.java
+++ /dev/null
@@ -1,427 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.hertzbeat.warehouse.store.history.greptime;
-
-import io.greptime.GreptimeDB;
-import io.greptime.models.ColumnDataType;
-import io.greptime.models.Err;
-import io.greptime.models.QueryOk;
-import io.greptime.models.QueryRequest;
-import io.greptime.models.Result;
-import io.greptime.models.Row;
-import io.greptime.models.SelectExprType;
-import io.greptime.models.SelectRows;
-import io.greptime.models.SemanticType;
-import io.greptime.models.TableName;
-import io.greptime.models.TableSchema;
-import io.greptime.models.WriteOk;
-import io.greptime.models.WriteRows;
-import io.greptime.options.GreptimeOptions;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
-import java.time.Duration;
-import java.time.ZonedDateTime;
-import java.time.temporal.TemporalAmount;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.arrow.flight.FlightRuntimeException;
-import org.apache.hertzbeat.common.constants.CommonConstants;
-import org.apache.hertzbeat.common.entity.dto.Value;
-import org.apache.hertzbeat.common.entity.message.CollectRep;
-import org.apache.hertzbeat.common.util.JsonUtil;
-import org.apache.hertzbeat.common.util.TimePeriodUtil;
-import org.apache.hertzbeat.warehouse.store.history.AbstractHistoryDataStorage;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.stereotype.Component;
-
-/**
- * greptimeDB data storage
- */
-@Component
-@ConditionalOnProperty(prefix = "warehouse.store.greptime", name = "enabled", havingValue = "true")
-@Slf4j
-public class GrepTimeDbDataStorage extends AbstractHistoryDataStorage {
-
- /**
- * storage database
- */
- private static final String STORAGE_DATABASE = "hertzbeat";
- private static final String QUERY_HISTORY_SQL =
- "SELECT CAST (ts AS Int64) ts, instance, \"%s\" FROM %s WHERE ts >= %s and monitor_id = %s order by ts desc;";
- private static final String QUERY_HISTORY_WITH_INSTANCE_SQL =
- "SELECT CAST (ts AS Int64) ts, instance, \"%s\" FROM %s WHERE ts >= %s and monitor_id = %s and instance = %s order by ts desc;";
- private static final String QUERY_INSTANCE_SQL =
- "SELECT DISTINCT instance FROM %s WHERE ts >= now() - interval '1' WEEK";
- private static final String QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL =
- "SELECT first, avg ,max, min FROM (SELECT \"%s\" as first FROM %s WHERE monitor_id = %s and ts >= %s"
- + " and ts < %s ORDER BY ts LIMIT 1) LEFT JOIN (SELECT avg(\"%s\") as avg, min(\"%s\") as min, max(\"%s\") as max FROM %s WHERE ts >= %s and ts < %s) ON 1=1";
- private static final String TABLE_NOT_EXIST = "not exist";
- private static final String DATABASE_NOT_EXIST = "not exist";
- private GreptimeDB greptimeDb;
-
- public GrepTimeDbDataStorage(GreptimeProperties greptimeProperties) {
- this.serverAvailable = this.initDbSession(greptimeProperties);
- }
-
- private boolean initDbSession(GreptimeProperties properties) {
- String endpoint = properties.endpoint();
- GreptimeOptions opts = GreptimeOptions.newBuilder(endpoint)
- .writeMaxRetries(1)
- .readMaxRetries(2)
- .routeTableRefreshPeriodSeconds(-1)
- .build();
- greptimeDb = new GreptimeDB();
- if (!greptimeDb.init(opts)) {
- log.error("Fail to start Greptime client");
- return false;
- }
- return createDatabase();
- }
-
- /**
- * Checks if the database exists; if not, creates the Database.
- */
- private boolean createDatabase() {
- // 查询现有数据库
- QueryRequest showDatabases = QueryRequest.newBuilder()
- .exprType(SelectExprType.Sql)
- .ql("SHOW DATABASES;")
- .build();
- Result result = null;
- try {
- CompletableFuture> future = greptimeDb.query(showDatabases);
- result = future.get();
- } catch (Exception e) {
- log.info("TABLE_NOT_EXIST: {}", e.getMessage());
- String msg = e.getMessage();
- if (msg != null && !msg.contains(DATABASE_NOT_EXIST)) {
- log.warn(msg);
- }
-
- }
- // Check if the existing database includes“Hertzbeat”
- boolean isDatabaseExist = false;
- if (result != null && result.isOk()) {
- QueryOk queryOk = result.getOk();
- SelectRows rows = queryOk.getRows();
- List rowsList = rows.collect();
- for (Row row : rowsList) {
- for (io.greptime.models.Value value : row.values()) {
- if (STORAGE_DATABASE.equals(value.value().toString())) {
- log.info("Exist Database {}", STORAGE_DATABASE);
- isDatabaseExist = true;
- break;
- }
- }
- }
- }
- // If it does not exist, create database
- if (!isDatabaseExist) {
- QueryRequest createDatabase = QueryRequest.newBuilder()
- .exprType(SelectExprType.Sql)
- .ql("CREATE DATABASE %s;", STORAGE_DATABASE)
- .build();
- try {
- CompletableFuture> createFuture = greptimeDb.query(createDatabase);
- isDatabaseExist = createFuture.get().isOk();
- log.info("Database {} does not exist,and has been created", STORAGE_DATABASE);
- } catch (InterruptedException | ExecutionException e) {
- log.error("Error creating database");
- }
- }
- return isDatabaseExist;
- }
-
- @Override
- public void saveData(CollectRep.MetricsData metricsData) {
- if (!isServerAvailable() || metricsData.getCode() != CollectRep.Code.SUCCESS) {
- return;
- }
- if (metricsData.getValuesList().isEmpty()) {
- log.info("[warehouse greptime] flush metrics data {} is null, ignore.", metricsData.getId());
- return;
- }
- String monitorId = String.valueOf(metricsData.getId());
- String table = metricsData.getApp() + "_" + metricsData.getMetrics();
- TableSchema.Builder tableSchemaBuilder = TableSchema.newBuilder(TableName.with(STORAGE_DATABASE, table));
-
- List semanticTypes = new LinkedList<>(Arrays.asList(SemanticType.Tag, SemanticType.Tag, SemanticType.Timestamp));
- List dataTypes = new LinkedList<>(Arrays.asList(ColumnDataType.String, ColumnDataType.String, ColumnDataType.TimestampMillisecond));
- List columnNames = new LinkedList<>(Arrays.asList("monitor_id", "instance", "ts"));
-
- List fieldsList = metricsData.getFieldsList();
- for (CollectRep.Field field : fieldsList) {
- semanticTypes.add(SemanticType.Field);
- columnNames.add(field.getName());
- // handle field type
- if (field.getType() == CommonConstants.TYPE_NUMBER) {
- dataTypes.add(ColumnDataType.Float64);
- } else if (field.getType() == CommonConstants.TYPE_STRING) {
- dataTypes.add(ColumnDataType.String);
- }
- }
- tableSchemaBuilder.semanticTypes(semanticTypes.toArray(new SemanticType[0]));
- tableSchemaBuilder.dataTypes(dataTypes.toArray(new ColumnDataType[0]));
- tableSchemaBuilder.columnNames(columnNames.toArray(new String[0]));
- WriteRows rows = WriteRows.newBuilder(tableSchemaBuilder.build()).build();
- try {
- long now = System.currentTimeMillis();
- Object[] values = new Object[3 + fieldsList.size()];
- values[0] = monitorId;
- values[2] = now;
- for (CollectRep.ValueRow valueRow : metricsData.getValuesList()) {
- Map labels = new HashMap<>(8);
- for (int i = 0; i < fieldsList.size(); i++) {
- if (!CommonConstants.NULL_VALUE.equals(valueRow.getColumns(i))) {
- CollectRep.Field field = fieldsList.get(i);
- if (field.getType() == CommonConstants.TYPE_NUMBER) {
- values[3 + i] = Double.parseDouble(valueRow.getColumns(i));
- } else if (field.getType() == CommonConstants.TYPE_STRING) {
- values[3 + i] = valueRow.getColumns(i);
- }
- if (field.getLabel()) {
- labels.put(field.getName(), String.valueOf(values[3 + i]));
- }
- } else {
- values[3 + i] = null;
- }
- }
- values[1] = JsonUtil.toJson(labels);
- rows.insert(values);
- }
- rows.finish();
- CompletableFuture> writeFuture = greptimeDb.write(rows);
- try {
- Result result = writeFuture.get(10, TimeUnit.SECONDS);
- if (result.isOk()) {
- log.debug("[warehouse greptime]-Write successful");
- } else {
- log.warn("[warehouse greptime]--Write failed: {}", result.getErr().getFailedQl());
- }
- } catch (Throwable throwable) {
- log.error("[warehouse greptime]--Error occurred: {}", throwable.getMessage());
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- }
- }
-
- @Override
- public Map> getHistoryMetricData(Long monitorId, String app, String metrics, String metric,
- String label, String history) {
- Map> instanceValuesMap = new HashMap<>(8);
- if (!isServerAvailable()) {
- log.error("\n\t---------------Greptime Init Failed---------------\n"
- + "\t--------------Please Config Greptime--------------\n"
- + "\t----------Can Not Use Metric History Now----------\n");
- return instanceValuesMap;
- }
- long expireTime = getExpireTimeFromToken(history);
- String table = app + "_" + metrics;
- String selectSql = label == null
- ? String.format(QUERY_HISTORY_SQL, metric, table, expireTime, monitorId)
- : String.format(QUERY_HISTORY_WITH_INSTANCE_SQL, metric, table, expireTime, monitorId, label);
- log.debug("selectSql: {}", selectSql);
- QueryRequest request = QueryRequest.newBuilder()
- .exprType(SelectExprType.Sql)
- .databaseName(STORAGE_DATABASE)
- .ql(selectSql)
- .build();
- try {
- CompletableFuture> future = greptimeDb.query(request);
- Result result = future.get();
- if (result != null && result.isOk()) {
- QueryOk queryOk = result.getOk();
- SelectRows rows = queryOk.getRows();
- List