The KAIROSDB client is a Java library, that makes sending metrics and querying, the KAIROSDB server simple. The HttpClient class is used to push metrics or query the KAIROSDB server. The library uses the builder pattern to simplify the task of creating, the JSON that's used by the client.
Source: https://github.com/kairosdb/kairosdb-client/
Integrate the source code into your Java project
The red part is the source code section.
We set up a class to test the operation
Package Kairosdb.client.demo;import Java.io.ioexception;import Java.net.malformedurlexception;import Java.net.urisyntaxexception;import Org.kairosdb.client.httpclient;import Org.kairosdb.client.builder.aggregatorfactory;import Org.kairosdb.client.builder.metricbuilder;import Org.kairosdb.client.builder.querybuilder;import Org.kairosdb.client.builder.timeunit;import Org.kairosdb.client.response.getresponse;import Org.kairosdb.client.response.queryresponse;import Org.kairosdb.client.response.response;public class Metric {//gets Metric's name collection public static void Getmetricnames () { HttpClient client; GetResponse response;try {client = new HttpClient ("http://10.20.0.10:9090"); response = Client.getmetricnames (); System.out.println ("*********************metricnames**********************"); System.out.println ("Response Code =" + Response.getstatuscode ()); for (String name:response.getResults ()) { SYSTEM.OUT.PRINTLN (name);} Client.shutdown ();} catch (IOException e) {e.printstacktrace ();}} Gets the name collection of the tagpublic static void Gettagnames () {HttpClient client; GetResponse response;try {client = new HttpClient ("http://10.20.0.10:9090"); response = Client.gettagnames (); System.out.println ("*********************tagnames**********************"); System.out.println ("Response Code =" + Response.getstatuscode ()); for (String name:response.getResults ()) { SYSTEM.OUT.PRINTLN (name);} Client.shutdown ();} catch (IOException e) {e.printstacktrace ();}} Gets the value of the tag collection public static void Gettagvalues () {HttpClient client; GetResponse response;try {client = new HttpClient ("http://10.20.0.10:9090"); response = Client.gettagvalues (); System.out.println ("*********************tagvalues**********************"); System.out.println ("Response Code =" + Response.getstatuscode ()); for (String name:response.getResults ()) { SYSTEM.OUT.PRINTLN (name);} Client.shutdown ();} catch (IOException e) {e.printstacktrace ();}} Pushmetricpublic static void Pushmetric () throws URISyntaxException, IOException {metricbuilder builder = MetricbuilDer.getinstance (); Builder.addmetric ("Proc.loadavg.1m"). Addtag ("host", "Kairosdb"). Adddatapoint ( System.currenttimemillis (), Adddatapoint (System.currenttimemillis (), 30L); HttpClient client = new HttpClient ("http://10.20.0.10:9090"); Response Response = client.pushmetrics (builder); System.out.println ("*********************push metric**********************"); System.out.println ("Response Code =" + Response.getstatuscode ()); Client.shutdown ();} Query data node public static void Querydatapoint () throws IOException, urisyntaxexception {querybuilder builder = QueryBuilder. GetInstance (); Builder.setstart (5, timeunit.minutes). SetEnd (1, timeunit.minutes). Addmetric ("Kairosdb.http.query_ Time "). Addaggregator (Aggregatorfactory.createsumaggregator (1, timeunit.minutes)); HttpClient client = new HttpClient ("http://10.20.0.10:9090"); Queryresponse response = Client.query (builder); String BODY = Response.getbody (); System.out.println ("*********************query datapoint**********************"); System.out.pRintln ("Response Code =" + Response.getstatuscode ()); System.out.println (body); Client.shutdown ();} Delete data points in metric public static void Deletemetric () throws IOException {//Metricbuilder builder = Metricbuilder.getinstanc E (); HttpClient client = new HttpClient ("http://10.20.0.10:9090"); Response Response = client.deletemetric ("Proc.loadavg.1m"); System.out.println ("*********************delete metric**********************"); System.out.println ("Response Code =" + Response.getstatuscode ()); Client.shutdown ();} /** * Class Description: * * @author: Blank * @date: Date: 2016-5-9 time: Morning 10:30:14 * @param args * @version 1.0 * @throws IOException * @th Rows urisyntaxexception */public static void Main (string[] args) throws URISyntaxException, IOException {getmetricnames ( ); Gettagnames (); Gettagvalues (); Pushmetric ();d eletemetric (); Querydatapoint ();}}
The result of the execution is:
Metricnames**********************response Code =200kairosdb.datastore.query_ Timekairosdb.protocol.telnet_request_countkairosdb.http.ingest_countkairosdb.datastore.query_row_ Countkairosdb.http.ingest_timekairosdb.protocol.http_request_countkairosdb.jvm.thread_countkairosdb.jvm.total_ Memorykairosdb.jvm.max_memorykairosdb.http.request_timekairosdb.jvm.free_memorykairosdb.datastore.query_sample _sizekairosdb.datastore.query_collisionskairosdb.http.query_timekairosdb.metric_countersproc.loadavg.1m******* Tagnames**********************response Code =200methodmetric_namequery_indexrequesthost*********** Tagvalues**********************response Code = 200kairosdbtagvaluesproc.loadavg.1mquerykairosdb.datastore.query_timekairosdb.protocol.telnet_request_ Countdatapointstagnamesputtagsnames12controller0/datapoints/querykairosdb.http.request_timekairosdb.jvm.total_ Memorymetricnamesversionkairosdb.jvm.free_memorykairosdb.datastore.query_sample_sizetagskairosdb. http.query_timekairosdb.jvm.thread_countkairosdb.datastore.query_collisionskairosdb.jvm.max_memory*********** Push metric**********************response Code =204*********************delete metric******************* Response Code =204*********************query datapoint**********************response code =200{"Queries": [{" Sample_size ": 0," results ": [{" Name ":" Kairosdb.http.query_time "," group_by ": [{" Name ":" Type "," type ":" Number "}]," Tags ": {" host ": [" Controller0 "," Kairosdb "]," metric_name ": [" kairosdb.datastore.query_collisions "," Kairosdb.datastore.query_sample_size "," Kairosdb.datastore.query_time "," Kairosdb.http.query_time "," Kairosdb.http.request_time "," Kairosdb.jvm.free_memory "," Kairosdb.jvm.max_memory "," Kairosdb.jvm.thread_count "," Kairosdb.jvm.total_memory "," Kairosdb.protocol.telnet_request_count "," Proc.loadavg.1m "]," Query_index ": [" 1 "," 2 "] , "Request": ["/datapoints/query"]}, "values": []}]}
The above is the result of kairosdb execution, is not easy to operate AH! Let me introduce you to this!
KAIROSDB Client Library