VictoriaMetrics时序数据库(TSDB)的使用

1:通过docker拉去镜像,设置挂载路径,设置数据保存天数,设置http数据接收端口,如下直接运行即可

docker run -d -p 4242:4242 -p 8428:8428  -v /opt/victoria:/victoria-metrics-data --name victoria victoriametrics/victoria-metrics:latest --opentsdbHTTPListenAddr=:4242 --retentionPeriod=36d

也可以设置文件的读写权重

docker run -d -p 4242:4242 -p 8428:8428  -v /opt/victoria:/victoria-metrics-data:rw --name victoria victoriametrics/victoria-metrics:latest --opentsdbHTTPListenAddr=:4242 --retentionPeriod=36d

2:发送数据到tsdb

2-1:windows环境下

curl -H "Content-Type: application/json" -d "{\"metric\":\"x.y.z\",\"value\":45.34,\"tags\":{\"t1\":\"v1\",\"t2\":\"v2\"}}" http://localhost:4242/api/put

2-2:linux环境下

curl -H 'Content-Type: application/json' -d '{"metric":"x.y.z","value":45.34,"tags":{"t1":"v1","t2":"v2"}}' http://localhost:4242/api/put

3:查询tsdb的数据

GET | POST http://localhost:8428/api/v1/query?query=...&time=...&step=...
GET | POST http://localhost:8428/api/v1/query_range?query=...&start=...&end=...&step=...

查询延迟

默认情况下,Victoria Metrics 不会立即返回最近写入的样本。-search.latencyOffset相反,它检索在命令行标志指定的时间之前写入的最后结果,该时间的默认偏移量为 30 秒。query对于和来说都是如此,query_range并且可能会给人这样的印象:数据写入 VM 时有 30 秒的延迟
此标志可防止由于在最后的抓取间隔中仅抓取部分值而导致不一致的结果

3:设置账号密码登录vmui与推送与查询使用账号密码

docker run -d -p 4242:4242 -p 8428:8428  -v C:/victoria:/victoria-metrics-data --name victoria victoriametrics/victoria-metrics:latest --opentsdbHTTPListenAddr=:4242 --retentionPeriod=36d  --httpAuth.username=admin -httpAuth.password=123456

3-1:使用基础java类推送数据

	public static void main2(String[] args) {
        String url = "http://localhost:4242/api/put";
        String username = "admin";
        String password = "123456";

        // 构建请求JSON数据
        String jsonData = "{\"metric\":\"point\",\"value\":45.34,\"tags\":{\"t1\":\"v1\",\"t2\":\"v2\"}}";

        try {
            URL obj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

            // 设置请求方法为 POST
            conn.setRequestMethod("POST");

            // 设置基本身份验证信息
            String auth = username + ":" + password;
            byte[] encodedAuth = java.util.Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
            String authHeaderValue = "Basic " + new String(encodedAuth);
            conn.setRequestProperty("Authorization", authHeaderValue);

            // 设置请求头为 JSON 格式
            conn.setRequestProperty("Content-Type", "application/json");

            // 启用输出流,并写入请求体数据
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(jsonData.getBytes(StandardCharsets.UTF_8));
            os.flush();
            os.close();

            // 获取响应
            int responseCode = conn.getResponseCode();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 输出响应结果
            System.out.println("Response Code: " + responseCode);
            System.out.println("Response Body: " + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3-2:使用hutool工具集推送数据

    public static void main(String[] args) {
        String url = "http://localhost:4242/api/put";
        String username = "admin";
        String password = "123456";

        // 构建请求JSON数据
        String jsonData = "{\"metric\":\"point\",\"value\":40.34,\"tags\":{\"t1\":\"v1\",\"t2\":\"v2\"}}";

        // 发送POST请求
        HttpResponse response = HttpRequest.post(url)
                .basicAuth(username, password)
                .body(jsonData)
                .header("Content-Type", "application/json")
                .execute();

        // 获取响应
        int statusCode = response.getStatus();
        String responseBody = response.body();

        // 输出响应结果
        System.out.println("Response Code: " + statusCode);
        System.out.println("Response Body: " + responseBody);
    }

<dependency>
    <groupId>cn.hutoolgroupId>
    <artifactId>hutool-httpartifactId>
    <version>5.7.6version>
dependency>

你可能感兴趣的:(时序数据库,数据库)