From a38532c909e84030f199cf3016030500267b4373 Mon Sep 17 00:00:00 2001 From: Leto_b Date: Wed, 25 Mar 2026 18:48:10 +0800 Subject: [PATCH] update python example code --- .../Programming-Python-Native-API_apache.md | 44 ++++++++++--------- .../Programming-Python-Native-API_timecho.md | 44 ++++++++++--------- .../Programming-Python-Native-API_apache.md | 29 ++++++------ .../Programming-Python-Native-API_timecho.md | 29 ++++++------ .../Programming-Python-Native-API_apache.md | 44 ++++++++++--------- .../Programming-Python-Native-API_timecho.md | 44 ++++++++++--------- .../Programming-Python-Native-API_apache.md | 29 ++++++------ .../Programming-Python-Native-API_timecho.md | 29 ++++++------ .../Programming-Python-Native-API_apache.md | 44 ++++++++++--------- .../Programming-Python-Native-API_timecho.md | 44 ++++++++++--------- .../Programming-Python-Native-API_apache.md | 30 ++++++------- .../Programming-Python-Native-API_timecho.md | 27 ++++++------ .../Programming-Python-Native-API_apache.md | 44 ++++++++++--------- .../Programming-Python-Native-API_timecho.md | 44 ++++++++++--------- .../Programming-Python-Native-API_apache.md | 30 ++++++------- .../Programming-Python-Native-API_timecho.md | 27 ++++++------ 16 files changed, 294 insertions(+), 288 deletions(-) diff --git a/src/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md b/src/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md index 568102fb5..1b1e629d4 100644 --- a/src/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md +++ b/src/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md @@ -338,11 +338,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -361,9 +362,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -374,9 +376,9 @@ if __name__ == "__main__": ## 4. Sample Code -**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py). +**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py). -**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py). +**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_pool_example.py). Here is an excerpt of the sample code: @@ -427,9 +429,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -484,14 +486,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -500,9 +502,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md b/src/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md index 7149b6dc8..b9193eaaa 100644 --- a/src/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md +++ b/src/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md @@ -338,11 +338,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -361,9 +362,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -374,9 +376,9 @@ if __name__ == "__main__": ## 4. Sample Code -**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py). +**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py). -**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py). +**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_pool_example.py). Here is an excerpt of the sample code: @@ -427,9 +429,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -484,14 +486,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -500,9 +502,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md b/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md index df5123d32..02b831655 100644 --- a/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md +++ b/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md @@ -33,9 +33,9 @@ First, download the package: `pip3 install apache-iotdb>=2.0` Note: Do not use a newer client to connect to an older server, as this may cause connection failures or unexpected errors. -You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (you need to add `import iotdb` in the head of the file) @@ -194,9 +194,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -217,9 +217,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -547,11 +547,10 @@ username_ = "root" password_ = "root" session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT * FROM root.*") - -# Transform to Pandas Dataset -df = result.todf() - +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() + session.close() # Now you can work with the dataframe @@ -571,8 +570,8 @@ class MyTestCase(unittest.TestCase): 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) + with session.execute_query_statement("SHOW TIMESERIES") result: + print(result) session.close() ``` diff --git a/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md b/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md index 9a0ba39cd..c5d95ffc0 100644 --- a/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md +++ b/src/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md @@ -33,9 +33,9 @@ First, download the package: `pip3 install apache-iotdb>=2.0` Note: Do not use a newer client to connect to an older server, as this may cause connection failures or unexpected errors. -You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (you need to add `import iotdb` in the head of the file) @@ -194,9 +194,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -217,9 +217,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -547,11 +547,10 @@ username_ = "root" password_ = "TimechoDB@2021" //Before V2.0.6.x the default password is root session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT * FROM root.*") - -# Transform to Pandas Dataset -df = result.todf() - +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() + session.close() # Now you can work with the dataframe @@ -571,8 +570,8 @@ class MyTestCase(unittest.TestCase): with IoTDBContainer() as c: session = Session("localhost", c.get_exposed_port(6667), "root", "TimechoDB@2021") //Before V2.0.6.x the default password is root session.open(False) - result = session.execute_query_statement("SHOW TIMESERIES") - print(result) + with session.execute_query_statement("SHOW TIMESERIES") result: + print(result) session.close() ``` diff --git a/src/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md b/src/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md index 568102fb5..1b1e629d4 100644 --- a/src/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md +++ b/src/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md @@ -338,11 +338,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -361,9 +362,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -374,9 +376,9 @@ if __name__ == "__main__": ## 4. Sample Code -**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py). +**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py). -**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py). +**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_pool_example.py). Here is an excerpt of the sample code: @@ -427,9 +429,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -484,14 +486,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -500,9 +502,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md b/src/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md index 7149b6dc8..b9193eaaa 100644 --- a/src/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md +++ b/src/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md @@ -338,11 +338,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -361,9 +362,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -374,9 +376,9 @@ if __name__ == "__main__": ## 4. Sample Code -**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py). +**Session** Example: You can find the full example code at [Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py). -**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py). +**Session Pool** Example: You can find the full example code at [SessionPool Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_pool_example.py). Here is an excerpt of the sample code: @@ -427,9 +429,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -484,14 +486,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -500,9 +502,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/UserGuide/latest/API/Programming-Python-Native-API_apache.md b/src/UserGuide/latest/API/Programming-Python-Native-API_apache.md index df5123d32..02b831655 100644 --- a/src/UserGuide/latest/API/Programming-Python-Native-API_apache.md +++ b/src/UserGuide/latest/API/Programming-Python-Native-API_apache.md @@ -33,9 +33,9 @@ First, download the package: `pip3 install apache-iotdb>=2.0` Note: Do not use a newer client to connect to an older server, as this may cause connection failures or unexpected errors. -You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (you need to add `import iotdb` in the head of the file) @@ -194,9 +194,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -217,9 +217,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -547,11 +547,10 @@ username_ = "root" password_ = "root" session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT * FROM root.*") - -# Transform to Pandas Dataset -df = result.todf() - +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() + session.close() # Now you can work with the dataframe @@ -571,8 +570,8 @@ class MyTestCase(unittest.TestCase): 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) + with session.execute_query_statement("SHOW TIMESERIES") result: + print(result) session.close() ``` diff --git a/src/UserGuide/latest/API/Programming-Python-Native-API_timecho.md b/src/UserGuide/latest/API/Programming-Python-Native-API_timecho.md index 9a0ba39cd..c5d95ffc0 100644 --- a/src/UserGuide/latest/API/Programming-Python-Native-API_timecho.md +++ b/src/UserGuide/latest/API/Programming-Python-Native-API_timecho.md @@ -33,9 +33,9 @@ First, download the package: `pip3 install apache-iotdb>=2.0` Note: Do not use a newer client to connect to an older server, as this may cause connection failures or unexpected errors. -You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +You can get an example of using the package to read and write data at here:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +An example of aligned timeseries: [Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (you need to add `import iotdb` in the head of the file) @@ -194,9 +194,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -217,9 +217,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -547,11 +547,10 @@ username_ = "root" password_ = "TimechoDB@2021" //Before V2.0.6.x the default password is root session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT * FROM root.*") - -# Transform to Pandas Dataset -df = result.todf() - +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() + session.close() # Now you can work with the dataframe @@ -571,8 +570,8 @@ class MyTestCase(unittest.TestCase): with IoTDBContainer() as c: session = Session("localhost", c.get_exposed_port(6667), "root", "TimechoDB@2021") //Before V2.0.6.x the default password is root session.open(False) - result = session.execute_query_statement("SHOW TIMESERIES") - print(result) + with session.execute_query_statement("SHOW TIMESERIES") result: + print(result) session.close() ``` diff --git a/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md b/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md index eaf45591b..8792f3ca3 100644 --- a/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md +++ b/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md @@ -347,11 +347,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -370,9 +371,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -383,9 +385,9 @@ if __name__ == "__main__": ## 4. 示例代码 -Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py) +Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py) -SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py) +SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/master/otdb-client/client-py/table_model_session_pool_example.py) ```Python # Licensed to the Apache Software Foundation (ASF) under one @@ -434,9 +436,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -491,14 +493,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -507,9 +509,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md b/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md index af1119b53..43bdfc3f7 100644 --- a/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md +++ b/src/zh/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md @@ -347,11 +347,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -370,9 +371,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -383,9 +385,9 @@ if __name__ == "__main__": ## 4. 示例代码 -Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py) +Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py) -SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py) +SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_pool_example.py) ```Python # Licensed to the Apache Software Foundation (ASF) under one @@ -434,9 +436,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -491,14 +493,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -507,9 +509,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md b/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md index 42bab4e64..9219ce9c0 100644 --- a/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md +++ b/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md @@ -31,9 +31,9 @@ 注意:请勿使用高版本客户端连接低版本服务。 -您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (您需要在文件的头部添加`import iotdb`) @@ -198,9 +198,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -221,9 +221,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -546,13 +546,11 @@ username_ = "root" password_ = "root" session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT ** FROM root") - -# Transform to Pandas Dataset -df = result.todf() - +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() + session.close() - # Now you can work with the dataframe df = ... ``` @@ -570,8 +568,8 @@ class MyTestCase(unittest.TestCase): 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) + with session.execute_query_statement("SHOW TIMESERIES") result: + print(result) session.close() ``` diff --git a/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md b/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md index dffad1211..d210cd396 100644 --- a/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md +++ b/src/zh/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md @@ -31,9 +31,9 @@ 注意:请勿使用高版本客户端连接低版本服务。 -您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (您需要在文件的头部添加`import iotdb`) @@ -197,9 +197,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -220,9 +220,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -545,10 +545,9 @@ username_ = "root" password_ = "TimechoDB@2021" //V2.0.6.x 之前密码默认值为root session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT ** FROM root") - -# Transform to Pandas Dataset -df = result.todf() +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() session.close() @@ -569,8 +568,8 @@ class MyTestCase(unittest.TestCase): with IoTDBContainer() as c: session = Session("localhost", c.get_exposed_port(6667), "root", "TimechoDB@2021") //V2.0.6.x 之前密码默认值为root session.open(False) - result = session.execute_query_statement("SHOW TIMESERIES") - print(result) + with session.execute_query_statement("SHOW TIMESERIES") as result: + print(result) session.close() ``` diff --git a/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md b/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md index eaf45591b..8792f3ca3 100644 --- a/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md +++ b/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md @@ -347,11 +347,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -370,9 +371,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -383,9 +385,9 @@ if __name__ == "__main__": ## 4. 示例代码 -Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py) +Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py) -SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py) +SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/master/otdb-client/client-py/table_model_session_pool_example.py) ```Python # Licensed to the Apache Software Foundation (ASF) under one @@ -434,9 +436,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -491,14 +493,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -507,9 +509,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md b/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md index af1119b53..43bdfc3f7 100644 --- a/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md +++ b/src/zh/UserGuide/latest-Table/API/Programming-Python-Native-API_timecho.md @@ -347,11 +347,12 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) + session.close() - return df def get_data2(): @@ -370,9 +371,10 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("SHOW DATABASES") as session_data_set: + print(session_data_set.get_column_names()) + while session_data_set.has_next(): + print(session_data_set.next()) session_pool.put_back(session) session_pool.close() @@ -383,9 +385,9 @@ if __name__ == "__main__": ## 4. 示例代码 -Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_example.py) +Session示例代码:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_example.py) -SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/table_model_session_pool_example.py) +SessionPool示例代码:[SessionPool Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/table_model_session_pool_example.py) ```Python # Licensed to the Apache Software Foundation (ASF) under one @@ -434,9 +436,9 @@ def prepare_data(): print("now the tables are:") # show result - res = session.execute_query_statement("SHOW TABLES") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("SHOW TABLES") as res: + while res.has_next(): + print(res.next()) session.close() @@ -491,14 +493,14 @@ def query_data(): session = session_pool.get_session() print("get data from table0") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) print("get data from table1") - res = session.execute_query_statement("select * from table0") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("select * from table0") as res: + while res.has_next(): + print(res.next()) session.close() @@ -507,9 +509,9 @@ def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_query_statement("show databases") - while res.has_next(): - print(res.next()) + with session.execute_query_statement("show databases") as res: + while res.has_next(): + print(res.next()) session.close() diff --git a/src/zh/UserGuide/latest/API/Programming-Python-Native-API_apache.md b/src/zh/UserGuide/latest/API/Programming-Python-Native-API_apache.md index 42bab4e64..9219ce9c0 100644 --- a/src/zh/UserGuide/latest/API/Programming-Python-Native-API_apache.md +++ b/src/zh/UserGuide/latest/API/Programming-Python-Native-API_apache.md @@ -31,9 +31,9 @@ 注意:请勿使用高版本客户端连接低版本服务。 -您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (您需要在文件的头部添加`import iotdb`) @@ -198,9 +198,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -221,9 +221,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -546,13 +546,11 @@ username_ = "root" password_ = "root" session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT ** FROM root") - -# Transform to Pandas Dataset -df = result.todf() - +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() + session.close() - # Now you can work with the dataframe df = ... ``` @@ -570,8 +568,8 @@ class MyTestCase(unittest.TestCase): 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) + with session.execute_query_statement("SHOW TIMESERIES") result: + print(result) session.close() ``` diff --git a/src/zh/UserGuide/latest/API/Programming-Python-Native-API_timecho.md b/src/zh/UserGuide/latest/API/Programming-Python-Native-API_timecho.md index dffad1211..d210cd396 100644 --- a/src/zh/UserGuide/latest/API/Programming-Python-Native-API_timecho.md +++ b/src/zh/UserGuide/latest/API/Programming-Python-Native-API_timecho.md @@ -31,9 +31,9 @@ 注意:请勿使用高版本客户端连接低版本服务。 -您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_example.py) +您可以从这里得到一个使用该包进行数据读写的例子:[Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_example.py) -关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/rc/2.0.1/iotdb-client/client-py/session_aligned_timeseries_example.py) +关于对齐时间序列读写的例子:[Aligned Timeseries Session Example](https://github.com/apache/iotdb/blob/master/iotdb-client/client-py/session_aligned_timeseries_example.py) (您需要在文件的头部添加`import iotdb`) @@ -197,9 +197,9 @@ def get_data(): ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs ) session.open(False) - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session.close() return df @@ -220,9 +220,9 @@ def get_data2(): wait_timeout_in_ms = 3000 session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) session = session_pool.get_session() - result = session.execute_query_statement("select * from root.eg.etth") - df = result.todf() - df.rename(columns={"Time": "date"}, inplace=True) + with session.execute_query_statement("select * from root.eg.etth") as result: + df = result.todf() + df.rename(columns={"Time": "date"}, inplace=True) session_pool.put_back(session) session_pool.close() @@ -545,10 +545,9 @@ username_ = "root" password_ = "TimechoDB@2021" //V2.0.6.x 之前密码默认值为root session = Session(ip, port_, username_, password_) session.open(False) -result = session.execute_query_statement("SELECT ** FROM root") - -# Transform to Pandas Dataset -df = result.todf() +with session.execute_query_statement("SELECT ** FROM root") as result: + # Transform to Pandas Dataset + df = result.todf() session.close() @@ -569,8 +568,8 @@ class MyTestCase(unittest.TestCase): with IoTDBContainer() as c: session = Session("localhost", c.get_exposed_port(6667), "root", "TimechoDB@2021") //V2.0.6.x 之前密码默认值为root session.open(False) - result = session.execute_query_statement("SHOW TIMESERIES") - print(result) + with session.execute_query_statement("SHOW TIMESERIES") as result: + print(result) session.close() ```