influxdb入门,批量插入数据提高性能

简介

       InfluxDB 是一个开源分布式时序、事件和指标数据库。使用Go语言编写,无需外部依赖。其设计目标是实现分布式和水平伸缩扩展。
       它有三大特性:
       1. Time Series (时间序列):你可以使用与时间有关的相关函数(如最大,最小,求和等);
       2. Metrics(度量):你可以实时对大量数据进行计算;
       3. Eevents(事件):它支持任意的事件数据。
       详细请参考官网:https://influxdata.com/

安装

       采用的是influxdb-0.13.0-static_linux_amd64.tar.gz,直接解压缩,进入解压目录运行
  1. ./influxd -pidfile influxd.pid -config influxdb.conf  
./influxd -pidfile influxd.pid -config influxdb.conf
其它安装方式请参考: https://docs.influxdata.com/influxdb/v0.13/introduction/installation/

下载地址:https://dl.influxdata.com,找到对应版本的Key,拼上前面的url即可下载。


       注意influxdb.conf需要按照修改某些dir和port。influxdb服务默认使用端口:

  1. TCP port 8083 is used for InfluxDB’s Admin panel  
  2. TCP port 8086 is used for client-server communication over InfluxDB’s HTTP API  
TCP port 8083 is used for InfluxDB’s Admin panel
TCP port 8086 is used for client-server communication over InfluxDB’s HTTP API
       8088端口其实也被占用了,而且不可配置,参见https://github.com/influxdata/influxdb/blob/master/cmd/influxd/run/config.go第36行DefaultBindAddress。之前的influxdb版本TCP ports 8088是可配置的。

使用

       可采用influx命令或者在浏览器中输入localhost:8083 即可进入web管理页面来使用。
       1创建数据库:  CREATE DATABASE testDB
       2 显示所有数据库: show databases
       3.  删除数据库: DROP DATABASE "db_name"
       4.  使用数据库: use testDB
       5.  显示该数据库中的表 : SHOW MEASUREMENTS
       6.  删除表:  DROP MEASUREMENT "measurementName"
       7 增:
            命令行:
  1. use testDB  
  2. insert weather,altitude=1000,area=北 temperature=11,humidity=-4  
          use testDB
          insert weather,altitude=1000,area=北 temperature=11,humidity=-4
            Http接口:
  1. curl -i -XPOST 'http://localhost:8086/write?db=testDB'   
  2.                 --data-binary 'weather,altitude=1000,area=北 temperature=11,humidity=-4'  
          curl -i -XPOST 'http://localhost:8086/write?db=testDB' 
                          --data-binary 'weather,altitude=1000,area=北 temperature=11,humidity=-4'
            插入数据的格式似乎比较奇怪,这是因为influxDB储存数据所采用的是Line Protocol格式。在上面两个插入数据的方法中,都有一样的部分。
  1. weather,altitude=1000,area=北 temperature=11,humidity=-4  
       weather,altitude=1000,area=北 temperature=11,humidity=-4
            其中:
            weather : 表名
            altitude=1000,area=北 : tag
            temperature=11,humidity=-4 :field
       8 删与改:在InfluxDB中并没有提供数据的删除与修改方法。可以通过数据保存策略(Retention Policies)来实现删除。
       9 查:
            命令行:
  1. use testDB  
  2. # 查询最新的三条数据  
  3. SELECT * FROM weather ORDER BY time DESC LIMIT 3  
          use testDB
          # 查询最新的三条数据
          SELECT * FROM weather ORDER BY time DESC LIMIT 3

            Http接口

  1. curl -G 'http://localhost:8086/query?pretty=true'   
  2.         --data-urlencode "db=testDB"   
  3.         --data-urlencode "q=SELECT * FROM weather ORDER BY time DESC LIMIT 3"  
          curl -G 'http://localhost:8086/query?pretty=true' 
                  --data-urlencode "db=testDB" 
                  --data-urlencode "q=SELECT * FROM weather ORDER BY time DESC LIMIT 3"
            InfluxDB是支持类SQL语句的,具体的查询语法都差不多。

       10. 用户管理:(以下语句都可以直接在InfluxDB的Web管理界面中调用)
              显示用户   SHOW USERS
              创建用户   CREATE USER "username" WITH PASSWORD 'password'
              创建管理员权限的用户   CREATE USER "username" WITH PASSWORD 'password' WITH ALL PRIVILEGES
              删除用户   DROP USER "username"


几个关键概念

       measurement,就相当于关系数据库中的table,就是tag,field,time的容器;
       对于influxDb的measurement来说,field是必须的,并且不能根据field来排序;
       Tag是可选的,tag可以用来做索引,tag是以字符串的形式存放的;tag字段一般用于where中限制条件。
       retention policy,保留策略,用于决定要保留多久的数据,保存几个备份,以及集群的策略等;
       series,a series is the collection of data that share a retention policy, measurement, and tag set。

Java操作

创建client
  1. private static InfluxDB getClient() {  
  2.     InfluxDB client = InfluxDBFactory.connect("http://10.210.228.89:9501""root""123456");  
  3.     Pong pong = client.ping();  
  4.     if (pong != null) {  
  5.         System.out.println("Pong : " + pong);  
  6.     } else  
  7.         return null;  
  8.     client.createDatabase(dbName);  
  9.     return client;  
  10. }  
private static InfluxDB getClient() {
    InfluxDB client = InfluxDBFactory.connect("http://10.210.228.89:9501", "root", "123456");
    Pong pong = client.ping();
    if (pong != null) {
        System.out.println("Pong : " + pong);
    } else
        return null;
    client.createDatabase(dbName);
    return client;
}
写数据:
  1. private static void writeData001() {  
  2.     Point point001 = Point.measurement("cpu")  
  3.             .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  
  4.             .tag("ip""127.0.0.1").tag("hostname""localhost")  
  5.             .addField("idle", 80L)  
  6.             .addField("user", 9L)  
  7.             .addField("system", 11L)  
  8.             .build();  
  9.     Point point002 = Point.measurement("disk")  
  10.             .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  
  11.             .tag("ip""127.0.0.1")  
  12.             .addField("used", 80L)  
  13.             .addField("free", 1L)  
  14.             .build();  
  15.   
  16.     BatchPoints batchPoints = BatchPoints  
  17.             .database(dbName)  
  18.             //.tag("async", "true") //Add a tag to this set of points.  
  19.             .retentionPolicy("default")  
  20.             .consistency(InfluxDB.ConsistencyLevel.ALL)  
  21.             .build();  
  22.     batchPoints.point(point001).point(point002);  
  23.     client.write(batchPoints);  
  24. }  
private static void writeData001() {
    Point point001 = Point.measurement("cpu")
            .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
            .tag("ip", "127.0.0.1").tag("hostname", "localhost")
            .addField("idle", 80L)
            .addField("user", 9L)
            .addField("system", 11L)
            .build();
    Point point002 = Point.measurement("disk")
            .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
            .tag("ip", "127.0.0.1")
            .addField("used", 80L)
            .addField("free", 1L)
            .build();

    BatchPoints batchPoints = BatchPoints
            .database(dbName)
            //.tag("async", "true") //Add a tag to this set of points.
            .retentionPolicy("default")
            .consistency(InfluxDB.ConsistencyLevel.ALL)
            .build();
    batchPoints.point(point001).point(point002);
    client.write(batchPoints);
}
  1. private static void writeData002() {  
  2.     // Flush every 2000 Points, at least every 100ms  
  3.     client.enableBatch(2000100, TimeUnit.MILLISECONDS);  
  4.   
  5.     Point point1 = Point.measurement("cpu")  
  6.             .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  
  7.             .addField("idle", 90L)  
  8.             .addField("user", 9L)  
  9.             .addField("system", 1L)  
  10.             .build();  
  11.     Point point2 = Point.measurement("disk")  
  12.             .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)  
  13.             .addField("used", 80L)  
  14.             .addField("free", 1L)  
  15.             .build();  
  16.   
  17.     client.write(dbName, "default", point1);  
  18.     client.write(dbName, "default", point2);  
  19. }  
private static void writeData002() {
    // Flush every 2000 Points, at least every 100ms
    client.enableBatch(2000, 100, TimeUnit.MILLISECONDS);

    Point point1 = Point.measurement("cpu")
            .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
            .addField("idle", 90L)
            .addField("user", 9L)
            .addField("system", 1L)
            .build();
    Point point2 = Point.measurement("disk")
            .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
            .addField("used", 80L)
            .addField("free", 1L)
            .build();

    client.write(dbName, "default", point1);
    client.write(dbName, "default", point2);
}
查询:
  1. private static void query() {  
  2.     Query query = new Query("SELECT idle FROM cpu", dbName);  
  3.     QueryResult queryResult = client.query(query);  
  4.     System.out.println(queryResult);  
  5. }  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不许赖zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值