最近用到了Influxdb数据库,这是一个优秀的时间序列数据库,我们主要用来记录了些监控日志,然后要做些统计查询。这里做个简单的总结。
项目基于 springboot2.*,整合 influxdb相当简单,引进依赖,1.7为当前最新版:
<dependency> <groupId>com.github.miwurster</groupId> <artifactId>spring-data-influxdb</artifactId> <version>1.7</version> </dependency>
添加配置:
spring: influxdb: url: http://localhost:8086 username: test password: test database: test retention-policy: autogen connect-timeout: 10 read-timeout: 30 write-timeout: 10
表在写入数据时自动创建,measurement相当于表。代码示例:
public void writeData(LoginLogDO loginLogDO) { final Point.Builder builder = Point.measurement("loginLogDO") .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS); builder.tag("loginUser", loginLogDO.getLoginUser()) .tag("ip", loginLogDO.getIp()) .tag("result", loginLogDO.getResult()) .tag("brower", loginLogDO.getBrower()) .tag("os", loginLogDO.getOs()) .addField("count", loginLogDO.getCount()); final Point point = builder.build(); influxDBTemplate.write(point); } 如上,就可将数据写入数据库。 分页查询 sql示例:
select * from loginLogDO where time>now()-3d and time<now() order by time desc limit 10 offset 20
记住:limit后面是pageSize,offset后面是偏移量,就是起始记录数。是(当前页-1)*pageSize.
limit {pageSize} offset {offset}
下面也可以, 直接用时间纳秒数,秒后面9个0。
select * from loginLogDO where time>1568172960000000000 and time<1568179960000000000 order by time desc limit 10 offset 20
下面说统计查询。influxdb这方面比较强大。简单好用。 select count(*) from monitorDO where module='spdemo' and time>now()-10m group by time(1m),resultType limit 10 这样会查出一个列表,以resultType 分组的数据。按时间排序显示。查最近10分钟内,以1分钟为 节点单位的10条统计结果。 后面的limit 10 表示是取最靠近time>now()-10m这个时间的10条统计数据, 不加,则取所有。 结果如下: name: monitorDO tags: resultType=success time count_count ---- ----------- 1568182080000000000 0 1568182140000000000 0 1568182200000000000 0 1568182260000000000 0 1568182320000000000 0 1568182380000000000 0 1568182440000000000 0 1568182500000000000 0 1568182560000000000 3 1568182620000000000 0 1568182680000000000 2 时间函数也很灵活强大。 d代表天,h代表小时,m代表分, s代表秒。 now()-1d就表示当前时间的减一天的时间点。 group by time(1m) 表示以一分钟为单位进行数据统计。 最后是分页的示例部分代码:
Query countquery=new Query(countsql,influxDBTemplate.getDatabase()); QueryResult countresult = influxDBTemplate.query(countquery); int total = 0; if (countresult.getResults().get(0).getSeries() != null) { Object totalObj = countresult.getResults().get(0).getSeries().get(0).getValues().get(0).get(1); total = totalObj == null ? 0 : ((Double) totalObj).intValue(); } if (total > 0){ Query query = new Query(selectsql, influxDBTemplate.getDatabase()); QueryResult result = influxDBTemplate.query(query); log.debug("result==>" + result); List<List<Object>> mylist = result.getResults().get(0).getSeries().get(0).getValues(); loginColumns = loginColumns == null ? result.getResults().get(0).getSeries().get(0).getColumns() : loginColumns; return new CommonPager<>( new PageParameter(pageParameter.getCurrentPage(), pageParameter.getPageSize(), total), mylist.stream().map(this::toMap).collect(Collectors.toList()));
下面是统计的java主要代码:
Query query = new Query(sql.replace(",resultType", ""), influxDBTemplate.getDatabase()); QueryResult result = influxDBTemplate.query(query);if (result.getResults().get(0).getSeries() == null) { return null; } List<List<Object>> mylist = result.getResults().get(0).getSeries().get(0).getValues(); List<Map<String, Object>> allList= mylist.stream().map(this::toMonitorMinuteMap).collect(Collectors.toList()); Query rstquery = new Query(sql, influxDBTemplate.getDatabase()); QueryResult opresult = influxDBTemplate.query(rstquery); List<QueryResult.Series> seriesList=opresult.getResults().get(0).getSeries(); List<Map<String, Object>> errorList=null; List<Map<String, Object>> successList=null; for(QueryResult.Series series: seriesList){ if( series.getTags().get("resultType").equals("success")){ successList=series.getValues().stream().map(this::toMonitorMinuteMap).collect(Collectors.toList()); }else{ errorList=series.getValues().stream().map(this::toMonitorMinuteMap).collect(Collectors.toList()); } }
private Map<String, Object> toMonitorMinuteMap( List<Object> list) { Map<String, Object> map = new HashMap<>(); map.put("time", ((String)list.get(0)).replace("T", " ").replace("Z", "")); map.put("value",list.get(1)); return map; }
好吧,就先介绍这么多哈。希望对大家有所帮助. 补充配置类:
@Configuration @EnableConfigurationProperties(InfluxDBProperties.class) @ConditionalOnProperty(prefix = "spring.influxdb", name = "url") public class InfluxDbConfiguration { @Bean public InfluxDBConnectionFactory connectionFactory(final InfluxDBProperties influxDBProperties) { return new InfluxDBConnectionFactory(influxDBProperties); } @Bean public InfluxDBTemplate<Point> influxDBTemplate(final InfluxDBConnectionFactory connectionFactory) { return new InfluxDBTemplate<>(connectionFactory, new PointConverter()); } @Bean public DefaultInfluxDBTemplate defaultTemplate(final InfluxDBConnectionFactory connectionFactory) { return new DefaultInfluxDBTemplate(connectionFactory); } }
服务类示例:
@Service("myInfluxDbService") public class MyInfluxDbServiceImpl implements MyInfluxDbService { private InfluxDBTemplate<Point> influxDBTemplate; public InfluxDbServiceImpl (InfluxDBTemplate<Point> influxDBTemplate) { this.influxDBTemplate=influxDBTemplate; }
相关推荐
InfluxDB查询 导出数据 mybatis SpringBoot
InfluxDB是一个用于存储和分析时间序列数据的开源数据库。 主要特性有: 内置HTTP接口,使用方便 数据可以打标记,查让查询可以很灵活 类SQL的查询语句 安装管理很简单,并且读写数据很高效 能够实时查询,数据在...
该适配器将状态历史记录保存到InfluxDB中。 仅支持InfluxDB> = v0.9,建议使用v1.0 该适配器使用Sentry库自动向开发人员报告异常和代码错误。 有关更多详细信息以及如何禁用错误报告的信息,请参见! Sentry报告...
influxDB的增删改查操作,全部测试通过了 安装详解在这: https://blog.csdn.net/qq_27026603/article/details/79710465
基于SpringBoot + Kafka + Redis + InfluxDB + HBase + Grafana 的风控系统,项目经过严格测试,确保可以运行! 基于SpringBoot + Kafka + Redis + InfluxDB + HBase + Grafana 的风控系统,项目经过严格测试,确保...
influxDB2.0基本使用以及相关jar 导入CSV文件以及查询数据,内置生成CSV数据工具
C#读写Influxdb代码示例源代码,简单、易用,新手可直接调用InfluxDBHelper,有其他方法的欢迎一起讨论学习
虚拟机Centos系统使用Docker部署TDengine和influxdb、GRafana;通过TDengine和influxdb两种方式显示数据在Grafana上。菜鸟教程,每一步都有截图,适合懒人使用。
阿里云关于InfluxDB数据采集服务的演示文档。。。。。
主要为大家介绍了java使用influxDB数据库的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
InfluxDB 是一个开源的时间序列数据库,能应付极高的写和查询并发数,主要用于存储大规模的时间戳数据(每条记录自动附加时间戳),如 DevOps 监控数据、应用系统运行指标数据、物联网感应器采集的数据及实时分析的...
平台:Windows InfluxDb开源数据库管理工具,帮你简化查询数据库的步骤,提高工作效率。
使用 MQTT、InfluxDB 和 Grafana 监控家庭传感器数据 项目 01-mosquitto: Mosquitto Docker 容器配置文件 02-bridge:接收 MQTT 数据并将其持久化到 InfluxDB 的 Python 脚本 03-bme280_mqtt:用于将传感器数据发布...
本文件是基于springboot + php + kotlin,底层设备采集到数据后,通过springboot后台程序进行转发,并且将数据存储到influxDB里面,后通过influxDB提供API接口供PHP端后台程序的界面展示,从而提供一个完整的物联网...
OPC_DA连接,导入Excel,InfluxDB存储,MQTT订阅发布。
这个简单的实用程序连接到Influxdb,下载JMeter响应时间数据并绘制图表。 它演示了如何完成任务。 脚本可以根据您的需要进行修改。 入门 这些说明将为您提供在本地计算机上运行并运行的项目的副本,以进行开发和...
将时间序列数据从 mysql 导入 InfluxDB。 安装 如果您使用的是 Debian/Ubuntu,则可以使用以下命令安装依赖项: $ sudo apt-get install python-influxdb $ sudo apt-get install python-mysqldb 如何使用 由于这...
时序数据库,可以操作influxDB数据库 简单方便,省去了 命令行的操作方式
InfluxDB 是一个开源的时间序列数据库,能应付极高的写和查询并发数,主要用于存储大规模的时间戳数据(每条记录自动附加时间戳),如 DevOps 监控数据、应用系统运行指标数据、物联网感应器采集的数据及实时分析的...
InfluxDB 数据库 c++ 、qt 实现与使用 ,InfluxDB 是一种时序型数据库,现目前只支持 http 方式进行传输 关键代码{ 参考 https://blog.csdn.net/weixin_41330194/article/details/111195621 }