You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2021/03/26 05:54:18 UTC
[iotdb] branch master updated: Improve Python Client (#2898)
This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new c5be711 Improve Python Client (#2898)
c5be711 is described below
commit c5be711a9a9b1862e3bd80e9af4f48348996f031
Author: Julian <j....@pragmaticminds.de>
AuthorDate: Fri Mar 26 06:53:52 2021 +0100
Improve Python Client (#2898)
* Improve Python Client:
* Add / Fix the insert_str_record method for type inference (sends TSInsertStringRecord)
* Add Support for python testcontainer with a specific IoTDB Container.
* Improved the readme a bit
Co-authored-by: Xiangdong Huang <hx...@apache.org>
---
client-py/pypi/README.md | 21 +++++++++++++++-
client-py/src/iotdb/Session.py | 13 +++++++---
client-py/src/iotdb/TestContainer.py | 49 ++++++++++++++++++++++++++++++++++++
3 files changed, 79 insertions(+), 4 deletions(-)
diff --git a/client-py/pypi/README.md b/client-py/pypi/README.md
index dc7182c..257c1d8 100644
--- a/client-py/pypi/README.md
+++ b/client-py/pypi/README.md
@@ -70,4 +70,23 @@ session.open(False)
zone = session.get_time_zone()
session.close()
-```
\ No newline at end of file
+```
+
+## IoTDB Testcontainer
+
+The Test Support is based on the lib `testcontainers` (https://testcontainers-python.readthedocs.io/en/latest/index.html) which you need to install in your project if you want to use the feature.
+
+To start (and stop) an IoTDB Database in a Docker container simply do:
+```
+class MyTestCase(unittest.TestCase):
+
+ def test_something(self):
+ with IoTDBContainer() as c:
+ session = Session('localhost', c.get_exposed_port(6667), 'root', 'root')
+ session.open(False)
+ result = session.execute_query_statement("SHOW TIMESERIES")
+ print(result)
+ session.close()
+```
+
+by default it will load the image `apache/iotdb:latest`, if you want a specific version just pass it like e.g. `IoTDBContainer("apache/iotdb:0.10.0")` to get version `0.10.0` running.
\ No newline at end of file
diff --git a/client-py/src/iotdb/Session.py b/client-py/src/iotdb/Session.py
index 1d7a4a4..e371423 100644
--- a/client-py/src/iotdb/Session.py
+++ b/client-py/src/iotdb/Session.py
@@ -25,7 +25,7 @@ from .utils.IoTDBConstants import *
from thrift.protocol import TBinaryProtocol, TCompactProtocol
from thrift.transport import TSocket, TTransport
-from .thrift.rpc.TSIService import Client, TSCreateTimeseriesReq, TSInsertRecordReq, TSInsertTabletReq, \
+from .thrift.rpc.TSIService import Client, TSCreateTimeseriesReq, TSInsertRecordReq, TSInsertStringRecordReq, TSInsertTabletReq, \
TSExecuteStatementReq, TSOpenSessionReq, TSCreateMultiTimeseriesReq, TSCloseSessionReq, TSInsertTabletsReq, TSInsertRecordsReq, \
TSInsertRecordsOfOneDeviceReq
from .thrift.rpc.ttypes import TSDeleteDataReq, TSProtocolVersion, TSSetTimeZoneReq
@@ -226,8 +226,8 @@ class Session(object):
def insert_str_record(self, device_id, timestamp, measurements, string_values):
""" special case for inserting one row of String (TEXT) value """
data_types = [TSDataType.TEXT.value for _ in string_values]
- request = self.gen_insert_record_req(device_id, timestamp, measurements, data_types, string_values)
- status = self.__client.insertRecord(request)
+ request = self.gen_insert_str_record_req(device_id, timestamp, measurements, data_types, string_values)
+ status = self.__client.insertStringRecord(request)
print("insert one record to device {} message: {}".format(device_id, status.message))
return Session.verify_success(status)
@@ -316,6 +316,13 @@ class Session(object):
values_in_bytes = Session.value_to_bytes(data_types, values)
return TSInsertRecordReq(self.__session_id, device_id, measurements, values_in_bytes, timestamp)
+ def gen_insert_str_record_req(self, device_id, timestamp, measurements, data_types, values):
+ if (len(values) != len(data_types)) or (len(values) != len(measurements)):
+ print("length of data types does not equal to length of values!")
+ # could raise an error here.
+ return
+ return TSInsertStringRecordReq(self.__session_id, device_id, measurements, values, timestamp)
+
def gen_insert_records_req(self, device_ids, times, measurements_lst, types_lst, values_lst):
if (len(device_ids) != len(measurements_lst)) or (len(times) != len(types_lst)) or \
(len(device_ids) != len(times)) or (len(times) != len(values_lst)):
diff --git a/client-py/src/iotdb/TestContainer.py b/client-py/src/iotdb/TestContainer.py
new file mode 100644
index 0000000..c34b242
--- /dev/null
+++ b/client-py/src/iotdb/TestContainer.py
@@ -0,0 +1,49 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+from os import environ
+
+from testcontainers.core.container import DockerContainer
+from testcontainers.core.waiting_utils import wait_container_is_ready
+
+from iotdb.Session import Session
+
+
+class IoTDBContainer(DockerContainer):
+ IOTDB_USER = environ.get("IOTDB_USER", "root")
+ IOTDB_PASSWORD = environ.get("IOTDB_PASSWORD", "root")
+
+ def _configure(self):
+ pass
+
+ @wait_container_is_ready()
+ def _connect(self):
+ session = Session(self.get_container_host_ip(), self.get_exposed_port(6667), 'root', 'root')
+ session.open(False)
+ session.close()
+
+ def __init__(self, image="apache/iotdb:latest", **kwargs):
+ super(IoTDBContainer, self).__init__(image)
+ self.port_to_expose = 6667
+ self.with_exposed_ports(self.port_to_expose)
+
+ def start(self):
+ self._configure()
+ super().start()
+ self._connect()
+ return self