时间:2021-07-01 10:21:17 帮助过:4人阅读
可以通过制定的指标名称和时间范围来查询数据点,并可以任选一个或多个标记。查询可以对数据进行处理操作,如聚合,平均值,最小值和最大值的计算。
所有的POST值和返回信息都以JSON格式表示。
在这里我用java代码来进行演示,熟悉java的同学应该很容易看懂
方法:GET
地址:http://[host]:[port]/api/v1/metricnames
body格式: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() { // 定义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(); } } /** * 类描述: * * @author: blank * @date: 日期:2016-5-7 时间:上午10:46:28 * @param args * @version 1.0 */ public static void main(String[] args) { new getMetricNames(); } }结果:Content is:
方法:GET
地址:http://[host]:[port]/api/v1/tagnames
body格式: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"; //初始化HttpClient 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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** *类描述: *@author: blank *@date: 日期:2016-5-7 时间:下午12:15:11 *@param args *@version 1.0 */ public static void main(String[] args) { new getTagNames(); } }
方法:GET
地址:http://[host]:[port]/api/v1/tagvalues
body格式: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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 类描述: * * @author: blank * @date: 日期:2016-5-7 时间:下午12:26:30 * @param args * @version 1.0 */ public static void main(String[] args) { new getTagValues(); } }
方法:post
地址:http://[host]:[port]/api/v1/tagvalues
body格式:
{ "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" } }
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); //将构造的json对象转化为字符串 String transJson = getJSONObject().toString(); System.out.println("Query is:"); System.out.println(transJson); RequestEntity se; try { //构造一个查询的实体 se = new StringRequestEntity(transJson, "application/json", "UTF-8"); //设置请求实体 method.setRequestEntity(se); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } try { //执行post请求 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(); } } 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() { //构造一个查询的json对象 kairosdb kairosdb = getkairosdb(); JSONObject object = JSONObject.fromObject(kairosdb); return object; } /** * 类描述: * * @author: blank * @date: 日期:2016-5-7 时间:下午12:43:21 * @param args * @version 1.0 */ public static void main(String[] args) { new queryMetricByPost(); } }结果:Content is:
以上便是常见的操作。但是你会发现在我们query数据的时候,要传递很复杂的Json字符串作为条件,显然这种方式比较复杂,别担心下一章给大家介绍一个开源库(kairosdb client),编写非常简单。
kairosdb Rest API
标签: