提交 b11703c6 编写于 作者: J Jiang Tian 提交者: Jialin Qiao

update python example to adapt to new QueryDataSet (#509)

* update python example to adapt to new QueryDataSet
上级 b845dd7a
......@@ -59,6 +59,54 @@ Compressor = {
}
def convertQueryDataSet(queryDataSet, dataTypeList):
bytes = queryDataSet.values
row_count = queryDataSet.rowCount
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)
for i in range(row_count):
records.append([times[i]])
for i in range(len(dataTypeList)):
type = dataTypeList[i]
for j in range(row_count):
is_null = value_bytes[0]
value_bytes = value_bytes[1:]
if is_null == 1:
records[j].append('null')
else:
if type == 'BOOLEAN':
value = value_bytes[0]
value_bytes = value_bytes[1:]
records[j].append(struct.unpack('>?', value)[0])
elif type == 'INT32':
value = value_bytes[:4]
value_bytes = value_bytes[4:]
records[j].append(struct.unpack('>i', value)[0])
elif type == 'INT64':
value = value_bytes[:8]
value_bytes = value_bytes[8:]
records[j].append(struct.unpack('>q', value)[0])
elif type == 'FLOAT':
value = value_bytes[:4]
value_bytes = value_bytes[4:]
records[j].append(struct.unpack('>f', value)[0])
elif type == 'DOUBLE':
value = value_bytes[:8]
value_bytes = value_bytes[8:]
records[j].append(struct.unpack('>d', value)[0])
elif type == 'TEXT':
size = value_bytes[:4]
value_bytes = value_bytes[4:]
size = struct.unpack('>i', size)[0]
records[j].append(value_bytes[:size])
value_bytes = value_bytes[size:]
return records
if __name__ == '__main__':
ip = "localhost"
port = "6667"
......@@ -98,32 +146,68 @@ if __name__ == '__main__':
print(status.statusType)
# create timeseries
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s1", TSDataType['INT64'], TSEncoding['PLAIN'],
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s1",
TSDataType['INT64'],
TSEncoding['PLAIN'],
Compressor['UNCOMPRESSED']))
print(status.statusType)
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s2",
TSDataType['INT32'],
TSEncoding['PLAIN'],
Compressor['UNCOMPRESSED']))
print(status.statusType)
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s2", TSDataType['INT64'], TSEncoding['PLAIN'],
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s3",
TSDataType['DOUBLE'],
TSEncoding['PLAIN'],
Compressor['UNCOMPRESSED']))
print(status.statusType)
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s3", TSDataType['INT64'], TSEncoding['PLAIN'],
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s4",
TSDataType['FLOAT'],
TSEncoding['PLAIN'],
Compressor['UNCOMPRESSED']))
print(status.statusType)
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s5",
TSDataType['BOOLEAN'],
TSEncoding['PLAIN'],
Compressor['UNCOMPRESSED']))
print(status.statusType)
status = client.createTimeseries(TSCreateTimeseriesReq("root.group1.s6",
TSDataType['TEXT'],
TSEncoding['PLAIN'],
Compressor['UNCOMPRESSED']))
print(status.statusType)
deviceId = "root.group1"
measurements = ["s1", "s2", "s3", "s4", "s5", "s6"]
# insert a single row
status = client.insertRow(TSInsertionReq("root.group1", ["s1", "s2", "s3"], ["1", "11", "111"], 1, 1))
values = ["1", "11", "1.1", "11.1", "TRUE", "\'text0\'"]
timestamp = 1
status = client.insertRow(TSInsertionReq(deviceId, measurements,
values, timestamp, stmtId))
print(status.statusType)
# insert multiple rows, this interface is more efficient
values = bytearray()
times = bytearray()
deviceId = "root.group1"
measurements = ["s1", "s2", "s3"]
dataSize = 3
dataTypes = [TSDataType['INT64'], TSDataType['INT64'], TSDataType['INT64']]
# the first 3 belong to 's1', the mid 3 belong to 's2', the last 3 belong to 's3'
values.extend(struct.pack('>qqqqqqqqq', 2, 3, 4, 22, 33, 44, 222, 333, 444))
times.extend(struct.pack('>qqq', 2, 3, 4))
resp = client.insertBatch(TSBatchInsertionReq(deviceId, measurements, values, times,
dataTypes, dataSize))
rowCnt = 3
dataTypes = [TSDataType['INT64'], TSDataType['INT32'], TSDataType['DOUBLE'],
TSDataType['FLOAT'], TSDataType['BOOLEAN'], TSDataType['TEXT']]
# the first 3 belong to 's1', the second 3 belong to 's2'... the last 3
# belong to 's6'
# to transfer a string, you must first send its length and then its bytes
# (like the last 3 'i7s'). Text values should start and end with ' or ".
# IoTDB use big endian in rpc
value_pack_str = '>3q3i3d3f3bi7si7si7s'
time_pack_str = '>3q'
values.extend(struct.pack(value_pack_str, 2, 3, 4, 22, 33, 44, 2.2, 3.3,
4.4, 22.2, 33.3, 44.4, True, True, False,
len('\'text1\''), '\'text1\'', len('\'text2\''),
'\'text2\'', len('\'text3\''), '\'text3\''))
times.extend(struct.pack(time_pack_str, 2, 3, 4))
resp = client.insertBatch(TSBatchInsertionReq(deviceId, measurements, values,
times, dataTypes, rowCnt))
status = resp.status
print(status.statusType)
......@@ -139,13 +223,16 @@ if __name__ == '__main__':
queryId = 1
resp = client.executeQueryStatement(TSExecuteStatementReq(handle, stmt))
# headers
dataTypeList = resp.dataTypeList
print(resp.columns)
print(dataTypeList)
stmtHandle = resp.operationHandle
status = resp.status
print(status.statusType)
while True:
rst = client.fetchResults(TSFetchResultsReq(stmt, fetchSize, queryId)).queryDataSet
records = rst.records
records = convertQueryDataSet(rst, dataTypeList)
if len(records) == 0:
break
for record in records:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册