【问题标题】:How to query data from InfluxDB using Node.js如何使用 Node.js 从 InfluxDB 查询数据
【发布时间】:2021-01-03 21:04:06
【问题描述】:

我正在做一个项目,我有一个 InfluxDB 存储桶,其尺寸为 elapsedtime,标签为 service。我想查询 Influx 以便能够在过去 1 小时内获取 foobar 作为服务的所有数据点。理想情况下,我稍后会添加一个时间测量值,我可以用它来确定我 1 小时的休息时间,因为获取经过时间的系统和将其写入 Influx 的系统是不同的,并且它们之间有大约 1-2 分钟的延迟。

我从here 获取了一些示例代码,我得到了几乎相同的代码,因为我不确定需要更改什么并且无法理解文档(头脑混乱?)。

这样做的最终目标是,当我查询我的应用程序(查询 Influx)时,能够有一个图表显示服务的 elapsedtime。我希望能够根据预设的服务和时间列表进行查询,但这是应用程序方面的事情,我在这里给出我希望最终结果的上下文。

...
variables that define bucket, url, org and token
...
const queryApi = new InfluxDB({url, token}).getQueryApi(org)
const fluxQuery =
  `from(bucket:"${bucket}") |> range(start: 0) |> filter(fn: (r) => r._measurement == "elapsedTime")`

console.log('*** QUERY ROWS ***')
// Execute query and receive table metadata and rows.
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
queryApi.queryRows(fluxQuery, {
  next(row: string[], tableMeta: FluxTableMetaData) {
    const o = tableMeta.toObject(row)
    console.log(
      `${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
    )
  },
  error(error: Error) {
    console.error(error)
  },
  complete() {
    console.log('\nFinished SUCCESS')
  },
})

当我运行它时,我收到一个关于额外值的错误,但是我希望该示例具有正确的代码,所以也许我遗漏了一些我需要更新的东西?

  next(row: string[], tableMeta: FluxTableMetaData) {
          ^

SyntaxError: Unexpected token ':'
    at wrapSafe (internal/modules/cjs/loader.js:992:16)
    at Module._compile (internal/modules/cjs/loader.js:1040:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

【问题讨论】:

    标签: node.js time-series influxdb influxdb-2


    【解决方案1】:

    问题最终是我复制的代码是 TypeScript,您可以想象它在 JavaScript 文件中不起作用。我的愚蠢错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多