This is an automated email from the ASF dual-hosted git repository. jfeinauer pushed a commit to branch feature/improve-py-client in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 468c36e468c3494aa953d701e10eeebcae6b6089 Author: julian <[email protected]> AuthorDate: Wed Mar 24 11:00:37 2021 +0100 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 --- client-py/pypi/README.md | 21 ++++++++++++++++++++- client-py/src/iotdb/Session.py | 13 ++++++++++--- client-py/src/iotdb/TestContainer.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 61 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..8230d6a --- /dev/null +++ b/client-py/src/iotdb/TestContainer.py @@ -0,0 +1,31 @@ +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 \ No newline at end of file
