kairosdb client库

2022-04-24 00:00:00 集合 专区 订阅 源码 获取

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 is used by the client.
源码下载地址:https://github.com/kairosdb/kairosdb-client/

将源码集成到你的java项目中


红色部分就是源码部分。

我们建立一个class来测试操作

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 {

// 获取metric的name集合
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();
}
}

// 获取Tag的name集合
public 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();
}
}

// 获取Tag的值集合
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();
}
}

// pushMetric
public static void pushMetric() throws URISyntaxException, IOException {
MetricBuilder builder = MetricBuilder.getInstance();
builder.addMetric("proc.loadavg.1m").addTag("host", "kairosdb")
.addDataPoint(System.currentTimeMillis(), 10)
.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();
}

// 查询数据节点
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();
}

// 删除Metric中的数据点
public static void deleteMetric() throws IOException {
// MetricBuilder builder = MetricBuilder.getInstance();
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();
}

/**
* 类描述:
*
* @author: blank
* @date: 日期:2016-5-9 时间:上午10:30:14
* @param args
* @version 1.0
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws URISyntaxException, IOException {
getMetricNames();
getTagNames();
getTagValues();
pushMetric();
deleteMetric();
queryDataPoint();

}

}

执行结果为:
*********************MetricNames**********************
Response Code =200
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
*********************TagNames**********************
Response Code =200
method
metric_name
query_index
request
host
*********************TagValues**********************
Response Code =200
kairosdb
tagvalues
proc.loadavg.1m
query
kairosdb.datastore.query_time
kairosdb.protocol.telnet_request_count
datapoints
tagnames
put
tagsnames
1
2
controller0
/datapoints/query
kairosdb.http.request_time
kairosdb.jvm.total_memory
metricnames
version
kairosdb.jvm.free_memory
kairosdb.datastore.query_sample_size
tags
kairosdb.http.query_time
kairosdb.jvm.thread_count
kairosdb.datastore.query_collisions
kairosdb.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":[]}]}]}

以上就是kairosdb执行的结果,是不是操作起来比较简单啊!先介绍到这吧!

相关文章