Skip to content

InfluxDB IOx

Brett Buddin edited this page Jan 13, 2023 · 3 revisions

Connecting

InfluxDB IOx requires bucket-name or bucket-id to be presented as gRPC metadata in Flight SQL requests. When creating a FlightSQLClient directly this means providing these keys in the metadata dictionary. When dialing via SQLAlchemy's DSN you will add bucket-name/bucket-id as additional query parameters.

Directly with Client

from flightsql import FlightSQLClient

client = FlightSQLClient(
  host="eu-central-1-1.aws.cloud2.influxdata.com",
  token="INFLUXDBTOKEN",
  metadata={"bucket-name": "_tasks"},
)

DB API 2 Interface

Same as Directly with Client, but passing the client into connect.

from flightsql import FlightSQLClient, connect

client = FlightSQLClient(
  host="eu-central-1-1.aws.cloud2.influxdata.com",
  token="INFLUXDBTOKEN",
  metadata={"bucket-name": "_tasks"},
)
conn = connect(client)

SQLAlchemy with the DataFusion Dialect

import flightsql.sqlalchemy
from sqlalchemy.engine import create_engine

engine = create_engine("datafusion+flightsql://eu-central-1-1.aws.cloud2.influxdata.com:443?token=INFLUXDBTOKEN&bucket-name=_tasks")

FlightSQLClient and Bucket Information

It's possible to specify bucket information at multiple points when you are using the FlightSQLClient directly. Below is an outline of the different strategies.

Per-RPC Bucket Override

This is the recommended approach.

Specify a bucket when creating the client, but potentially override it later if you need to query a different bucket.

from flightsql import FlightSQLClient
from flightsql.client import CallOptions

client = FlightSQLClient(
  host="eu-central-1-1.aws.cloud2.influxdata.com",
  token="INFLUXDBTOKEN",
  metadata={"bucket-name": "cool"},
)

# Executed against the `cool` bucket.
info = client.execute("select * from data")
results = client.do_get(info.endpoints[0].ticket)

# Override the bucket to source data from `rad`.
#
# While not strictly necessary to present `call_options` to *both* the `execute`
# and `do_get` calls, it's recommended, because Flight SQL is stateless.
call_options = CallOptions(headers=[(b'bucket-name', b'rad')])
info = client.execute("select * from data", call_options)
results = client.do_get(info.endpoints[0].ticket, call_options)

Per-RPC Bucket

Omit the bucket information from the client creation entirely and require RPCs to specify the bucket they are speaking about.

from flightsql import FlightSQLClient
from flightsql.client import CallOptions

# Notice we have not specified a bucket here. RPCs without `CallOptions` will fail.
client = FlightSQLClient(
  host="eu-central-1-1.aws.cloud2.influxdata.com",
  token="INFLUXDBTOKEN",
)

# Executed against the `cool` bucket.
call_options = CallOptions(headers=[(b'bucket-name', b'cool')])
info = client.execute("select * from data", call_options)
results = client.do_get(info.endpoints[0].ticket, call_options)

# Executed against the `rad` bucket.
call_options = CallOptions(headers=[(b'bucket-name', b'rad')])
info = client.execute("select * from data", call_options)
results = client.do_get(info.endpoints[0].ticket, call_options)
Clone this wiki locally