python中查询influxdb数据时如何获取结构化数据?
How do I get structured data when querying data from influxdb in python?
我一直在将数据作为字段输入 influxdb。当我查询数据时,我不会像在 SQL 中查询时那样将它们作为 table 获取。有没有什么办法可以像我插入时那样获取字典列表数据到数据库。
用于插入,
for i in range(len(df)-1):
point=influxdb_client.Point(switch_id).field(list(df[i].keys())[0],list(df[i].values())[0]).field(list(df[i].keys())[1],list(df[i].values())[1]).field(list(df[i].keys())[2],list(df[i].values())[2]).field(list(df[i].keys())[3],list(df[i].values())[3]).field(list(df[i].keys())[4],list(df[i].values())[4]).field(list(df[i].keys())[5],list(df[i].values())[5]).field(list(df[i].keys())[6],list(df[i].values())[6]).field(list(df[i].keys())[7],list(df[i].values())[7]).field(list(df[i].keys())[8],list(df[i].values())[8])
points.append(point)
write_status=write_api.write(bucket, org, points)
当我查询数据时,我按照文档发送查询:
query_api = client.query_api()
query = 'from(bucket: "SDN-IDS")\
|> range(start: -24h)\
|> filter(fn: (r) => r["_measurement"] == "S1")\
|> filter(fn: (r) => r["_field"] == "actions" or r["_field"] == "cookie" or r["_field"] == "dl_dst" or r["_field"] == "duration" or r["_field"] == "in_port" or r["_field"] == "n_bytes" or r["_field"] == "n_packets" or r["_field"] == "priority" or r["_field"] == "table")'
try:
result = query_api.query(org=org,query=query)
results = []
for table in result:
for record in table.records:
results.append((record.get_field(), record.get_value()))
print(results)
client.close()
except Exception as e:
print(e)
结果输出如下:
[('actions', 'output:"s1-eth4"'), ('actions', 'output:"s1-eth4"'), ('cookie', '0x0'), ('cookie', '0x0'), ('dl_dst', '4a:b8:04:37:ed:48'), ('dl_dst', '4a:b8:04:37:ed:48'), ('duration', '36.505s'), ('duration', '36.505s'), ('in_port', '"s1-eth3"'), ('in_port', '"s1-eth3"'), ('n_bytes', '378'), ('n_bytes', '378'), ('n_packets', '5'), ('n_packets', '5'), ('priority', '1'), ('priority', '1'), ('table', '0'), ('table', '0')]
我需要以这样一种方式构建这些数据,这样我还可以看到时间戳以及作为行而不是列值对列表传入的数据。可能吗?
时间戳可以通过record.get_time()
来实现。
你可以随心所欲地塑造它,例如作为 JSON:
json_data=[]
...
for record in table.records:
json_data.append({
"measurement": record.get_measurement(),
"fields":{
"tag": record.get_field(),
"value": record.get_value()
},
"time": record.get_time() # can be shaped as you like, e.g. ISO with .replace(tzinfo=None).isoformat()+'Z'
})
或
results.append((record.get_time(), record.get_field(), record.get_value()))
或者,如果您已经在使用 Pandas,请将其放入数据框中。
我一直在将数据作为字段输入 influxdb。当我查询数据时,我不会像在 SQL 中查询时那样将它们作为 table 获取。有没有什么办法可以像我插入时那样获取字典列表数据到数据库。
用于插入,
for i in range(len(df)-1):
point=influxdb_client.Point(switch_id).field(list(df[i].keys())[0],list(df[i].values())[0]).field(list(df[i].keys())[1],list(df[i].values())[1]).field(list(df[i].keys())[2],list(df[i].values())[2]).field(list(df[i].keys())[3],list(df[i].values())[3]).field(list(df[i].keys())[4],list(df[i].values())[4]).field(list(df[i].keys())[5],list(df[i].values())[5]).field(list(df[i].keys())[6],list(df[i].values())[6]).field(list(df[i].keys())[7],list(df[i].values())[7]).field(list(df[i].keys())[8],list(df[i].values())[8])
points.append(point)
write_status=write_api.write(bucket, org, points)
当我查询数据时,我按照文档发送查询:
query_api = client.query_api()
query = 'from(bucket: "SDN-IDS")\
|> range(start: -24h)\
|> filter(fn: (r) => r["_measurement"] == "S1")\
|> filter(fn: (r) => r["_field"] == "actions" or r["_field"] == "cookie" or r["_field"] == "dl_dst" or r["_field"] == "duration" or r["_field"] == "in_port" or r["_field"] == "n_bytes" or r["_field"] == "n_packets" or r["_field"] == "priority" or r["_field"] == "table")'
try:
result = query_api.query(org=org,query=query)
results = []
for table in result:
for record in table.records:
results.append((record.get_field(), record.get_value()))
print(results)
client.close()
except Exception as e:
print(e)
结果输出如下:
[('actions', 'output:"s1-eth4"'), ('actions', 'output:"s1-eth4"'), ('cookie', '0x0'), ('cookie', '0x0'), ('dl_dst', '4a:b8:04:37:ed:48'), ('dl_dst', '4a:b8:04:37:ed:48'), ('duration', '36.505s'), ('duration', '36.505s'), ('in_port', '"s1-eth3"'), ('in_port', '"s1-eth3"'), ('n_bytes', '378'), ('n_bytes', '378'), ('n_packets', '5'), ('n_packets', '5'), ('priority', '1'), ('priority', '1'), ('table', '0'), ('table', '0')]
我需要以这样一种方式构建这些数据,这样我还可以看到时间戳以及作为行而不是列值对列表传入的数据。可能吗?
时间戳可以通过record.get_time()
来实现。
你可以随心所欲地塑造它,例如作为 JSON:
json_data=[]
...
for record in table.records:
json_data.append({
"measurement": record.get_measurement(),
"fields":{
"tag": record.get_field(),
"value": record.get_value()
},
"time": record.get_time() # can be shaped as you like, e.g. ISO with .replace(tzinfo=None).isoformat()+'Z'
})
或
results.append((record.get_time(), record.get_field(), record.get_value()))
或者,如果您已经在使用 Pandas,请将其放入数据框中。