java使用influxDB数据库的详细代码
本文实例为大家分享了java使用influxDB数据库的具体代码,供大家参考,具体内容如下
1.pom.xml中导入jar包依赖
org.influxdb influxdb-java 2.5
2.编写influxDB工具类:
packagecom.hontye.parameter.util; importorg.influxdb.InfluxDB; importorg.influxdb.InfluxDBFactory; importorg.influxdb.dto.Point; importorg.influxdb.dto.Point.Builder; importorg.influxdb.dto.Query; importorg.influxdb.dto.QueryResult; importjava.util.Map; /** *时序数据库InfluxDB连接 *@authorDai_LW * */ publicclassInfluxDbUtil{ privatestaticStringopenurl="http://127.0.0.1:8086";//连接地址 privatestaticStringusername="root";//用户名 privatestaticStringpassword="root";//密码 privatestaticStringdatabase="PARAMTER_DB";//数据库 privatestaticStringmeasurement="tw_parameter_tb";//表名 privateInfluxDBinfluxDB; publicInfluxDbUtil(Stringusername,Stringpassword,Stringopenurl,Stringdatabase){ this.username=username; this.password=password; this.openurl=openurl; this.database=database; } publicstaticInfluxDbUtilsetUp(){ //创建连接 InfluxDbUtilinfluxDbUtil=newInfluxDbUtil(username,password,openurl,database); influxDbUtil.influxDbBuild(); influxDbUtil.createRetentionPolicy(); //influxDB.deleteDB(database); //influxDB.createDB(database); returninfluxDbUtil; } /**连接时序数据库;获得InfluxDB**/ publicInfluxDBinfluxDbBuild(){ if(influxDB==null){ influxDB=InfluxDBFactory.connect(openurl,username,password); influxDB.createDatabase(database); } returninfluxDB; } /** *设置数据保存策略 *defalut策略名/database数据库名/30d数据保存时限30天/1副本个数为1/结尾DEFAULT表示设为默认的策略 */ publicvoidcreateRetentionPolicy(){ Stringcommand=String.format("CREATERETENTIONPOLICY\"%s\"ON\"%s\"DURATION%sREPLICATION%sDEFAULT", "defalut",database,"30d",1); this.query(command); } /** *查询 *@paramcommand查询语句 *@return */ publicQueryResultquery(Stringcommand){ returninfluxDB.query(newQuery(command,database)); } /** *插入 *@paramtags标签 *@paramfields字段 */ publicvoidinsert(Maptags,Map fields){ Builderbuilder=Point.measurement(measurement); builder.tag(tags); builder.fields(fields); influxDB.write(database,"",builder.build()); } /** *删除 *@paramcommand删除语句 *@return返回错误信息 */ publicStringdeleteMeasurementData(Stringcommand){ QueryResultresult=influxDB.query(newQuery(command,database)); returnresult.getError(); } /** *创建数据库 *@paramdbName */ publicvoidcreateDB(StringdbName){ influxDB.createDatabase(dbName); } /** *删除数据库 *@paramdbName */ publicvoiddeleteDB(StringdbName){ influxDB.deleteDatabase(dbName); } publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } publicStringgetPassword(){ returnpassword; } publicvoidsetPassword(Stringpassword){ this.password=password; } publicStringgetOpenurl(){ returnopenurl; } publicvoidsetOpenurl(Stringopenurl){ this.openurl=openurl; } publicvoidsetDatabase(Stringdatabase){ this.database=database; } }
3.存值
publicclassQuatyServiceImpl{ privateInfluxDbUtilinfluxDB; publicvoidintoDb(){ influxDB=InfluxDbUtil.setUp(); Maptags=newHashMap<>(); Map fields=newHashMap<>(); tags.put("TAG_NAME",info.getKey()); fields.put("TAG_VALUE",code); fields.put("TIMAMPEST",df.format(newDate())); influxDB.insert(tags,fields); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。