openTSDB写数据的三种方法

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/82468037

openTSDB写数据的三种方法

1.Telnet Style API

见我博客:openTSDB详解之Telnet Style API

2.HTTP API

见我博客【待完善】

3.Java API

3.1 简介

接下来我主要分析一下使用Java API将数据写入到openTSDB中。因为openTSDB官方并没有提供Java API,所以这个API需要使用我们自己开发。但是整个开源世界何其广阔,在你需要使用java往openTSDB写入数据时,早就有前辈遇到这个问题。所以我就从网上下载了一个包,然后继续倒腾。接下来我就针对这个包的代码进行写入数据过程的分析。

3.2 源码分析
public class OpentsdbClientTest {
    private static Logger log = LoggerFactory.getLogger(OpentsdbClientTest.class);
    static OpentsdbClient client = new OpentsdbClient("http://192.168.211.4:4399");

    public static void testPutData() {
        try {
            Map tagMap = new HashMap();
            tagMap.put("aaa", "bbb");
            client.putData("metric-t", DateTimeUtil.parse("20180902 23:10", "yyyyMMdd HH:mm"), 330l, tagMap);
            client.putData("metric-t", DateTimeUtil.parse("20180902 23:06", "yyyyMMdd HH:mm"), 440l, tagMap);
            client.putData("metric-t", DateTimeUtil.parse("20180902 22:50", "yyyyMMdd HH:mm"), 460l, tagMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OpentsdbClientTest是一个测试类,用于测试将数据写入到openTSDB中。
- 使用sl4j的日志对象,记录OpentClientTest这个类。
- 同时OpentsdbClient对象访问的url是”http://192.168.211.4:4399
- 使用testPutData()方法进行数据的写入测试。
testPutData()方法中有一个Map对象tagMap。这个是用于记录tag对。这里我随便使用了一组tag。接着在client端使用putData(…)方法。其中putData(…)方法如下:

/**
     * 写入数据 *
     * @param metric 指标
     * @param timestamp 时间戳
     * @param value type is long
     * @param tagMap
     * @return
     * @throws Exception
     */
    public boolean putData(String metric, Date timestamp, long value, Map tagMap) throws Exception {
        /*
        1.Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
        2.but why did timestamp divide 1000?[译者注:这个地方我不大理解!!!]
         */
        long timsSecs = timestamp.getTime() / 1000;
        //call the simple function putData(...), but the parameter is Long instead of Date.
        return this.putData(metric, timsSecs, value, tagMap);
    }

这个putData(…)方法将Date 型的timestamp转型成long型。并再次调用下面的putData(…)方法:

/**
     * 写入数据
     * @param metric 指标
     * @param timestamp 转化为秒的时间点
     * @param value
     * @param tagMap
     * @return
     * @throws Exception
     */
    public boolean putData(String metric, long timestamp, long value, Map tagMap) throws Exception {
        /* 1.Builder used to create the JSON to push metrics to openTSDB.
         */
        MetricBuilder builder = MetricBuilder.getInstance();
        //addMetric(metric) return on Metric's instance
        //设置metric的数据值、标签
        builder.addMetric(metric).setDataPoint(timestamp, value).addTags(tagMap);

        try {
            log.debug("write quest:{}", builder.build());
            /*1.first,putMetrics()
             */
            Response response = httpClient.pushMetrics(builder, ExpectResponse.SUMMARY);

            //sometimes : 22:00:48.898 [main] DEBUG enn.cn.dataimport.OpentsdbClient - response.statusCode: 200
            log.debug("response.statusCode: {}", response.getStatusCode());
            return response.isSuccess();
        } catch (Exception e) {
            log.error("put data to opentsdb error: ", e);
            throw e;
        }
    }

这里有两个主要的方法调用:
- 使用MetricBuilder构建一个builder实例,这个builder用于构建一个metric。使用addmetric()方法,将传入到方法中的metric添加到其中,然后使用setDataPoint()设置这个metric的数据点,最后为这个数据点添加一个tag 对。
- 最后使用httpClient对象将这个builder对象发送出去。具体的发送方法是putMetrics()。方法如下:

public Response pushMetrics(MetricBuilder builder,
                                ExpectResponse expectResponse) throws IOException {
        //checkNotNull(): Ensures that an object reference passed as a parameter to the calling method is not null.
        //see above: import static com.google.common.base.Preconditions.checkNotNull;
        checkNotNull(builder);

        /*        1.TODO 错误处理,比如IOException或者failed>0,写到队列或者文件后续重试。             */
        SimpleHttpResponse response = httpClient
                .doPost(buildUrl(serviceUrl, PUT_POST_API, expectResponse),
                        builder.build());
        return getResponse(response);
    }

因为导入了checkNotNull()所在的方法包,所以可以直接使用checkNotNull(builder)方法。那这里的httpClient又是个什么呢?看下面的这个定义:
private PoolingHttpClient httpClient = new PoolingHttpClient();
as the name,PoolingHttpClient is a http client pool。需要记住这个httpClient是一个连接到远端服务器的client端即可。
然后使用doPost方法将整个builder发送出去。这里builder.build()方法如下:

    /**
     * Returns the JSON string built by the builder. This is the JSON that can
     * be used by the client add metrics.
     *
     * @return JSON
     * @throws IOException
     *             if metrics cannot be converted to JSON
     */
    public String build() throws IOException {
        // verify that there is at least one tag for each metric
        for (Metric metric : metrics) {         
            checkState(metric.getTags().size() > 0, metric.getName()
                    + " must contain at least one tag.");
        }
        /*
           1.toJson(): This method serializes the specified object into its equivalent Json representation.
         */
        return mapper.toJson(metrics);
    }
3.3 执行结果

写入到openTSDB之后,可以打开浏览器查看写入效果,如下:
这里写图片描述

4.注

有需要jar包的可留言告知博主。

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/82468037