This is an automated email from the ASF dual-hosted git repository.

zhaoqingran pushed a commit to branch doris
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git

commit 2fb1dce97912514f682fb7908e46a4d7d9d10842
Author: zqr10159 <[email protected]>
AuthorDate: Fri Aug 23 11:36:33 2024 +0800

    ```feat(doris): add support for Doris data storage integration
    
    Implement Doris as a new data storage option for historical metrics. This 
includes thenecessary utilities for streaming data loading into Doris clusters, 
and integration with
    existing framework structures to support configuration properties and data 
storage operations.
    
    BREAKING CHANGE: The introduction of Doris support does not affect existing 
functionalities
    but provides an additional storage backend option. Users must configure the 
Doris connection
    appropriately in the application.yml to utilize this feature.
    ```
---
 manager/src/main/resources/application.yml         |  9 +++
 .../warehouse/constants/WarehouseConstants.java    |  2 +
 .../store/history/doris/DorisDataStorage.java      | 81 +++++++++++++++++++
 .../store/history/doris/DorisProperties.java       | 42 ++++++++++
 .../warehouse/utils/DorisStreamLoadUtil.java       | 94 ++++++++++++++++++++++
 5 files changed, 228 insertions(+)

diff --git a/manager/src/main/resources/application.yml 
b/manager/src/main/resources/application.yml
index 33ad559a2..1902ae6ec 100644
--- a/manager/src/main/resources/application.yml
+++ b/manager/src/main/resources/application.yml
@@ -165,6 +165,15 @@ warehouse:
       password: root
       expire-time: '30d'
       replication: 1
+    doris:
+      enabled: false
+      host: 127.0.0.1
+      httpPort: 8030
+      username: root
+      password:
+      db: _internal_schema
+      table: hzb_history
+      expire-time: '30d'
   # store real-time metrics data, enable only one below
   real-time:
     memory:
diff --git 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/constants/WarehouseConstants.java
 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/constants/WarehouseConstants.java
index aee6e4ff9..49ab795e9 100644
--- 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/constants/WarehouseConstants.java
+++ 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/constants/WarehouseConstants.java
@@ -44,6 +44,8 @@ public interface WarehouseConstants {
         String VM = "victoria-metrics";
 
         String VM_CLUSTER = "victoria-metrics.cluster";
+
+        String DORIS = "doris";
     }
 
     /**
diff --git 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/doris/DorisDataStorage.java
 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/doris/DorisDataStorage.java
new file mode 100644
index 000000000..3a92892de
--- /dev/null
+++ 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/doris/DorisDataStorage.java
@@ -0,0 +1,81 @@
+/*
+ * 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.doris;
+
+import java.util.List;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.hertzbeat.common.entity.dto.Value;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+import org.apache.hertzbeat.warehouse.store.history.AbstractHistoryDataStorage;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConditionalOnProperty(prefix = "warehouse.store.doris", name = "enabled", 
havingValue = "true")
+@Slf4j
+public class DorisDataStorage extends AbstractHistoryDataStorage {
+    /**
+     * query history range metrics data from doris
+     *
+     * @param monitorId monitor id
+     * @param app       monitor type
+     * @param metrics   metrics
+     * @param metric    metric
+     * @param label     label
+     * @param history   range
+     * @return metrics data
+     */
+    @Override
+    public Map<String, List<Value>> getHistoryMetricData(Long monitorId, 
String app, String metrics, String metric, String label, String history) {
+        return Map.of();
+    }
+
+    /**
+     * query history range interval metrics data from doris
+     * max min mean metrics value
+     *
+     * @param monitorId monitor id
+     * @param app       monitor type
+     * @param metrics   metrics
+     * @param metric    metric
+     * @param label     label
+     * @param history   history range
+     * @return metrics data
+     */
+    @Override
+    public Map<String, List<Value>> getHistoryIntervalMetricData(Long 
monitorId, String app, String metrics, String metric, String label, String 
history) {
+        return Map.of();
+    }
+
+    /**
+     * save metrics data
+     *
+     * @param metricsData metrics data
+     */
+    @Override
+    public void saveData(CollectRep.MetricsData metricsData) {
+
+    }
+
+    @Override
+    public void destroy() throws Exception {
+
+    }
+}
diff --git 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/doris/DorisProperties.java
 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/doris/DorisProperties.java
new file mode 100644
index 000000000..b3118ceb6
--- /dev/null
+++ 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/doris/DorisProperties.java
@@ -0,0 +1,42 @@
+/*
+ * 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.doris;
+
+import org.apache.hertzbeat.common.constants.ConfigConstants;
+import org.apache.hertzbeat.common.constants.SignConstants;
+import org.apache.hertzbeat.warehouse.constants.WarehouseConstants;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.context.properties.bind.DefaultValue;
+
+
+@ConfigurationProperties(prefix = 
ConfigConstants.FunctionModuleConstants.WAREHOUSE
+        + SignConstants.DOT
+        + WarehouseConstants.STORE
+        + SignConstants.DOT
+        + WarehouseConstants.HistoryName.DORIS)
+public record DorisProperties(@DefaultValue("false") boolean enabled,
+                              @DefaultValue("127.0.0.1") String host,
+                              @DefaultValue("8030") int httpPort,
+                              @DefaultValue("root") String username,
+                              @DefaultValue("") String password,
+                              @DefaultValue("_internal_schema") String db,
+                              @DefaultValue("hzb_history") String table,
+                              // Database TTL, default is 30 days.
+                              @DefaultValue("30d") String expireTime) {
+}
+
diff --git 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/utils/DorisStreamLoadUtil.java
 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/utils/DorisStreamLoadUtil.java
new file mode 100644
index 000000000..464569c28
--- /dev/null
+++ 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/utils/DorisStreamLoadUtil.java
@@ -0,0 +1,94 @@
+/*
+ * 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.utils;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.http.HttpHeaders;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.DefaultRedirectStrategy;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+@Slf4j
+public class DorisStreamLoadUtil {
+
+    public static void sendData(String host,
+                                int httpPort,
+                                String username,
+                                String password,
+                                String db,
+                                String table,
+                                String content) throws Exception {
+        final String loadUrl = 
String.format("http://%s:%s/api/%s/%s/_stream_load";,
+                host,
+                httpPort,
+                db,
+                table);
+
+        final HttpClientBuilder httpClientBuilder = HttpClients
+                .custom()
+                .setRedirectStrategy(new DefaultRedirectStrategy() {
+                    @Override
+                    protected boolean isRedirectable(String method) {
+                        return true;
+                    }
+                });
+
+        try (CloseableHttpClient client = httpClientBuilder.build()) {
+            HttpPut put = new HttpPut(loadUrl);
+            StringEntity entity = new StringEntity(content, "UTF-8");
+            put.setHeader(HttpHeaders.EXPECT, "100-continue");
+            put.setHeader(HttpHeaders.AUTHORIZATION, basicAuthHeader(username, 
password));
+            // format:json
+            put.setHeader("format","json");
+            // mode:array
+            put.setHeader("strip_outer_array","true");
+            // ignore error
+            put.setHeader("max_filter_ratio","0.1");
+            put.setEntity(entity);
+
+            try (CloseableHttpResponse response = client.execute(put)) {
+                String loadResult = "";
+                if (response.getEntity() != null) {
+                    loadResult = EntityUtils.toString(response.getEntity());
+                }
+                final int statusCode = 
response.getStatusLine().getStatusCode();
+                if (statusCode != 200) {
+                    throw new IOException(
+                            String.format("Stream load failed, statusCode=%s 
load result=%s", statusCode, loadResult));
+
+
+                }
+            }
+        }
+    }
+
+    private static String basicAuthHeader(String username, String password) {
+        final String tobeEncode = username + ":" + password;
+        byte[] encoded = 
Base64.encodeBase64(tobeEncode.getBytes(StandardCharsets.UTF_8));
+        return "Basic " + new String(encoded);
+    }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to