04-opentsdb-json.mdx 3.0 KB
Newer Older
D
dingbo 已提交
1
---
G
gccgdb1234 已提交
2 3
sidebar_label: OpenTSDB JSON Protocol
title: OpenTSDB JSON Protocol
D
dingbo 已提交
4 5 6 7 8 9 10 11 12 13 14 15
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
import JavaJson from "./_java_opts_json.mdx";
import PyJson from "./_py_opts_json.mdx";
import GoJson from "./_go_opts_json.mdx";
import RustJson from "./_rust_opts_json.mdx";
import NodeJson from "./_js_opts_json.mdx";
import CsJson from "./_cs_opts_json.mdx";
import CJson from "./_c_opts_json.mdx";

G
gccgdb1234 已提交
16
## Introduction
D
dingbo 已提交
17

D
dingbo 已提交
18
A JSON string is used in OpenTSDB JSON to represent one or more rows of data, for example:
D
dingbo 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

```json
[
    {
        "metric": "sys.cpu.nice",
        "timestamp": 1346846400,
        "value": 18,
        "tags": {
           "host": "web01",
           "dc": "lga"
        }
    },
    {
        "metric": "sys.cpu.nice",
        "timestamp": 1346846400,
        "value": 9,
        "tags": {
           "host": "web02",
           "dc": "lga"
        }
    }
]
```

G
gccgdb1234 已提交
43
Similar to OpenTSDB line protocol, `metric` will be used as the STable name, `timestamp` is the timestamp to be used, `value` represents the metric collected, `tags` are the tag sets.
D
dingbo 已提交
44 45


G
gccgdb1234 已提交
46
Please refer to [OpenTSDB HTTP API](http://opentsdb.net/docs/build/html/api_http/put.html) for more details.
D
dingbo 已提交
47 48

:::note
G
gccgdb1234 已提交
49 50
- In JSON protocol, strings will be converted to nchar type and numeric values will be converted to double type.
- Only data in array format is accepted, array must be used even there is only one row.
D
dingbo 已提交
51 52 53

:::

G
gccgdb1234 已提交
54
## Examples
D
dingbo 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

<Tabs defaultValue="java" groupId="lang">
  <TabItem label="Java" value="java">
    <JavaJson />
  </TabItem>
  <TabItem label="Python" value="Python">
    <PyJson />
  </TabItem>
  <TabItem label="Go" value="go">
    <GoJson />
  </TabItem>
  <TabItem label="Rust" value="rust">
    <RustJson />
  </TabItem>
  <TabItem label="Node.js" value="nodejs">
    <NodeJson />
  </TabItem>
  <TabItem label="C#" value="csharp">
    <CsJson />
  </TabItem>
  <TabItem label="C" value="c">
    <CJson />
  </TabItem>
</Tabs>

G
gccgdb1234 已提交
80
The above sample code will created 2 STables automatically while each STable has 2 rows of data.
D
dingbo 已提交
81 82 83 84 85

```cmd
taos> use test;
Database changed.

G
gccgdb1234 已提交
86
taos> show STables;
D
dingbo 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
              name              |      created_time       | columns |  tags  |   tables    |
============================================================================================
 meters.current                 | 2022-03-29 16:05:25.193 |       2 |      2 |           1 |
 meters.voltage                 | 2022-03-29 16:05:25.200 |       2 |      2 |           1 |
Query OK, 2 row(s) in set (0.001954s)

taos> select * from `meters.current`;
           ts            |           value           |          groupid          |            location            |
===================================================================================================================
 2022-03-28 09:56:51.249 |              10.300000000 |               2.000000000 | Beijing.Chaoyang               |
 2022-03-28 09:56:51.250 |              12.600000000 |               2.000000000 | Beijing.Chaoyang               |
Query OK, 2 row(s) in set (0.004076s)
```