浏览代码

优化批量写入

欧阳劲驰 2 周之前
父节点
当前提交
34718f8d08

+ 1 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/domain/InfluxDbClient.java

@@ -22,6 +22,7 @@ public class InfluxDbClient extends InfluxDBImpl {
     public InfluxDbClient(String url, String username, String password, String database, OkHttpClient.Builder client) {
         super(url, username, password, client);
         super.setDatabase(database);
+        super.enableGzip();
         this.database = database;
     }
 }

+ 5 - 2
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/utils/InfluxDbUtil.java

@@ -76,11 +76,14 @@ public class InfluxDbUtil {
                     , String.format("开始批量写入InfluxDb,数据量:%d", points.size()));
             long begin = System.currentTimeMillis();
 
+            //转批量对象
+            Point[] array = points.parallelStream().toArray(Point[]::new);
+
+            //写入
             for (InfluxDbClient influxDb : influxDBClients.clients()) {
-                //转批量对象
                 BatchPoints batchPoints = BatchPoints
                         .database(influxDb.getDatabase())
-                        .points(points.toArray(new Point[0]))
+                        .points(array)
                         .build();
 
                 influxDb.write(batchPoints);