使用 Python influxdb_client 在通量查询中使用参数

问题描述

我正在尝试更新我们所有的 influxdb python 查询,以便它们不易受到 sql 注入的影响。

为此,我读到您可以将参数与 query_api() 一起使用,特别是与 query_data_frame() (https://medium.com/sekoia-io-blog/avoiding-injections-with-influxdb-bind-parameters-50f67e379abb) 一起使用

我遇到的问题是我不知道如何将我的参数传递到我的查询中。以下是我们的一个查询示例:

client = InfluxDBClient(url="localhost:5000",token="",timeout=100000,retries=0,enable_gzip=True,profilers="query,operator")
query_api = client.query_api()

ver = "data" # This variable would actually come from a function
params = {
    "ver": ver,}
query =                      '''from(bucket: "db")
                                |> range(start: -200d)
                                |> pivot(rowKey:["_time"],columnKey: ["_field"],valueColumn: "_value")
                                |> filter(fn: (r) => r._measurement == "test_result")
                                |> filter(fn: (r) => r.version == ver)
                                |> keep(columns: ["_time","test","run","status_tag","duration_sec","version"])'''

df = query_api.query_data_frame(query=query,params=params)

运行上面的代码会给我一个 HTTP response body: b'{"error":"type error 5:75-5:78: undefined identifier \\"ver\\""}\n' 错误

有谁知道如何使用 Python 将参数正确地注入到通量查询中?

我还使用了以下帮助: https://influxdb-client.readthedocs.io/_/downloads/en/stable/pdf/

根据 user16442705 问题更新

我在我的字典中尝试了另一个变量名,它产生了相同的结果。我还尝试在查询中使用 $ 产生不同的错误。请参阅以下错误代码

client = InfluxDBClient(url="localhost:5000",operator")
query_api = client.query_api()

ver = "data" # This variable would actually come from a function
params = {
    "pVersion": ver,valueColumn: "_value")
                                |> filter(fn: (r) => r._measurement == "test_result")
                                |> filter(fn: (r) => r.version == pVersion)
                                |> keep(columns: ["_time",params=params)

HTTP response body: b'{"error":"type error 5:67-5:80: undefined identifier \\"pVersion\\""}\n'

client = InfluxDBClient(url="localhost:5000",valueColumn: "_value")
                                |> filter(fn: (r) => r._measurement == "test_result")
                                |> filter(fn: (r) => r.version == $pVersion)
                                |> keep(columns: ["_time",params=params)

HTTP response body: b'{"error":"loc 0:0-0:0: expected an operator between two expressions"}\n'

一个需要注意的数据点是我们正在使用以下版本:

  • Influxdb 版本:1.8.6
  • influxdb 客户端:1.19.0

解决方法

尝试在查询字符串中使用 $ver,而不是 ver

client = InfluxDBClient(url="localhost:5000",token="",timeout=100000,retries=0,enable_gzip=True,profilers="query,operator")
query_api = client.query_api()

ver = "data" # This variable would actually come from a function
params = {
    "ver": ver,}
query = '''from(bucket: "db")
           |> range(start: -200d)
           |> pivot(rowKey:["_time"],columnKey: ["_field"],valueColumn: "_value")
           |> filter(fn: (r) => r._measurement == "test_result")
           |> filter(fn: (r) => r.version == $ver)  # <--------------------------------- 
           |> keep(columns: ["_time","test","run","status_tag","duration_sec","version"])'''

df = query_api.query_data_frame(query=query,params=params)

Disclaimer

,

问题实际上出在我使用的 influxdb 版本 (1.8.6) 上。查询参数不是 Influxdb 1.8.6 的特性,仅在 Influxdb 2.0.x 中引入

有关 Influxdb-python-client 团队提出的问题,请参阅下面的链接。 https://github.com/influxdata/influxdb-client-python/issues/285

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...