kairosdb Rest API

2022-04-24 00:00:00 查询 操作 指标 地址 格式

今天和大家分享一下有关通过Rest API方式操作kairosdb server的相关内容,这是学习kairosdb入门很关键的部分,希望大家可以认真学习,要学习这部分内容需要我们做好之前环境搭建的工作。我的kairosdb server的http url的地址是“http://10.20.0.10:9090”,后面将通过这个连接来操作kairosdb。

KairosDB REST API提供了对一系列已存在的指标名、标记名和值、存储指标数据点和查询指标数据点的操作。

可以通过制定的指标名称和时间范围来查询数据点,并可以任选一个或多个标记。查询可以对数据进行处理操作,如聚合,平均值,小值和大值的计算。

所有的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:
{"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"]}

列出所有标记名
方法: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();
}

}


查询Metric的Datapoint
方法: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:
{"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":[]}]}]}

以上便是常见的操作。但是你会发现在我们query数据的时候,要传递很复杂的Json字符串作为条件,显然这种方式比较复杂,别担心下一章给大家介绍一个开源库(kairosdb client),编写非常简单。

相关文章