1.maven導入
< dependency>
< groupId> com. github. eulery< / groupId>
< artifactId> opentsdb- java- sdk< / artifactId>
< version> 1.1 .6 < / version>
< / dependency>
2.編寫OpenTsDbUtil類
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. Point;
import org. opentsdb. client. bean. response. DetailResult;
import org. opentsdb. client. http. callback. BatchPutHttpResponseCallback;
import java. io. IOException;
import java. util. List;
public class OpenTsDbUtil {
private static final String OPENTSDB_HOST = "localhost" ;
private static final int OPENTSDB_PORT = 8090 ;
private static final OpenTSDBConfig config;
static {
config = OpenTSDBConfig
. address ( OPENTSDB_HOST, OPENTSDB_PORT)
. httpConnectionPool ( 100 )
. httpConnectTimeout ( 100 )
. batchPutSize ( 50 )
. batchPutBufferSize ( 20000 )
. batchPutTimeLimit ( 300 )
. readonly ( )
. batchPutCallBack ( new BatchPutHttpResponseCallback. BatchPutCallBack ( ) {
@Override
public void response ( List< Point> points, DetailResult result) {
}
@Override
public void responseError ( List< Point> points, DetailResult result) {
}
@Override
public void failed ( List< Point> points, Exception e) {
}
} ) . config ( ) ;
}
public static OpenTSDBClient getClient ( ) {
OpenTSDBClient client = null;
try {
client = OpenTSDBClientFactory. connect ( config) ;
} catch ( IOReactorException e) {
e. printStackTrace ( ) ;
}
return client;
}
public static void insertOne ( OpenTSDBClient client, String metric, String tagName, String tagValue, Number value) {
Long timestamp = System. currentTimeMillis ( ) / 1000 ;
Point point = Point. metric ( metric) . tag ( tagName, tagValue) . value ( timestamp, value) . build ( ) ;
client. put ( point) ;
}
public static void insertMap ( OpenTSDBClient client, String metric, Map< String, String> tags, Number value) {
Long timestamp = System. currentTimeMillis ( ) / 1000 ;
Point point = Point. metric ( metric) . tag ( tags) . value ( timestamp, value) . build ( ) ;
client. put ( point) ;
}
public static void close ( OpenTSDBClient client) {
if ( client != null) {
try {
client. gracefulClose ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
3.插入數據
OpenTSDBClient client = OpenTsDbUtil. getClient ( ) ;
Long timestamp = System. currentTimeMillis ( ) / 1000 ;
Point point = Point. metric ( "point" ) . tag ( "testTag" , "test" ) . value ( timestamp, 1.0 ) . build ( ) ;
client. put ( point) ;
OpenTsDbUtil. close ( client) ;
4.查詢數據
Query query = Query. begin ( "7d-ago" )
. sub ( SubQuery. metric ( "metric.test" )
. aggregator ( SubQuery. Aggregator. NONE)
. build ( ) )
. build ( ) ;
List< QueryResult> resultList = client. query ( query) ;
client. query ( query, new QueryHttpResponseCallback. QueryCallback ( ) {
@Override
public void response ( Query query, List< QueryResult> queryResults) {
}
@Override
public void responseError ( Query query, HttpException e) {
}
@Override
public void failed ( Query query, Exception e) {
}
} ) ;
5.刪除數據
Query query = Query. begin ( "7d-ago" )
. sub ( SubQuery. metric ( "metric.test" )
. aggregator ( SubQuery. Aggregator. NONE)
. build ( ) )
. build ( ) ;
client. delete ( query) ;