50-opentsdb-json.mdx 3.5 KB
Newer Older
1 2
---
title: OpenTSDB JSON Protocol
D
danielclow 已提交
3 4
sidebar_label: OpenTSDB JSON Protocol
description: This document describes how to insert data into TDengine using the OpenTSDB JSON protocol.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
---

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";

## Introduction

19
A JSON string is used in OpenTSDB JSON to represent one or more rows of data, for example: For example:
20 21 22

```json
[
23 24 25 26 27 28 29
  {
    "metric": "sys.cpu.nice",
    "timestamp": 1346846400,
    "value": 18,
    "tags": {
      "host": "web01",
      "dc": "lga"
30
    }
31 32 33 34 35 36 37 38 39 40
  },
  {
    "metric": "sys.cpu.nice",
    "timestamp": 1346846400,
    "value": 9,
    "tags": {
      "host": "web02",
      "dc": "lga"
    }
  }
41 42 43 44 45 46 47 48
]
```

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.

Please refer to [OpenTSDB HTTP API](http://opentsdb.net/docs/build/html/api_http/put.html) for more details.

:::note
49

50
- In JSON protocol, strings will be converted to NCHAR type and numeric values will be converted to double type.
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
51
- The child table name is created automatically in a rule to guarantee its uniqueness. But you can configure `smlChildTableName` in taos.cfg to specify a tag value as the table names if the tag value is unique globally. For example, if a tag is called `tname` and you set `smlChildTableName=tname` in taos.cfg, when you insert `st,tname=cpu1,t1=4 c1=3 1626006833639000000`, the child table `cpu1` will be automatically created. Note that if multiple rows have the same tname but different tag_set values, the tag_set of the first row is used to create the table and the others are ignored.
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
52
:::
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

## Examples

<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="Node.js" value="nodejs">
    <NodeJson />
  </TabItem>
  <TabItem label="C#" value="csharp">
    <CsJson />
  </TabItem>
  <TabItem label="C" value="c">
    <CJson />
  </TabItem>
</Tabs>

77
2 STables will be created automatically and each STable has 2 rows of data in the above sample code.
78 79 80 81 82

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

83
taos> show stables;
84 85 86 87
              name              |
=================================
 meters.current                 |
 meters.voltage                 |
88 89 90
Query OK, 2 row(s) in set (0.001954s)

taos> select * from `meters.current`;
wmmhello's avatar
wmmhello 已提交
91
           _ts            |           _value           |          groupid          |            location            |
92
===================================================================================================================
G
gccgdb1234 已提交
93 94
 2022-03-28 09:56:51.249 |              10.300000000 |               2.000000000 | California.SanFrancisco               |
 2022-03-28 09:56:51.250 |              12.600000000 |               2.000000000 | California.SanFrancisco               |
95 96
Query OK, 2 row(s) in set (0.004076s)
```
97

wmmhello's avatar
wmmhello 已提交
98
## Query Examples
99 100 101 102 103 104

If you want query the data of "tags": {"location": "California.LosAngeles", "groupid": 1},here is the query SQL:

```sql
SELECT * FROM `meters.current` WHERE location = "California.LosAngeles" AND groupid = 3;
```