You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ji...@apache.org on 2019/11/16 03:24:00 UTC

[incubator-iotdb] branch master updated: Correct python example (#561)

This is an automated email from the ASF dual-hosted git repository.

jiangtian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new fe0b627  Correct python example (#561)
fe0b627 is described below

commit fe0b6275aa90329d665d04f60b82589804826c91
Author: Jackie Tien <Ja...@foxmail.com>
AuthorDate: Sat Nov 16 11:23:50 2019 +0800

    Correct python example (#561)
    
    * correct python example
---
 client-py/readme.md             |  2 +-
 client-py/src/client_example.py | 65 +++++++++++++++++++++--------------------
 2 files changed, 34 insertions(+), 33 deletions(-)

diff --git a/client-py/readme.md b/client-py/readme.md
index 9d849db..492b25c 100644
--- a/client-py/readme.md
+++ b/client-py/readme.md
@@ -20,7 +20,7 @@
 -->
 
 # Python connection
-## introduction
+## Introduction
 This is an example of how to connect to IoTDB with python, using the thrift rpc interfaces. Things 
 are almost the same on Windows or Linux, but pay attention to the difference like path separator.
 
diff --git a/client-py/src/client_example.py b/client-py/src/client_example.py
index 2d4188b..6926e17 100644
--- a/client-py/src/client_example.py
+++ b/client-py/src/client_example.py
@@ -17,6 +17,7 @@
 #
 
 import sys, struct
+
 sys.path.append("../target")
 
 from thrift.protocol import TBinaryProtocol
@@ -26,44 +27,45 @@ from rpc.TSIService import Client, TSCreateTimeseriesReq, TSInsertionReq, \
     TSBatchInsertionReq, TSExecuteStatementReq, \
     TS_SessionHandle, TSHandleIdentifier, TSOpenSessionReq, TSQueryDataSet, \
     TSFetchResultsReq, TSCloseOperationReq, \
-    TSCloseSessionReq, TSProtocolVersion
+    TSCloseSessionReq
+from rpc.ttypes import TSProtocolVersion
 
 TSDataType = {
-    'BOOLEAN' : 0,
-    'INT32' : 1,
-    'INT64' : 2,
-    'FLOAT' : 3,
-    'DOUBLE' : 4,
-    'TEXT' : 5
+    'BOOLEAN': 0,
+    'INT32': 1,
+    'INT64': 2,
+    'FLOAT': 3,
+    'DOUBLE': 4,
+    'TEXT': 5
 }
 
 TSEncoding = {
-    'PLAIN' : 0,
-    'PLAIN_DICTIONARY' : 1,
-    'RLE' : 2,
-    'DIFF' : 3,
-    'TS_2DIFF' : 4,
-    'BITMAP' : 5,
-    'GORILLA' : 6,
-    'REGULAR' : 7
+    'PLAIN': 0,
+    'PLAIN_DICTIONARY': 1,
+    'RLE': 2,
+    'DIFF': 3,
+    'TS_2DIFF': 4,
+    'BITMAP': 5,
+    'GORILLA': 6,
+    'REGULAR': 7
 }
 
 Compressor = {
-    'UNCOMPRESSED' : 0,
-    'SNAPPY' : 1,
-    'GZIP' : 2,
-    'LZO' : 3,
-    'SDT' : 4,
-    'PAA' : 5,
-    'PLA' : 6
+    'UNCOMPRESSED': 0,
+    'SNAPPY': 1,
+    'GZIP': 2,
+    'LZO': 3,
+    'SDT': 4,
+    'PAA': 5,
+    'PLA': 6
 }
 
 
 def convertQueryDataSet(queryDataSet, dataTypeList):
     bytes = queryDataSet.values
     row_count = queryDataSet.rowCount
-    time_bytes = bytes[:8*row_count]
-    value_bytes = bytes[8*row_count:]
+    time_bytes = bytes[:8 * row_count]
+    value_bytes = bytes[8 * row_count:]
     time_unpack_str = '>' + str(row_count) + 'q'
     records = []
     times = struct.unpack(time_unpack_str, time_bytes)
@@ -183,9 +185,9 @@ if __name__ == '__main__':
     # insert a single row
     values = ["1", "11", "1.1", "11.1", "TRUE", "\'text0\'"]
     timestamp = 1
-    status = client.insertRow(TSInsertionReq(deviceId, measurements,
-                                             values, timestamp, stmtId))
-    print(status.statusType)
+    status = client.insert(TSInsertionReq(deviceId, measurements,
+                                          values, timestamp, stmtId))
+    print(status.status)
 
     # insert multiple rows, this interface is more efficient
     values = bytearray()
@@ -217,7 +219,7 @@ if __name__ == '__main__':
     print(status.statusType)
 
     # execute deletion (or other statements)
-    resp = client.executeStatement(TSExecuteStatementReq(handle, "DELETE FROM root.group1 where time < 2"))
+    resp = client.executeStatement(TSExecuteStatementReq(handle, "DELETE FROM root.group1 where time < 2", stmtId))
     status = resp.status
     print(status.statusType)
 
@@ -225,8 +227,7 @@ if __name__ == '__main__':
     stmt = "SELECT * FROM root.group1"
     fetchSize = 2
     # this is also for resource control, make sure different queries will not use the same id at the same time
-    queryId = 1
-    resp = client.executeQueryStatement(TSExecuteStatementReq(handle, stmt))
+    resp = client.executeQueryStatement(TSExecuteStatementReq(handle, stmt, stmtId))
     # headers
     dataTypeList = resp.dataTypeList
     print(resp.columns)
@@ -235,6 +236,8 @@ if __name__ == '__main__':
     stmtHandle = resp.operationHandle
     status = resp.status
     print(status.statusType)
+
+    queryId = resp.operationHandle.operationId.queryId
     while True:
         rst = client.fetchResults(TSFetchResultsReq(stmt, fetchSize, queryId)).queryDataSet
         records = convertQueryDataSet(rst, dataTypeList)
@@ -248,5 +251,3 @@ if __name__ == '__main__':
 
     # and do not forget to close the session before exiting
     client.closeSession(TSCloseSessionReq(handle))
-
-