Python 访问XDB 2.0记录中的多个字段

Python 访问XDB 2.0记录中的多个字段,python,flux,influxdb-2,Python,Flux,Influxdb 2,我是XDB 2.0新手,正在构建一个时间序列数据库,在这里,我每个点存储多个字段(XAUUSD货币的价格值) 虽然我能够像预期的那样存储它;当我获取记录时,似乎无法获取每个记录可访问的所有字段 下面是我如何将一些虚拟数据写入DB的代码片段: from datetime import datetime import time import influxdb_client from influxdb_client.client.write_api import SYNCHRONOUS import

我是XDB 2.0新手,正在构建一个时间序列数据库,在这里,我每个点存储多个字段(XAUUSD货币的价格值)

虽然我能够像预期的那样存储它;当我获取记录时,似乎无法获取每个记录可访问的所有字段

下面是我如何将一些虚拟数据写入DB的代码片段:

from datetime import datetime
import time
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
import random

token = "XXX"
org = "Trader"
bucket = "Master"
url="http://localhost:8086"

client = influxdb_client.InfluxDBClient(
    url=url,
    token=token,
    org=org
)

write_api = client.write_api(write_options=SYNCHRONOUS)
while True:
    p = influxdb_client.Point("D1").tag("currency", "XAUUSD").field("open", random.randint(900,1100)).field("close", random.randint(900,1100)).time(datetime.utcnow(), influxdb_client.WritePrecision.NS)
    write_api.write(bucket=bucket, org=org, record=p)
    time.sleep(1)
我试图将数据读回如下:

query_api = client.query_api()
query = ' from(bucket:"Master")\
|> range(start: -5h)\
|> filter(fn:(r) => r._measurement == "D1")\
|> filter(fn: (r) => r.currency == "XAUUSD")\
|> filter(fn:(r) => r["_field"] == "close" or r["_field"] == "open")'

result = client.query_api().query(org=org, query=query)

for table in result:
  for record in table.records:
    results.append((record.get_field(), record.get_value()))

print(results)
问题是,;每行结果如下所示:

{'result': '_result', 'table': 1, '_start': datetime.datetime(2021, 5, 4, 8, 58, 35, 12587, tzinfo=tzutc()), '_stop': datetime.datetime(2021, 5, 4, 13, 58, 35, 12587, tzinfo=tzutc()), '_time': datetime.datetime(2021, 5, 4, 13, 12, 56, 86095, tzinfo=tzutc()), '_value': 961, '_field': 'open', '_measurement': 'D1', 'currency': 'XAUUSD'}
而且它没有显示两个领域;打开和关闭(它们显示为单独的行,其中_字段对于一个条目为“打开”,对于第二个条目为“关闭”,对于同一条目为“关闭”

有没有一种方法可以让结果行在一个结果中同时包含两个字段值,而不是每个字段2;1?因为如果我添加更多字段,我必须找到一种方法,将n行合并为相同的价格

我曾尝试浏览XDB文档,但所有示例都只显示一个_字段值,而不是多个

在线上有一些答案使用pivot和regex,但我认为这不适合我的情况,比如MySQL中的一个简单查询:

SELECT open, close FROM XAUUSD WHERE interval="D1";
关于如何使用XDB解决这个“简单”任务,有什么想法或帮助吗?或者我只是使用了错误的工具来完成这项工作