How to read NEW data from SQL Server to InfluxDB via FLUX

Hi everybody,

I read data from a SQL Server and write it on my InfluxDB via Flux.

Is there a chance I could read only new data from the SQL Server and write it on the InfluxDB?
I thought about writring the last timestamp and add a Where condition on the query but I don’t know if it is ok or not.

query: select * from dbo.table where timestamp > VARIABLE_NAME

If anybody knows how to do it or if it is another option let me know please.

Thank you a lot

Hi @JMU,
So I believe you are correct in your approach. Perhaps something like this as an example:

import "sql"
import "influxdata/influxdb/secrets"

rawtime = from(bucket: "Jetson")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "exec_jetson_stats")
  |> filter(fn: (r) => r["_field"] == "jetson_CPU1")
  |> last()
    |> findRecord(
        fn: (key) => key._field == "jetson_CPU1",
        idx: 0,
    )
  
  rawtime._time // this will give you the timestamp

username = secrets.get(key: "POSTGRES_USER")
password = secrets.get(key: "POSTGRES_PASS")

// Example of using it
sql.from(
    driverName: "postgres",
    dataSourceName: "postgresql://${username}:${password}@localhost:5432",
    query: "SELECT * FROM example_table WHERE time=" + string(v : rawtime._time),
)
2 Likes

Hi, according to your code example this is what I write:
Neither “v” nor “rawtime._time” are recognised.

Do you know why?

Hi @JMU,
Nice to see you using the Fux vscode plugin. So variables (v) can only be used within the context of a dashboard query. You will have to define your time range more specifically for now.
Something like:

|> range(start: -24h)

Please remove this rawtime._time :slight_smile:

 rawtime._time // this will give you the timestamp

This was just an example. It should work once you solve the dashboard variable issue.