Kairosdb Rest API

Source: Internet
Author: User

Today to share with you about the operation of Kairosdb server through the rest API, this is a key part of learning Kairosdb Introduction, I hope you can seriously study, to learn this part of the content requires us to do a good job before the environment. The address of my KAIROSDB server's HTTP URL is "http://10.20.0.10:9090" and will be followed by this connection to manipulate kairosdb.

The KAIROSDB REST API provides operations on a range of existing indicator names, tag names and values, storage metrics data points, and query metric data points.

You can query data points by setting the name of the indicator and time range, and you can select one or more tags. A query can manipulate data, such as aggregations, averages, minimums, and the calculation of maximum values.

All post values and return information are expressed in JSON format.

Here I use Java code to demonstrate, familiar with Java students should be easy to read

List all indicator names

Method: GET

Address: Http://[host]:[port]/api/v1/metricnames

Body Format: None

Package Kairosdb.metric.com;import Java.io.ioexception;import Org.apache.commons.httpclient.httpclient;import Org.apache.commons.httpclient.httpexception;import Org.apache.commons.httpclient.httpstatus;import Org.apache.commons.httpclient.methods.getmethod;public class Getmetricnames {public Getmetricnames () {// Defines an instance of httpclient string url= "Http://10.20.0.10:9090/api/v1/metricnames"; HttpClient HttpClient = new HttpClient (); GetMethod method=new getmethod (URL); Httpclient.gethttpconnectionmanager (). Getparams (). Setconnectiontimeout (2000) ; try {int Statuscode=httpclient.executemethod (method); if (STATUSCODE==HTTPSTATUS.SC_OK) {String body= Method.getresponsebodyasstring (); System.out.println ("Content is:"); System.out.println (body);} Else{system.out.println ("Something is error!");}} catch (HttpException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} /** * Class Description: * * @author: Blank * @date: Date: 2016-5-7 time: Morning 10:46:28 * @param args * @version 1.0 */public static void main (S Tring[] args) {new Getmetricnames ();}} 
Result: Content is:
{"Results": ["Kairosdb.datastore.query_time", "Kairosdb.protocol.telnet_request_count", "Kairosdb.http.ingest_ Count "," Kairosdb.datastore.query_row_count "," Kairosdb.http.ingest_time "," Kairosdb.protocol.http_request_count " , "Kairosdb.jvm.thread_count", "Kairosdb.jvm.total_memory", "Kairosdb.jvm.max_memory", "Kairosdb.http.request_time "," Kairosdb.jvm.free_memory "," Kairosdb.datastore.query_sample_size "," Kairosdb.datastore.query_collisions "," Kairosdb.http.query_time "," Kairosdb.metric_counters "," Proc.loadavg.1m "]}


List all tag names

Method: GET

Address: Http://[host]:[port]/api/v1/tagnames

Body Format: None

Package Kairosdb.tag.com;import Java.io.ioexception;import Org.apache.commons.httpclient.httpclient;import Org.apache.commons.httpclient.httpexception;import Org.apache.commons.httpclient.httpstatus;import Org.apache.commons.httpclient.methods.getmethod;public class Gettagnames {public Gettagnames () {String url= "/http 10.20.0.10:9090/api/v1/tagnames ";//Initialize httpclienthttpclient httpclient=new httpclient (); GetMethod method=new getmethod (URL); try {int Statuscode=httpclient.executemethod (method); if (statuscode== HTTPSTATUS.SC_OK) {String body=method.getresponsebodyasstring (); System.out.println ("Content is:"); System.out.println (body);} Else{system.out.println ("Something is error!");}} catch (HttpException E) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gener Ated catch Blocke.printstacktrace ();}} /** * Class Description: * @author: Blank * @date: Date: 2016-5-7 time: PM 12:15:11 * @param args * @version 1.0 */public static void Main (string[] args) {new Gettagnames ();}}
List all Tagged values

Method: GET

Address: Http://[host]:[port]/api/v1/tagvalues

Body Format: None

Package Kairosdb.tag.com;import Java.io.ioexception;import Org.apache.commons.httpclient.httpclient;import Org.apache.commons.httpclient.httpexception;import Org.apache.commons.httpclient.httpstatus;import Org.apache.commons.httpclient.methods.getmethod;public class Gettagvalues {public gettagvalues () {String URL = "http:/ /10.20.0.10:9090/api/v1/tagvalues "; HttpClient HttpClient = new HttpClient (); GetMethod method = new GetMethod (URL); try {int statusCode = Httpclient.executemethod (method); if (StatusCode = = Httpstatus . SC_OK) {String BODY = method.getresponsebodyasstring (); System.out.println ("Content is:"); System.out.println (body);} else {System.out.println ("Something is error!");}} catch (HttpException E) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gener Ated catch Blocke.printstacktrace ();}} /** * Class Description: * * @author: Blank * @date: Date: 2016-5-7 time: PM 12:26:30 * @param args * @version 1.0 */public static void main (S Tring[] (args) {new GettagvaluEs ();}} 


Querying the Datapoint of metric

Method: Post

Address: Http://[host]:[port]/api/v1/tagvalues

Body Format:

{"  metrics": [    {"      tags": {        "host": [          "Kairosdb"        ]      },      "name": " Kairosdb.datastore.query_collisions ",      " aggregators ": [        {          " name ":" Sum ",          " align_sampling ": true,          "sampling": {            "value": "1",            "unit": "Milliseconds"          }      ]    }  ],  " Cache_time ": 0,  " start_relative ": {    " value ":" 5 ",    " unit ":" Minutes "  }}

Code:

Package Kairosdb.metric.com;import Java.io.ioexception;import Java.io.unsupportedencodingexception;import Net.sf.json.jsonobject;import Org.apache.commons.httpclient.httpclient;import Org.apache.commons.httpclient.httpexception;import Org.apache.commons.httpclient.httpstatus;import Org.apache.commons.httpclient.methods.postmethod;import org.apache.commons.httpclient.methods.RequestEntity; Import Org.apache.commons.httpclient.methods.stringrequestentity;public class Querymetricbypost {public Querymetricbypost () {String URL = "Http://10.20.0.10:9090/api/v1/datapoints/query"; HttpClient HttpClient = new HttpClient (); Postmethod method = new Postmethod (URL);//Convert the constructed JSON object to string Transjson = Getjsonobject (). toString (); System.out.println ("Query is:"); System.out.println (Transjson); Requestentity Se;try {//Constructs a query entity SE = new stringrequestentity (Transjson, "Application/json", "UTF-8");// Set the request entity method.setrequestentity (SE);} catch (Unsupportedencodingexception E1) {e1.printstacktrace ();} try {//execute POST request int statusCode = Httpclient.executemethod (method); if (StatusCode = = HTTPSTATUS.SC_OK) {//Get to result string BODY = Method.getresponsebodyasstring (); System.out.println ("Content is:"); System.out.println (body);} else {System.out.println ("Something is error!");}} catch (HttpException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} public class Metrics {public tags tags;public tags getTags () {return this.tags;} public void Settags (tags tags) {this.tags = tags;}        public String name;        Public String GetName () {return this.name; }public void SetName (String name) {this.name = name;}        Public aggregators[] aggregators;        Public aggregators[] Getaggregators () {return this.aggregators; }public void Setaggregators (aggregators[] aggregators) {this.aggregators = aggregators;}}        public class Kairosdb {public metrics[] metrics;        Public metrics[] Getmetrics () {return this.metrics; }public void Setmetrics (metrics[] metrics) {This.metrics = metrics;}        public int cache_time;        public int Getcache_time () {return this.cache_time; }public void setcache_time (int cache_time) {this.cache_time = Cache_time;}        Public start_relative start_relative;        Public start_relative getstart_relative () {return this.start_relative; }public void Setstart_relative (start_relative start_relative) {this.start_relative = start_relative;}}        public class Start_relative {public String value;        Public String GetValue () {return this.value; }public void SetValue (String value) {this.value = value;}        public String Unit;        Public String Getunit () {return this.unit; }public void Setunit (String unit) {this.unit = unit;}}        public class Tags {public string[] host;        Public string[] GetHost () {return this.host; }public void Sethost (string[] host) {this.host = host;}} public class Sampling {public String value;public string Unit;public string GetValue () {return this.value;} Public void SetValue (String value) {this.value = value;} Public String Getunit () {return this.unit;} public void Setunit (String unit) {this.unit = unit;}}        public class Aggregators {public String name;        Public String GetName () {return this.name; }public void SetName (String name) {this.name = name;}        public Boolean align_sampling;        public Boolean getalign_sampling () {return this.align_sampling; }public void Setalign_sampling (Boolean align_sampling) {this.align_sampling = align_sampling;}        public sampling sampling;        Public sampling getsampling () {return this.sampling; }public void setsampling (sampling sampling) {this.sampling = sampling;}} Public Kairosdb Getkairosdb () {start_relative start_relative = new start_relative (); Start_relative.setvalue ("5"); Start_relative.setunit ("minutes"); sampling sampling = new sampling (); Sampling.setvalue ("1"); Sampling.setunit (" Milliseconds "); aggregators aggregator = new aggregators (); Aggregator.setsampling (Sampling); aggregator.setalign_sampling (true); Aggregator.setname ("sum"); tags tags = new tags (); String[] host = {"Kairosdb"};tags.sethost (host); Metrics metric = new metrics (); metric.settags (tags); aggregators[] aggregators = new aggregators[] {aggregator};metric.setaggregators (aggregators); Metric.setname (" Kairosdb.http.query_time "); Kairosdb kairosdb = new Kairosdb (); kairosdb.setcache_time (0); metrics[] metrics = new Metrics[] {metric};kairosdb.setmetrics (metrics); kairosdb.setstart_relative (start_relative); return kairosdb;} Public Jsonobject Getjsonobject () {//Constructs a query JSON object Kairosdb kairosdb = Getkairosdb (); Jsonobject object = Jsonobject.fromobject (Kairosdb); return object;} /** * Class Description: * * @author: Blank * @date: Date: 2016-5-7 time: PM 12:43:21 * @param args * @version 1.0 */public static void main (S Tring[] (args) {new Querymetricbypost ();}}
Result: Content is:
{"Queries": [{"sample_size": 0, "results": [{"Name": "Kairosdb.http.query_time", "group_by": [{"Name": "Type", "type": " Number "}", "tags": {"host": ["Kairosdb"], "metric_name": ["kairosdb.datastore.query_collisions", " Kairosdb.datastore.query_time "," Kairosdb.http.query_time "," Kairosdb.http.request_time "," 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 ": []}}]}


These are common operations. But you will find that when we query the data, it is very complicated to pass a very complex JSON string, and don't worry about the next chapter to introduce an open source library (KAIROSDB client), which is very simple to write.



Kairosdb Rest API

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.