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 發佈不了: