InfluxDB Getting Started with Java

Source: Internet
Author: User
Tags influxdb

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.