QueryAPI

public class QueryAPI

The asynchronous API to Query InfluxDB 2.x.

Example:

Query into sequence of FluxRecord

let query = """
            from(bucket: "my-bucket")
                |> range(start: -10m)
                |> filter(fn: (r) => r["_measurement"] == "cpu")
                |> filter(fn: (r) => r["cpu"] == "cpu-total")
                |> filter(fn: (r) => r["_field"] == "usage_user" or r["_field"] == "usage_system")
                |> last()
            """

print("\nQuery to execute:\n\(query)\n")

let records = try await client.queryAPI.query(query: query)

print("Query results:")
try records.forEach { print(" > \($0.values["_field"]!): \($0.values["_value"]!)") }

Query into Data

let response = try await client.queryAPI.queryRaw(query: query)

let csv = String(decoding: response, as: UTF8.self)
print("InfluxDB response: \(csv)")

client.close()
  • The default Query Dialect with annotation = [“datatype”, “group”, “default”]

    Declaration

    Swift

    public static let defaultDialect: Dialect
  • Query executes a query and returns the response as a Cursor<FluxRecord>.

    Declaration

    Swift

    public func query(query: String,
                      org: String? = nil,
                      params: [String: String]? = nil,
                      responseQueue: DispatchQueue = .main,
                      completion: @escaping (_ response: FluxRecordCursor?,
                                             _ error: InfluxDBClient.InfluxDBError?) -> Void)

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    completion

    The handler to receive the data and the error objects.

  • Query executes a query and returns the response as a Cursor<FluxRecord>.

    Declaration

    Swift

    public func query(query: String,
                      org: String? = nil,
                      params: [String: String]? = nil,
                      responseQueue: DispatchQueue = .main,
                      completion: @escaping (
                              _ result: Swift.Result<FluxRecordCursor, InfluxDBClient.InfluxDBError>) -> Void)

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    completion

    completion handler to receive the Swift.Result

  • Query executes a query and returns the response as a Cursor<FluxRecord>.

    Declaration

    Swift

    @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    public func query(query: String,
                      org: String? = nil,
                      params: [String: String]? = nil,
                      responseQueue: DispatchQueue = .main)
            -> AnyPublisher<FluxRecordCursor, InfluxDBClient.InfluxDBError>

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    Return Value

    Publisher to attach a subscriber

  • Query executes a query and asynchronously returns the response as a Cursor<FluxRecord>.

    Declaration

    Swift

    @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
    public func query(query: String,
                      org: String? = nil,
                      params: [String: String]? = nil,
                      responseQueue: DispatchQueue = .main) async throws -> FluxRecordCursor

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    Return Value

    Cursor<FluxRecord>

  • QueryRaw executes a query and returns the response as a Data.

    Declaration

    Swift

    public func queryRaw(query: String,
                         org: String? = nil,
                         dialect: Dialect = defaultDialect,
                         params: [String: String]? = nil,
                         responseQueue: DispatchQueue = .main,
                         completion: @escaping (_ response: Data?, _ error: InfluxDBClient.InfluxDBError?) -> Void)

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    dialect

    The Dialect are options to change the default CSV output format.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    completion

    handler to receive the data and the error objects

  • QueryRaw executes a query and returns the response as a Data.

    Declaration

    Swift

    public func queryRaw(query: String,
                         org: String? = nil,
                         dialect: Dialect = defaultDialect,
                         params: [String: String]? = nil,
                         responseQueue: DispatchQueue = .main,
                         completion: @escaping (_ result: Swift.Result<Data, InfluxDBClient.InfluxDBError>) -> Void)

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    dialect

    The Dialect are options to change the default CSV output format.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    completion

    completion handler to receive the Swift.Result

  • QueryRaw executes a query and returns the response as a Data.

    Declaration

    Swift

    @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    public func queryRaw(query: String,
                         org: String? = nil,
                         dialect: Dialect = defaultDialect,
                         params: [String: String]? = nil,
                         responseQueue: DispatchQueue = .main) -> AnyPublisher<Data, InfluxDBClient.InfluxDBError>

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    dialect

    The Dialect are options to change the default CSV output format.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    Return Value

    Publisher to attach a subscriber

  • QueryRaw executes a query and asynchronously returns the response as a Data.

    Declaration

    Swift

    @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
    public func queryRaw(query: String,
                         org: String? = nil,
                         params: [String: String]? = nil,
                         responseQueue: DispatchQueue = .main) async throws -> Data

    Parameters

    query

    The Flux query to execute.

    org

    The organization executing the query. Takes either the ID or Name interchangeably.

    params

    params represent key/value pairs parameters to be injected into query

    responseQueue

    The queue on which api response is dispatched.

    Return Value

    Cursor<FluxRecord>

  • FluxTable holds flux query result table information represented by collection of columns.

    See more

    Declaration

    Swift

    public class FluxTable
  • FluxColumn holds flux query table column properties

    See more

    Declaration

    Swift

    public class FluxColumn
  • FluxRecord represents row in the flux query result table

    See more

    Declaration

    Swift

    public class FluxRecord : Equatable
  • Cursor for FluxRecord.

    See more

    Declaration

    Swift

    public final class FluxRecordCursor : Cursor