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'

参考链接


文章作者: wuzhiyong
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 wuzhiyong !
评论
 上一篇
influxdb客户端连接超时问题 influxdb客户端连接超时问题
问题// 默认情况,java客户端连接服务端等待数据返回的时间很短,如果超时就会报以下错误 org.influxdb.InfluxDBIOException: java.net.SocketTimeoutException: timeout
2020-09-21
下一篇 
influxdb使用docker部署 influxdb使用docker部署
步骤 导出配置 docker run --rm influxdb influxd config > influxdb.conf 启动 # 设置端口映射 # 设置自动重启 # 挂载配置并使用配置启动 # 挂载数据 docker run
2020-09-17
  目录