diff --git a/documentation20/cn/08.connector/docs.md b/documentation20/cn/08.connector/docs.md index ee5d8e9f825e12bd65331736ecb23db62e5fe388..ecd9770e6a52ce06440d4788daaa527b194b5fef 100644 --- a/documentation20/cn/08.connector/docs.md +++ b/documentation20/cn/08.connector/docs.md @@ -749,6 +749,49 @@ conn.execute("drop database pytest") conn.close() ``` +#### JSON 类型 + +从 `taospy` `v2.2.0` 开始,Python连接器开始支持 JSON 数据类型的标签(TDengine版本要求 Beta 版 2.3.5+, 稳定版 2.4.0+)。 + +创建一个使用JSON类型标签的超级表及其子表: + +```python +# encoding:UTF-8 +import taos + +conn = taos.connect() +conn.execute("create database if not exists py_test_json_type") +conn.execute("use py_test_json_type") + +conn.execute("create stable s1 (ts timestamp, v1 int) tags (info json)") +conn.execute("create table s1_1 using s1 tags ('{\"k1\": \"v1\"}')") +``` + +查询子表标签及表名: + +```python +tags = conn.query("select info, tbname from s1").fetch_all_into_dict() +tags +``` + +`tags` 内容为: + +```python +[{'info': '{"k1":"v1"}', 'tbname': 's1_1'}] +``` + +获取 JSON 中某值: + +```python +k1 = conn.query("select info->'k1' as k1 from s1").fetch_all_into_dict() +""" +>>> k1 +[{'k1': '"v1"'}] +""" +``` + +更多JSON类型的操作方式请参考 [JSON 类型使用说明](https://www.taosdata.com/cn/documentation/taos-sql)。 + #### 关于纳秒 (nanosecond) 在 Python 连接器中的说明 由于目前 Python 对 nanosecond 支持的不完善(参见链接 1. 2. ),目前的实现方式是在 nanosecond 精度时返回整数,而不是 ms 和 us 返回的 datetime 类型,应用开发者需要自行处理,建议使用 pandas 的 to_datetime()。未来如果 Python 正式完整支持了纳秒,涛思数据可能会修改相关接口。 diff --git a/documentation20/en/08.connector/docs.md b/documentation20/en/08.connector/docs.md index bc1197d2ed3c4c566e9618b505183123b1e31eb9..f8b444281587e03bb0b143d5ecd1c41abed9dd64 100644 --- a/documentation20/en/08.connector/docs.md +++ b/documentation20/en/08.connector/docs.md @@ -575,6 +575,49 @@ Close connection. conn.close() ``` +#### JSON Type Support + +Python connector `taospy` starts supporting JSON type as tags since `v2.2.0` (requires TDengine beta v2.3.5+, or stable v2.4.0+). + +Create stable and table with JSON tag. + +```python +# encoding:UTF-8 +import taos + +conn = taos.connect() +conn.execute("create database if not exists py_test_json_type") +conn.execute("use py_test_json_type") + +conn.execute("create stable s1 (ts timestamp, v1 int) tags (info json)") +conn.execute("create table s1_1 using s1 tags ('{\"k1\": \"v1\"}')") +``` + +Query JSON tag and table name from a stable. + +```python +tags = conn.query("select info, tbname from s1").fetch_all_into_dict() +tags +``` + +The `tags` value is: + +```python +[{'info': '{"k1":"v1"}', 'tbname': 's1_1'}] +``` + +To get value from JSON tag by key: + +```python +k1 = conn.query("select info->'k1' as k1 from s1").fetch_all_into_dict() +""" +>>> k1 +[{'k1': '"v1"'}] +""" +``` + +Refer to [JSON type instructions](https://www.taosdata.com/en/documentation/taos-sql) for more usage of JSON type. + #### Using nanosecond in Python connector So far Python still does not completely support nanosecond type. Please refer to the link 1 and 2. The implementation of the python connector is to return an integer number for nanosecond value rather than datatime type as what ms and us do. The developer needs to handle it themselves. We recommend using pandas to_datetime() function. If Python officially support nanosecond in the future, TAOS Data might be possible to change the interface accordingly, which mean the application need change too.