opentsdb使用

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
                // OpenTsDb數據庫地址和端口號
                .address(OPENTSDB_HOST, OPENTSDB_PORT)
                // http連接池大小,默認100
                .httpConnectionPool(100)
                // http請求超時時間,默認100s
                .httpConnectTimeout(100)
                // 異步寫入數據時,每次http提交的數據條數,默認50
                .batchPutSize(50)
                // 異步寫入數據中,內部有一個隊列,默認隊列大小20000
                .batchPutBufferSize(20000)
                // 異步寫入等待時間,如果距離上一次請求超多300ms,且有數據,則直接提交
                .batchPutTimeLimit(300)
                // 當確認這個client只用於查詢時設置,可不創建內部隊列從而提高效率
                .readonly()
                // 每批數據提交完成後回調
                .batchPutCallBack(new BatchPutHttpResponseCallback.BatchPutCallBack() {
                    @Override
                    public void response(List<Point> points, DetailResult result) {
                        // 在請求完成並且response code成功時回調
                    }

                    @Override
                    public void responseError(List<Point> points, DetailResult result) {
                        // 在response code失敗時回調
                    }

                    @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;
    }

    /**
     * 插入單個tag數據
     */
    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);
    }

    /**
     * 插入多個tag數據
     */
    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);
    }

    /**
     * 優雅關閉連接,會等待所有異步操作完成
     *
     * @param client 需要關閉的客戶端
     */
    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) {
        // 在請求完成並且response code成功時回調
    }

    @Override
    public void responseError(Query query, HttpException e) {
        // 在response code失敗時回調
    }

    @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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章