influxDB && docker-compose安装 && 基本操作以及语法

1 docker-compose安装

version: "3.4"

# https://docs.influxdata.com/influxdb/v1.7/administration/config
services:
  influxdb:
    image: influxdb:1.7-alpine
    environment:
      - INFLUXDB_ADMIN_ENABLED=true 
      - INFLUXDB_ADMIN_USER=${
    
    INFLUXDB_ADMIN_USER:-root}
      - INFLUXDB_ADMIN_PASSWORD=${
    
    INFLUXDB_ADMIN_PASSWORD:-root}
      - INFLUXDB_DB=test
      - INFLUXDB_HTTP_LOG_ENABLED=false
      - INFLUXDB_REPORTING_DISABLED=true
      - INFLUXDB_USER=${
    
    INFLUXDB_USER:-test}
      - INFLUXDB_USER_PASSWORD=${
    
    INFLUXDB_USER_PASSWORD:-test}
    ports:
      - "8083:8083"
      - "8086:8086"
    deploy:
      mode: replicated
      replicas: 1
      resources:
        limits:
          memory: 2048M
        reservations:
          memory: 1024M
    volumes:
      - ./local_bind_volume_dir:/var/lib/influxdb

1.5 基本概念理解

https://www.cnblogs.com/yihuihui/p/11386679.html

2 基本操作

https://jasper-zhang1.gitbooks.io/influxdb/content/Guide/writing_data.html
插入一个值:

curl -i -X POST "http://localhost:8086/write?db=test" -u root:root --data-binary "cpu_load_short,host=server01,region=us-west value=0.64,value2=0.86 1434055562000000000"

> insert cpu_load_short,host=server02,region=de value=0.1,value2=0.2

从上面的输出,简单小结一下插入的语句写法:
insert + measurement + “,” + tag=value,tag=value + + field=value,field=value

  • tag与tag之间用逗号分隔;field与field之间用逗号分隔
  • tag与field之间用空格分隔
  • tag都是string类型,不需要引号将value包裹
  • field如果是string类型,需要加引号

查询该值:

> select * from cpu_load_short;
name: cpu_load_short
time                host     region  value value2
----                ----     ------  ----- ------
1434055562000000000 server01 us-west 0.64  0.86
1637648158355267700 server02 de      0.1   0.2

4 代码操作:

注意写入的过程需要保证有field,不能全为tag,将很多raw插入:(以下划线开始的为filed,filed字段不会建立索引,故查询慢)

    public void insert(String table, List<Map<String, Object>> rows) {
    
    
        if (CollectionUtils.isEmpty(rows)) {
    
    
            return;
        }
        List<Point> points = rows.stream()
                .filter(e -> !CollectionUtils.isEmpty(e))
                .map(e -> {
    
    
                        return to(table, e);

                })
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(points)) {
    
    
            return;
        }
        BatchPoints batchPoints = BatchPoints.builder()
                .points(points)
                .build();
        influxDB.write(batchPoints);
        influxDB.flush();
    }

/** to函数 **/ 
    private Point to(String table, Map<String, Object> row)  {
    
    
        Point.Builder builder = Point.measurement(table).time(getTime(row), TimeUnit.MILLISECONDS);
        row.remove("time");
        row.forEach((k, v) -> {
    
    
            if (v == null) {
    
    
                return;
            }
            if (StringUtils.startsWith(k, "_")) {
    
    
                String key = StringUtils.removeStart(k, "_");
                if (v.getClass().getName().equals(boolean.class.getName())) {
    
    
                    builder.addField(key, (boolean) v);
                } else if (v.getClass().getName().equals(short.class.getName())) {
    
    
                    builder.addField(key, (short) v);
                } else if (v.getClass().getName().equals(int.class.getName())) {
    
    
                    builder.addField(key, (int) v);
                } else if (v.getClass().getName().equals(long.class.getName())) {
    
    
                    builder.addField(key, (long) v);
                } else if (v.getClass().getName().equals(float.class.getName())) {
    
    
                    builder.addField(key, (float) v);
                } else if (v.getClass().getName().equals(double.class.getName())) {
    
    
                    builder.addField(key, (double) v);
                } else if (v instanceof Boolean) {
    
    
                    builder.addField(key, (Boolean) v);
                } else if (v instanceof Number) {
    
    
                    builder.addField(key, (Number) v);
                } else if (v instanceof String) {
    
    
                    builder.addField(key, (String) v);
                } else {
    
    
                    builder.addField(key, v.toString());
                }
            } else {
    
    
                builder.tag(k, v.toString());
            }
        });
        return builder.build();
    }
    
/** build函数(influxdb官方维护) 将会检测是否含有field字段 **/ 
        public Point build() {
    
    
            Preconditions.checkNonEmptyString(this.measurement, "measurement");
            Preconditions.checkPositiveNumber(this.fields.size(), "fields size"); // 此处需保证fields size 大于等于1
            Point point = new Point();
            point.setFields(this.fields);
            point.setMeasurement(this.measurement);
            if (this.time != null) {
    
    
                point.setTime(this.time);
                point.setPrecision(this.precision);
            }

            point.setTags(this.tags);
            return point;
        }

猜你喜欢

转载自blog.csdn.net/cxy_hust/article/details/121493631
今日推荐