0%

influxdb正则表达式的应用

准备测试数据

public static void main(String[] args) {
        InfluxDB influxDB = InfluxDBFactory.connect("http://192.168.41.128:8086", "root", "root");
        String dbName = "mydb";
        influxDB.query(new Query("CREATE DATABASE " + dbName));
        long time = 1600327592000L;
        // 每张表内5w条数据
        int count = 50000;
        while (count > 0) {
            System.out.println(count);
            count--;
            // 每条数据间隔5秒
            time = time + 5000;
            BatchPoints batchPoints = BatchPoints
                    .database(dbName)
                    .tag("async", "true")
                    .consistency(InfluxDB.ConsistencyLevel.ALL)
                    .build();
            for (int i = 0; i < 10000; i++) {
                // 表名node-0 ~ node-9999,共10000张表,数据随机生成
                Point point1 = Point.measurement("node-" + i)
                        .time(time, TimeUnit.MILLISECONDS)
                        .addField("idle", new Random().nextInt(100))
                        .addField("user", new Random().nextInt(100))
                        .addField("system", new Random().nextInt(100))
                        .build();
                batchPoints.point(point1);
            }
            influxDB.write(batchPoints);
        }
        influxDB.close();
    }

使用正则表达式查询测试

目前influxdb支持在以下场景中使用正则表达式:

  • field keys and tag keys in the SELECT clause
  • measurements in the FROM clause
  • tag values and string field values in the WHERE clause.
  • tag keys in the GROUP BY clause
-- 查询格式
-- Supported operators
-- =~ matches against !~ doesn’t match against
SELECT /<regular_expression_field_key>/ FROM /<regular_expression_measurement>/ WHERE [<tag_key> <operator> /<regular_expression_tag_value>/ | <field_key> <operator> /<regular_expression_field_value>/] GROUP BY /<regular_expression_tag_key>/
-- 测试用例
SELECT /l/ FROM "h2o_feet" LIMIT 1
SELECT MEAN("degrees") FROM /temperature/
SELECT MEAN(water_level) FROM "h2o_feet" WHERE "location" =~ /[m]/ AND "water_level" > 3
SELECT * FROM "h2o_feet" WHERE "location" !~ /./
SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" =~ /./
SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND "level description" =~ /between/
SELECT FIRST("index") FROM "h2o_quality" GROUP BY /l/

测试使用正则表达式进行切面查询

-- 查询某个时间点,所有表的数据,切面查询
SELECT * FROM /node-/ WHERE time='2020-09-18T01:43:27Z'
-- 查询node-1001,node-1002,node-1003,node-1004,node-1005数据
SELECT * FROM /(node-100)[1-5]/ WHERE time='2020-09-18T01:43:27Z'

参考链接