Add dependency
<dependency> <groupId>org.influxdb</groupId> <artifactid>influxdb-java</ artifactid> <version>2.5</version></dependency>
Java instance
Package com.ysl.influx;import Org.influxdb.influxdb;import org.influxdb.InfluxDBFactory; Public classInfluxdbutils {PrivateString UserName; PrivateString password; PrivateString URL; PrivateInfluxDB InfluxDB; Publicinfluxdbutils (String url,string userName, string password) { This. UserName =UserName; This. Password =password; This. url =URL; } PublicInfluxDB Builder () {if(InfluxDB = =NULL) {synchronized ( This){ if(InfluxDB = =NULL) {InfluxDB=Influxdbfactory.connect (Url,username,password); } } } returnInfluxDB; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString GetUrl () {returnURL; } Public voidseturl (String url) { This. url =URL; } PublicInfluxDB getinfluxdb () {returnInfluxDB; } Public voidsetinfluxdb (InfluxDB InfluxDB) { This. InfluxDB =InfluxDB; }}
Package Com.ysl.influx;import Org.influxdb.influxdb;import org.influxdb.dto.point;import Org.influxdb.dto.query;import Org.influxdb.dto.queryresult;import Java.util.Map; Public classInfluxdbservice {PrivateString database; PrivateString PolicyName; PrivateInfluxDB InfluxDB; Publicinfluxdbservice (String database, String policyName, InfluxDB InfluxDB) { This. Database =database; This. PolicyName =PolicyName; This. InfluxDB =InfluxDB; } Public voidCreateDatabase () {influxdb.createdatabase (database); } /** * Build Save Policy * @param duration Storage time * @param replicationnum Backup Quantity*/ Public voidCreateretentionpolicy (string duration, Integer replicationnum) {string cmd= String.Format ("CREATE RETENTION POLICY%s on%s DURATION%s REPLICATION%s DEFAULT", Policyname,database,duration,replicationnum); Influxdb.query (NewQuery (cmd,database)); } Publicqueryresult query (String cmd) {returnInfluxdb.query (NewQuery (cmd,database)); } /** * Insert data * @param measurement table name * @param tags * @param fields*/ Public voidInsert (String measurement,map<string,string> tags,map<string,object>Fields ) {Point.builder Builder=point.measurement (measurement). tag (tags); if(Fields! =NULL&&!Fields.isempty ()) {builder.fields (fields); } influxdb.write (Database,policyname,builder.build ()); } PublicString getdatabase () {returndatabase; } PublicString Getpolicyname () {returnPolicyName; } PublicInfluxDB getinfluxdb () {returnInfluxDB; }}
Package Com.ysl.influx;import Org.influxdb.influxdb;import org.influxdb.dto.queryresult;import Java.util.hashmap;import Java.util.list;import Java.util.Map; Public classInfluxdbtest {/** * Database name*/ Private StaticFinal String db_name ="influx_test"; /** * Data Retention policy*/ Private StaticFinal String Retentionpolicy ="Influx_name"; Public Static voidMain (string[] args) {InfluxDB InfluxDB=NewInfluxdbutils ("http://localhost:8086/","User","Pass"). Builder (); Influxdbservice Service=NewInfluxdbservice (DB_NAME,RETENTIONPOLICY,INFLUXDB); Service.createdatabase (); Service.createretentionpolicy ("30d",1); Map<string, string> tags =NewHashmap<>(); Tags.put ("MethodName","GetName"); Map<string, object> fields =NewHashmap<>(); Fields.put ("RT", $); Fields.put ("TPS", -); Service.insert ("Measurementkey", tags, fields); //Querying DataQueryResult QueryResult = Service.query ("select * from \ "Measurementkey\""); List<QueryResult.Result> results =queryresult.getresults (); if(Results! =NULL&& results.size () >0) { for(Queryresult.result result:results) {System. out. println (Result.tostring ()); } } }}
InfluxDB Getting Started with Java