From b27bfa1dce3865c5312741dfd0be16bb77fe5911 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 9 Sep 2021 09:22:13 +0800 Subject: [PATCH] [TD-6560]: Add unit test cases for parsing OpenTSDB HTTP JSON data import format --- src/connector/python/taos/cinterface.py | 8 +++++++- src/connector/python/taos/connection.py | 19 +++++++++++++++++++ src/connector/python/taos/error.py | 12 +++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/connector/python/taos/cinterface.py b/src/connector/python/taos/cinterface.py index fadef7c397..7977a6bc02 100644 --- a/src/connector/python/taos/cinterface.py +++ b/src/connector/python/taos/cinterface.py @@ -838,8 +838,14 @@ def taos_insert_telnet_lines(connection, lines): p_lines = lines_type(*lines) errno = _libtaos.taos_insert_telnet_lines(connection, p_lines, num_of_lines) if errno != 0: - raise LinesError("insert telnet lines error", errno) + raise TelnetLinesError("insert telnet lines error", errno) +def taos_insert_json_payload(connection, payload): + # type: (c_void_p, list[str] | tuple(str)) -> None + payload = payload.encode("utf-8") + errno = _libtaos.taos_insert_json_payload(connection, payload) + if errno != 0: + raise JsonPayloadError("insert json payload error", errno) class CTaosInterface(object): def __init__(self, config=None): diff --git a/src/connector/python/taos/connection.py b/src/connector/python/taos/connection.py index a8a71ecc3a..35aca1fb26 100644 --- a/src/connector/python/taos/connection.py +++ b/src/connector/python/taos/connection.py @@ -154,6 +154,25 @@ class TaosConnection(object): """ return taos_insert_telnet_lines(self._conn, lines) + def insert_json_payload(self, payload): + """OpenTSDB HTTP JSON format support + + ## Example + "{ + "metric": "cpu_load_0", + "timestamp": 1626006833610123, + "value": 55.5, + "tags": + { + "host": "ubuntu", + "interface": "eth0", + "Id": "tb0" + } + }" + + """ + return taos_insert_json_payload(self._conn, payload) + def cursor(self): # type: () -> TaosCursor """Return a new Cursor object using the connection.""" diff --git a/src/connector/python/taos/error.py b/src/connector/python/taos/error.py index a30adbb162..f6a9d41f56 100644 --- a/src/connector/python/taos/error.py +++ b/src/connector/python/taos/error.py @@ -83,4 +83,14 @@ class ResultError(DatabaseError): class LinesError(DatabaseError): """taos_insert_lines errors.""" - pass \ No newline at end of file + pass + +class TelnetLinesError(DatabaseError): + """taos_insert_telnet_lines errors.""" + + pass + +class JsonPayloadError(DatabaseError): + """taos_insert_json_payload errors.""" + + pass -- GitLab