Skip to content

Commit a38532c

Browse files
committed
update python example code
1 parent 0da8ed8 commit a38532c

16 files changed

Lines changed: 294 additions & 288 deletions

src/UserGuide/Master/Table/API/Programming-Python-Native-API_apache.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,12 @@ def get_data():
338338
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
339339
)
340340
session.open(False)
341-
result = session.execute_query_statement("select * from root.eg.etth")
342-
df = result.todf()
343-
df.rename(columns={"Time": "date"}, inplace=True)
341+
with session.execute_query_statement("SHOW DATABASES") as session_data_set:
342+
print(session_data_set.get_column_names())
343+
while session_data_set.has_next():
344+
print(session_data_set.next())
345+
344346
session.close()
345-
return df
346347

347348

348349
def get_data2():
@@ -361,9 +362,10 @@ def get_data2():
361362
wait_timeout_in_ms = 3000
362363
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
363364
session = session_pool.get_session()
364-
result = session.execute_query_statement("select * from root.eg.etth")
365-
df = result.todf()
366-
df.rename(columns={"Time": "date"}, inplace=True)
365+
with session.execute_query_statement("SHOW DATABASES") as session_data_set:
366+
print(session_data_set.get_column_names())
367+
while session_data_set.has_next():
368+
print(session_data_set.next())
367369
session_pool.put_back(session)
368370
session_pool.close()
369371

@@ -374,9 +376,9 @@ if __name__ == "__main__":
374376

375377
## 4. Sample Code
376378

377-
**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).
379+
**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).
378380

379-
**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).
381+
**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).
380382

381383
Here is an excerpt of the sample code:
382384

@@ -427,9 +429,9 @@ def prepare_data():
427429

428430
print("now the tables are:")
429431
# show result
430-
res = session.execute_query_statement("SHOW TABLES")
431-
while res.has_next():
432-
print(res.next())
432+
with session.execute_query_statement("SHOW TABLES") as res:
433+
while res.has_next():
434+
print(res.next())
433435

434436
session.close()
435437

@@ -484,14 +486,14 @@ def query_data():
484486
session = session_pool.get_session()
485487

486488
print("get data from table0")
487-
res = session.execute_query_statement("select * from table0")
488-
while res.has_next():
489-
print(res.next())
489+
with session.execute_query_statement("select * from table0") as res:
490+
while res.has_next():
491+
print(res.next())
490492

491493
print("get data from table1")
492-
res = session.execute_query_statement("select * from table0")
493-
while res.has_next():
494-
print(res.next())
494+
with session.execute_query_statement("select * from table0") as res:
495+
while res.has_next():
496+
print(res.next())
495497

496498
session.close()
497499

@@ -500,9 +502,9 @@ def delete_data():
500502
session = session_pool.get_session()
501503
session.execute_non_query_statement("drop database db1")
502504
print("data has been deleted. now the databases are:")
503-
res = session.execute_query_statement("show databases")
504-
while res.has_next():
505-
print(res.next())
505+
with session.execute_query_statement("show databases") as res:
506+
while res.has_next():
507+
print(res.next())
506508
session.close()
507509

508510

src/UserGuide/Master/Table/API/Programming-Python-Native-API_timecho.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,12 @@ def get_data():
338338
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
339339
)
340340
session.open(False)
341-
result = session.execute_query_statement("select * from root.eg.etth")
342-
df = result.todf()
343-
df.rename(columns={"Time": "date"}, inplace=True)
341+
with session.execute_query_statement("SHOW DATABASES") as session_data_set:
342+
print(session_data_set.get_column_names())
343+
while session_data_set.has_next():
344+
print(session_data_set.next())
345+
344346
session.close()
345-
return df
346347

347348

348349
def get_data2():
@@ -361,9 +362,10 @@ def get_data2():
361362
wait_timeout_in_ms = 3000
362363
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
363364
session = session_pool.get_session()
364-
result = session.execute_query_statement("select * from root.eg.etth")
365-
df = result.todf()
366-
df.rename(columns={"Time": "date"}, inplace=True)
365+
with session.execute_query_statement("SHOW DATABASES") as session_data_set:
366+
print(session_data_set.get_column_names())
367+
while session_data_set.has_next():
368+
print(session_data_set.next())
367369
session_pool.put_back(session)
368370
session_pool.close()
369371

@@ -374,9 +376,9 @@ if __name__ == "__main__":
374376

375377
## 4. Sample Code
376378

377-
**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).
379+
**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).
378380

379-
**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).
381+
**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).
380382

381383
Here is an excerpt of the sample code:
382384

@@ -427,9 +429,9 @@ def prepare_data():
427429

428430
print("now the tables are:")
429431
# show result
430-
res = session.execute_query_statement("SHOW TABLES")
431-
while res.has_next():
432-
print(res.next())
432+
with session.execute_query_statement("SHOW TABLES") as res:
433+
while res.has_next():
434+
print(res.next())
433435

434436
session.close()
435437

@@ -484,14 +486,14 @@ def query_data():
484486
session = session_pool.get_session()
485487

486488
print("get data from table0")
487-
res = session.execute_query_statement("select * from table0")
488-
while res.has_next():
489-
print(res.next())
489+
with session.execute_query_statement("select * from table0") as res:
490+
while res.has_next():
491+
print(res.next())
490492

491493
print("get data from table1")
492-
res = session.execute_query_statement("select * from table0")
493-
while res.has_next():
494-
print(res.next())
494+
with session.execute_query_statement("select * from table0") as res:
495+
while res.has_next():
496+
print(res.next())
495497

496498
session.close()
497499

@@ -500,9 +502,9 @@ def delete_data():
500502
session = session_pool.get_session()
501503
session.execute_non_query_statement("drop database db1")
502504
print("data has been deleted. now the databases are:")
503-
res = session.execute_query_statement("show databases")
504-
while res.has_next():
505-
print(res.next())
505+
with session.execute_query_statement("show databases") as res:
506+
while res.has_next():
507+
print(res.next())
506508
session.close()
507509

508510

src/UserGuide/Master/Tree/API/Programming-Python-Native-API_apache.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ First, download the package: `pip3 install apache-iotdb>=2.0`
3333

3434
Note: Do not use a newer client to connect to an older server, as this may cause connection failures or unexpected errors.
3535

36-
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)
36+
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)
3737

38-
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)
38+
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)
3939

4040
(you need to add `import iotdb` in the head of the file)
4141

@@ -194,9 +194,9 @@ def get_data():
194194
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
195195
)
196196
session.open(False)
197-
result = session.execute_query_statement("select * from root.eg.etth")
198-
df = result.todf()
199-
df.rename(columns={"Time": "date"}, inplace=True)
197+
with session.execute_query_statement("select * from root.eg.etth") as result:
198+
df = result.todf()
199+
df.rename(columns={"Time": "date"}, inplace=True)
200200
session.close()
201201
return df
202202

@@ -217,9 +217,9 @@ def get_data2():
217217
wait_timeout_in_ms = 3000
218218
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
219219
session = session_pool.get_session()
220-
result = session.execute_query_statement("select * from root.eg.etth")
221-
df = result.todf()
222-
df.rename(columns={"Time": "date"}, inplace=True)
220+
with session.execute_query_statement("select * from root.eg.etth") as result:
221+
df = result.todf()
222+
df.rename(columns={"Time": "date"}, inplace=True)
223223
session_pool.put_back(session)
224224
session_pool.close()
225225

@@ -547,11 +547,10 @@ username_ = "root"
547547
password_ = "root"
548548
session = Session(ip, port_, username_, password_)
549549
session.open(False)
550-
result = session.execute_query_statement("SELECT * FROM root.*")
551-
552-
# Transform to Pandas Dataset
553-
df = result.todf()
554-
550+
with session.execute_query_statement("SELECT ** FROM root") as result:
551+
# Transform to Pandas Dataset
552+
df = result.todf()
553+
555554
session.close()
556555

557556
# Now you can work with the dataframe
@@ -571,8 +570,8 @@ class MyTestCase(unittest.TestCase):
571570
with IoTDBContainer() as c:
572571
session = Session("localhost", c.get_exposed_port(6667), "root", "root")
573572
session.open(False)
574-
result = session.execute_query_statement("SHOW TIMESERIES")
575-
print(result)
573+
with session.execute_query_statement("SHOW TIMESERIES") result:
574+
print(result)
576575
session.close()
577576
```
578577

src/UserGuide/Master/Tree/API/Programming-Python-Native-API_timecho.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ First, download the package: `pip3 install apache-iotdb>=2.0`
3333

3434
Note: Do not use a newer client to connect to an older server, as this may cause connection failures or unexpected errors.
3535

36-
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)
36+
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)
3737

38-
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)
38+
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)
3939

4040
(you need to add `import iotdb` in the head of the file)
4141

@@ -194,9 +194,9 @@ def get_data():
194194
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
195195
)
196196
session.open(False)
197-
result = session.execute_query_statement("select * from root.eg.etth")
198-
df = result.todf()
199-
df.rename(columns={"Time": "date"}, inplace=True)
197+
with session.execute_query_statement("select * from root.eg.etth") as result:
198+
df = result.todf()
199+
df.rename(columns={"Time": "date"}, inplace=True)
200200
session.close()
201201
return df
202202

@@ -217,9 +217,9 @@ def get_data2():
217217
wait_timeout_in_ms = 3000
218218
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
219219
session = session_pool.get_session()
220-
result = session.execute_query_statement("select * from root.eg.etth")
221-
df = result.todf()
222-
df.rename(columns={"Time": "date"}, inplace=True)
220+
with session.execute_query_statement("select * from root.eg.etth") as result:
221+
df = result.todf()
222+
df.rename(columns={"Time": "date"}, inplace=True)
223223
session_pool.put_back(session)
224224
session_pool.close()
225225

@@ -547,11 +547,10 @@ username_ = "root"
547547
password_ = "TimechoDB@2021" //Before V2.0.6.x the default password is root
548548
session = Session(ip, port_, username_, password_)
549549
session.open(False)
550-
result = session.execute_query_statement("SELECT * FROM root.*")
551-
552-
# Transform to Pandas Dataset
553-
df = result.todf()
554-
550+
with session.execute_query_statement("SELECT ** FROM root") as result:
551+
# Transform to Pandas Dataset
552+
df = result.todf()
553+
555554
session.close()
556555

557556
# Now you can work with the dataframe
@@ -571,8 +570,8 @@ class MyTestCase(unittest.TestCase):
571570
with IoTDBContainer() as c:
572571
session = Session("localhost", c.get_exposed_port(6667), "root", "TimechoDB@2021") //Before V2.0.6.x the default password is root
573572
session.open(False)
574-
result = session.execute_query_statement("SHOW TIMESERIES")
575-
print(result)
573+
with session.execute_query_statement("SHOW TIMESERIES") result:
574+
print(result)
576575
session.close()
577576
```
578577

src/UserGuide/latest-Table/API/Programming-Python-Native-API_apache.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,12 @@ def get_data():
338338
ip, port_, username_, password_, use_ssl=use_ssl, ca_certs=ca_certs
339339
)
340340
session.open(False)
341-
result = session.execute_query_statement("select * from root.eg.etth")
342-
df = result.todf()
343-
df.rename(columns={"Time": "date"}, inplace=True)
341+
with session.execute_query_statement("SHOW DATABASES") as session_data_set:
342+
print(session_data_set.get_column_names())
343+
while session_data_set.has_next():
344+
print(session_data_set.next())
345+
344346
session.close()
345-
return df
346347

347348

348349
def get_data2():
@@ -361,9 +362,10 @@ def get_data2():
361362
wait_timeout_in_ms = 3000
362363
session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms)
363364
session = session_pool.get_session()
364-
result = session.execute_query_statement("select * from root.eg.etth")
365-
df = result.todf()
366-
df.rename(columns={"Time": "date"}, inplace=True)
365+
with session.execute_query_statement("SHOW DATABASES") as session_data_set:
366+
print(session_data_set.get_column_names())
367+
while session_data_set.has_next():
368+
print(session_data_set.next())
367369
session_pool.put_back(session)
368370
session_pool.close()
369371

@@ -374,9 +376,9 @@ if __name__ == "__main__":
374376

375377
## 4. Sample Code
376378

377-
**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).
379+
**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).
378380

379-
**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).
381+
**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).
380382

381383
Here is an excerpt of the sample code:
382384

@@ -427,9 +429,9 @@ def prepare_data():
427429

428430
print("now the tables are:")
429431
# show result
430-
res = session.execute_query_statement("SHOW TABLES")
431-
while res.has_next():
432-
print(res.next())
432+
with session.execute_query_statement("SHOW TABLES") as res:
433+
while res.has_next():
434+
print(res.next())
433435

434436
session.close()
435437

@@ -484,14 +486,14 @@ def query_data():
484486
session = session_pool.get_session()
485487

486488
print("get data from table0")
487-
res = session.execute_query_statement("select * from table0")
488-
while res.has_next():
489-
print(res.next())
489+
with session.execute_query_statement("select * from table0") as res:
490+
while res.has_next():
491+
print(res.next())
490492

491493
print("get data from table1")
492-
res = session.execute_query_statement("select * from table0")
493-
while res.has_next():
494-
print(res.next())
494+
with session.execute_query_statement("select * from table0") as res:
495+
while res.has_next():
496+
print(res.next())
495497

496498
session.close()
497499

@@ -500,9 +502,9 @@ def delete_data():
500502
session = session_pool.get_session()
501503
session.execute_non_query_statement("drop database db1")
502504
print("data has been deleted. now the databases are:")
503-
res = session.execute_query_statement("show databases")
504-
while res.has_next():
505-
print(res.next())
505+
with session.execute_query_statement("show databases") as res:
506+
while res.has_next():
507+
print(res.next())
506508
session.close()
507509

508510

0 commit comments

Comments
 (0)