pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.eulery</groupId>
<artifactId>opentsdb-java-sdk</artifactId>
<version>1.1.4</version>
</dependency>
Service:
package com.test.opentsdb;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.nio.reactor.IOReactorException;
import org.opentsdb.client.OpenTSDBClient;
import org.opentsdb.client.OpenTSDBClientFactory;
import org.opentsdb.client.OpenTSDBConfig;
import org.opentsdb.client.bean.request.*;
import org.opentsdb.client.bean.response.DetailResult;
import org.opentsdb.client.bean.response.LastPointQueryResult;
import org.opentsdb.client.bean.response.QueryResult;
import org.opentsdb.client.http.callback.BatchPutHttpResponseCallback;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* 时序库工具类
*/
@Service
@Slf4j
public class OpenTSDBService {
private static String host = "http://127.0.0.1";
private static int port = "";
/**
* 像时序库中插入数据
* @param metricName 相等与一个表明
* @param tag key -value
* @return
*/
public String putData(String metricName, HashMap<String, String> tag, Number version, String time){
OpenTSDBConfig config = OpenTSDBConfig.address(host,port )
.config();
try {
OpenTSDBClient client = OpenTSDBClientFactory.connect(config);
Timestamp stamp = Timestamp.valueOf(time);
Point point = Point.metric(metricName)
.tag(tag)
.value(stamp.getTime(), version)
.build();
client.put(point);
client.gracefulClose();
} catch (IOReactorException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return "success";
}
/**
* 时序库查询(几天前的数据)
* @param days 天数
* @param metricName
* @return
*/
public List<QueryResult> queryData(String metricName,int days) {
OpenTSDBConfig config = OpenTSDBConfig.address(host,port )
.config();
// 同步查询
try {
OpenTSDBClient client = OpenTSDBClientFactory.connect(config);
Query query = Query.begin(days+"d-ago")
.sub(SubQuery.metric(metricName)
.aggregator(SubQuery.Aggregator.NONE)
.build())
.build();
List<QueryResult> resultList = client.query(query);
client.gracefulClose();
return resultList;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* 查询时序库中数据
* @param metricName
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
public List<QueryResult> queryData(String metricName,String startTime,String endTime) {
OpenTSDBConfig config = OpenTSDBConfig.address(host,port )
.config();
// 同步查询
try {
OpenTSDBClient client = OpenTSDBClientFactory.connect(config);
Query query = Query.begin(startTime)
.end(endTime)
.sub(SubQuery.metric(metricName)
.aggregator(SubQuery.Aggregator.NONE)
.build())
.build();
List<QueryResult> resultList = client.query(query);
client.gracefulClose();
return resultList;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* 时序数据按照具体数据查询几小时之前的
* @param metricName
* @param tag
* @param value
* @param backScan 几小时之前
* @return
*/
public List<LastPointQueryResult> queryData(String metricName,HashMap<String,String> tag,int backScan) {
OpenTSDBConfig config = OpenTSDBConfig.address(host,port )
.config();
OpenTSDBClient client = null;
try {
client = OpenTSDBClientFactory.connect(config);
LastPointQuery query = LastPointQuery.sub(LastPointSubQuery.metric(metricName)
.tag(tag)
.build())
// baskScan表示查询最多向前推进多少小时
// 比如在5小时前写入过数据
// 那么backScan(6)可以查出数据,但backScan(4)则不行
.backScan(backScan)
.build();
List<LastPointQueryResult> lastPointQueryResults = client.queryLast(query);
return lastPointQueryResults;
} catch (IOReactorException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public List<LastPointQueryResult> queryData(String metricName,String tagk,String tagv,int backScan) {
OpenTSDBConfig config = OpenTSDBConfig.address(host,port )
.config();
OpenTSDBClient client = null;
try {
client = OpenTSDBClientFactory.connect(config);
LastPointQuery query = LastPointQuery.sub(LastPointSubQuery.metric(metricName)
.tag(tagk,tagv)
.build())
// baskScan表示查询最多向前推进多少小时
// 比如在5小时前写入过数据
// 那么backScan(6)可以查出数据,但backScan(4)则不行
.backScan(backScan)
.build();
List<LastPointQueryResult> lastPointQueryResults = client.queryLast(query);
return lastPointQueryResults;
} catch (IOReactorException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* 删除某条数据
* @param metricName
* @param beginDays 几天之前的数据
* @return
*/
public String deleteData(String metricName,String beginDays){
OpenTSDBConfig config = OpenTSDBConfig.address(host,port )
.config();
try{
OpenTSDBClient client = OpenTSDBClientFactory.connect(config);
Query query = Query.begin(beginDays+"d-ago")
.sub(SubQuery.metric(metricName)
.aggregator(SubQuery.Aggregator.NONE)
.build())
.build();
client.delete(query);
}catch (Exception e){
}
return "sucess";
}
}
有个static 发布不了: