• influxdb-client-swift
    • Documentation
    • Features
    • Supported Platforms
    • Installation
      • Swift Package Manager
    • Usage
      • Creating a client
        • Client Options
          • Configure default Bucket, Organization and Precision
        • InfluxDB 1.8 API compatibility
      • Writes
      • Queries
        • Query to FluxRecord
        • Query to Data
        • Parameterized queries
      • Delete data
      • Management API
    • Advanced Usage
      • Default Tags
        • Example
          • Example Output
      • Proxy and redirects
        • Redirects
    • Contributing
    • License

    Swift - 图1influxdb-client-swift

    CircleCI codecov Platforms License Documentation GitHub issues GitHub pull requests Slack Status

    This repository contains the reference Swift client for the InfluxDB 2.x.

    Swift - 图10Documentation

    This section contains links to the client library documentation.

    Swift - 图11Features

    InfluxDB 2.x client consists of two packages

    • InfluxDBSwift
      • Querying data using the Flux language
      • Writing data
        • batched in chunks on background
        • automatic retries on write failures
    • InfluxDBSwiftApis
      • provides all other InfluxDB 2.x APIs for managing
        • health check
        • sources, buckets
        • tasks
        • authorizations
      • built on top of InfluxDBSwift

    Swift - 图12Supported Platforms

    This package requires Swift 5 and Xcode 12+.

    • iOS 14.0+
    • macOS 11.0+
    • tvOS 14.0+
    • watchOS 7.0+
    • Linux

    Swift - 图13Installation

    Swift - 图14Swift Package Manager

    Add this line to your Package.swift :

    1. // swift-tools-version:5.3
    2. import PackageDescription
    3. let package = Package(
    4. name: "MyPackage",
    5. dependencies: [
    6. .package(name: "influxdb-client-swift", url: "https://github.com/influxdata/influxdb-client-swift", from: "1.6.0"),
    7. ],
    8. targets: [
    9. .target(name: "MyModule", dependencies: [
    10. .product(name: "InfluxDBSwift", package: "influxdb-client-swift"),
    11. // or InfluxDBSwiftApis for management API
    12. .product(name: "InfluxDBSwiftApis", package: "influxdb-client-swift")
    13. ])
    14. ]
    15. )

    Swift - 图15Usage

    Important: You should call close() at the end of your application to release allocated resources.

    Swift - 图16Creating a client

    Specify url and token via parameters:

    1. let client = InfluxDBClient(url: "http://localhost:8086", token: "my-token")
    2. ...
    3. client.close()

    Swift - 图17Client Options

    OptionDescriptionTypeDefault
    bucketDefault destination bucket for writesStringnone
    orgDefault organization bucket for writesStringnone
    precisionDefault precision for the unix timestamps within the body line-protocolTimestampPrecisionns
    timeoutIntervalForRequestThe timeout interval to use when waiting for additional data.TimeInterval60 sec
    timeoutIntervalForResourceThe maximum amount of time that a resource request should be allowed to take.TimeInterval5 min
    enableGzipEnable Gzip compression for HTTP requests.Boolfalse
    debuggingEnable debugging for HTTP request/response.Boolfalse
    Swift - 图18Configure default Bucket, Organization and Precision
    1. let options: InfluxDBClient.InfluxDBOptions = InfluxDBClient.InfluxDBOptions(
    2. bucket: "my-bucket",
    3. org: "my-org",
    4. precision: .ns)
    5. let client = InfluxDBClient(url: "http://localhost:8086", token: "my-token", options: options)
    6. ...
    7. client.close()

    Swift - 图19InfluxDB 1.8 API compatibility

    1. client = InfluxDBClient(
    2. url: "http://localhost:8086",
    3. username: "user",
    4. password: "pass",
    5. database: "my-db",
    6. retentionPolicy: "autogen")
    7. ...
    8. client.close()

    Swift - 图20Writes

    The WriteApi supports asynchronous writes into InfluxDB 2.x. The results of writes could be handled by (response, error), Swift.Result or Combine.

    The data could be written as:

    1. String that is formatted as a InfluxDB’s Line Protocol
    2. Data Point structure
    3. Tuple style mapping with keys: measurement, tags, fields and time
    4. Array of above items

    The following example demonstrates how to write data with Data Point structure. For further information see docs and examples.

    1. import ArgumentParser
    2. import Foundation
    3. import InfluxDBSwift
    4. import InfluxDBSwiftApis
    5. @main
    6. struct WriteData: AsyncParsableCommand {
    7. @Option(name: .shortAndLong, help: "The name or id of the bucket destination.")
    8. private var bucket: String
    9. @Option(name: .shortAndLong, help: "The name or id of the organization destination.")
    10. private var org: String
    11. @Option(name: .shortAndLong, help: "Authentication token.")
    12. private var token: String
    13. @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
    14. private var url: String
    15. }
    16. extension WriteData {
    17. mutating func run() async throws {
    18. //
    19. // Initialize Client with default Bucket and Organization
    20. //
    21. let client = InfluxDBClient(
    22. url: url,
    23. token: token,
    24. options: InfluxDBClient.InfluxDBOptions(bucket: bucket, org: org))
    25. //
    26. // Record defined as Data Point
    27. //
    28. let recordPoint = InfluxDBClient
    29. .Point("demo")
    30. .addTag(key: "type", value: "point")
    31. .addField(key: "value", value: .int(2))
    32. //
    33. // Record defined as Data Point with Timestamp
    34. //
    35. let recordPointDate = InfluxDBClient
    36. .Point("demo")
    37. .addTag(key: "type", value: "point-timestamp")
    38. .addField(key: "value", value: .int(2))
    39. .time(time: .date(Date()))
    40. try await client.makeWriteAPI().write(points: [recordPoint, recordPointDate])
    41. print("Written data:\n\n\([recordPoint, recordPointDate].map { "\t- \($0)" }.joined(separator: "\n"))")
    42. print("\nSuccess!")
    43. client.close()
    44. }
    45. }

    Swift - 图21Queries

    The result retrieved by QueryApi could be formatted as a:

    1. Lazy sequence of FluxRecord
    2. Raw query response as a Data.

    Swift - 图22Query to FluxRecord

    1. import ArgumentParser
    2. import Foundation
    3. import InfluxDBSwift
    4. import InfluxDBSwiftApis
    5. @main
    6. struct QueryCpu: AsyncParsableCommand {
    7. @Option(name: .shortAndLong, help: "The name or id of the bucket destination.")
    8. private var bucket: String
    9. @Option(name: .shortAndLong, help: "The name or id of the organization destination.")
    10. private var org: String
    11. @Option(name: .shortAndLong, help: "Authentication token.")
    12. private var token: String
    13. @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
    14. private var url: String
    15. }
    16. extension QueryCpu {
    17. mutating func run() async throws {
    18. //
    19. // Initialize Client with default Bucket and Organization
    20. //
    21. let client = InfluxDBClient(
    22. url: url,
    23. token: token,
    24. options: InfluxDBClient.InfluxDBOptions(bucket: bucket, org: org))
    25. // Flux query
    26. let query = """
    27. from(bucket: "\(self.bucket)")
    28. |> range(start: -10m)
    29. |> filter(fn: (r) => r["_measurement"] == "cpu")
    30. |> filter(fn: (r) => r["cpu"] == "cpu-total")
    31. |> filter(fn: (r) => r["_field"] == "usage_user" or r["_field"] == "usage_system")
    32. |> last()
    33. """
    34. print("\nQuery to execute:\n\(query)\n")
    35. let records = try await client.queryAPI.query(query: query)
    36. print("Query results:")
    37. try records.forEach { print(" > \($0.values["_field"]!): \($0.values["_value"]!)") }
    38. client.close()
    39. }
    40. }

    Swift - 图23Query to Data

    1. @main
    2. struct QueryCpuData: AsyncParsableCommand {
    3. @Option(name: .shortAndLong, help: "The name or id of the bucket destination.")
    4. private var bucket: String
    5. @Option(name: .shortAndLong, help: "The name or id of the organization destination.")
    6. private var org: String
    7. @Option(name: .shortAndLong, help: "Authentication token.")
    8. private var token: String
    9. @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
    10. private var url: String
    11. }
    12. extension QueryCpuData {
    13. mutating func run() async throws {
    14. //
    15. // Initialize Client with default Bucket and Organization
    16. //
    17. let client = InfluxDBClient(
    18. url: url,
    19. token: token,
    20. options: InfluxDBClient.InfluxDBOptions(bucket: bucket, org: org))
    21. // Flux query
    22. let query = """
    23. from(bucket: "\(self.bucket)")
    24. |> range(start: -10m)
    25. |> filter(fn: (r) => r["_measurement"] == "cpu")
    26. |> filter(fn: (r) => r["cpu"] == "cpu-total")
    27. |> filter(fn: (r) => r["_field"] == "usage_user" or r["_field"] == "usage_system")
    28. |> last()
    29. """
    30. print("\nQuery to execute:\n\(query)\n")
    31. let response = try await client.queryAPI.queryRaw(query: query)
    32. let csv = String(decoding: response, as: UTF8.self)
    33. print("InfluxDB response: \(csv)")
    34. client.close()
    35. }
    36. }

    Swift - 图24Parameterized queries

    InfluxDB Cloud supports Parameterized Queries that let you dynamically change values in a query using the InfluxDB API. Parameterized queries make Flux queries more reusable and can also be used to help prevent injection attacks.

    InfluxDB Cloud inserts the params object into the Flux query as a Flux record named params. Use dot or bracket notation to access parameters in the params record in your Flux query. Parameterized Flux queries support only int , float, and string data types. To convert the supported data types into other Flux basic data types, use Flux type conversion functions.

    Parameterized query example:

    ⚠️ Parameterized Queries are supported only in InfluxDB Cloud, currently there is no support in InfluxDB OSS.

    1. import ArgumentParser
    2. import Foundation
    3. import InfluxDBSwift
    4. import InfluxDBSwiftApis
    5. @main
    6. struct ParameterizedQuery: AsyncParsableCommand {
    7. @Option(name: .shortAndLong, help: "The bucket to query. The name or id of the bucket destination.")
    8. private var bucket: String
    9. @Option(name: .shortAndLong,
    10. help: "The organization executing the query. Takes either the `ID` or `Name` interchangeably.")
    11. private var org: String
    12. @Option(name: .shortAndLong, help: "Authentication token.")
    13. private var token: String
    14. @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
    15. private var url: String
    16. }
    17. extension ParameterizedQuery {
    18. mutating func run() async throws {
    19. // Initialize Client with default Organization
    20. let client = InfluxDBClient(
    21. url: url,
    22. token: token,
    23. options: InfluxDBClient.InfluxDBOptions(bucket: bucket, org: org))
    24. for index in 1...3 {
    25. let point = InfluxDBClient
    26. .Point("demo")
    27. .addTag(key: "type", value: "point")
    28. .addField(key: "value", value: .int(index))
    29. try await client.makeWriteAPI().write(point: point)
    30. }
    31. // Flux query
    32. let query = """
    33. from(bucket: params.bucketParam)
    34. |> range(start: -10m)
    35. |> filter(fn: (r) => r["_measurement"] == params.measurement)
    36. """
    37. // Query parameters [String:String]
    38. let queryParams = ["bucketParam": "\(bucket)", "measurement": "demo"]
    39. print("\nQuery to execute:\n\n\(query)\n\n\(queryParams)")
    40. let records = try await client.queryAPI.query(query: query, params: queryParams)
    41. print("\nSuccess response...\n")
    42. try records.forEach { print(" > \($0.values["_field"]!): \($0.values["_value"]!)") }
    43. client.close()
    44. }
    45. }

    Swift - 图25Delete data

    The DeleteAPI supports deletes points from an InfluxDB bucket. Use the DeletePredicateRequest identifies which points to delete.

    1. import ArgumentParser
    2. import Foundation
    3. import InfluxDBSwift
    4. import InfluxDBSwiftApis
    5. @main
    6. struct DeleteData: AsyncParsableCommand {
    7. @Option(name: .shortAndLong, help: "Specifies the bucket name to delete data from.")
    8. private var bucket: String
    9. @Option(name: .shortAndLong,
    10. help: "Specifies the organization name to delete data from.")
    11. private var org: String
    12. @Option(name: .shortAndLong, help: "Authentication token.")
    13. private var token: String
    14. @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
    15. private var url: String
    16. @Option(name: .shortAndLong, help: "InfluxQL-like delete predicate statement.")
    17. private var predicate: String
    18. }
    19. extension DeleteData {
    20. mutating func run() async throws {
    21. // Initialize Client with default Organization
    22. let client = InfluxDBClient(
    23. url: url,
    24. token: token,
    25. options: InfluxDBClient.InfluxDBOptions(org: self.org))
    26. // Create DeletePredicateRequest
    27. let predicateRequest = DeletePredicateRequest(
    28. start: Date(timeIntervalSince1970: 0),
    29. stop: Date(),
    30. predicate: predicate)
    31. try await client.deleteAPI.delete(predicate: predicateRequest, bucket: bucket, org: org)
    32. print("\nDeleted data by predicate:\n\n\t\(predicateRequest)")
    33. // Print date after Delete
    34. try await queryData(client: client)
    35. client.close()
    36. }
    37. private func queryData(client: InfluxDBClient) async throws {
    38. let query = """
    39. from(bucket: "\(bucket)")
    40. |> range(start: 0)
    41. |> filter(fn: (r) => r["_measurement"] == "server")
    42. |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
    43. """
    44. let response = try await client.queryAPI.query(query: query)
    45. print("\nRemaining data after delete:\n")
    46. try response.forEach { record in
    47. let provider = record.values["provider"]!
    48. let production = record.values["production"]
    49. let app = record.values["app"]
    50. return print("\t\(provider),production=\(production!),app=\(app!)")
    51. }
    52. }
    53. }

    Swift - 图26Management API

    The client supports following management API:

    API docs
    AuthorizationsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Authorizations
    BucketsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Buckets
    DBRPsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/DBRPs
    HealthAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Health
    PingAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Ping
    LabelsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Labels
    OrganizationsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Organizations
    ReadyAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Ready
    ScraperTargetsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/ScraperTargets
    SecretsAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Secrets
    SetupAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Tasks
    SourcesAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Sources
    TasksAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Tasks
    UsersAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Users
    VariablesAPIhttps://docs.influxdata.com/influxdb/latest/api/#tag/Variables

    The following example demonstrates how to use a InfluxDB 2.0 Management API to create new bucket. For further information see docs and examples.

    1. import ArgumentParser
    2. import Foundation
    3. import InfluxDBSwift
    4. import InfluxDBSwiftApis
    5. @main
    6. struct CreateNewBucket: AsyncParsableCommand {
    7. @Option(name: .shortAndLong, help: "New bucket name.")
    8. private var name: String
    9. @Option(name: .shortAndLong, help: "Duration bucket will retain data.")
    10. private var retention: Int64 = 3600
    11. @Option(name: .shortAndLong, help: "Specifies the organization name.")
    12. private var org: String
    13. @Option(name: .shortAndLong, help: "Authentication token.")
    14. private var token: String
    15. @Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
    16. private var url: String
    17. }
    18. extension CreateNewBucket {
    19. mutating func run() async throws {
    20. // Initialize Client and API
    21. let client = InfluxDBClient(url: url, token: token)
    22. let api = InfluxDB2API(client: client)
    23. let orgId = (try await api.organizationsAPI.getOrgs(org: org)!).orgs?.first?.id
    24. // Bucket configuration
    25. let request = PostBucketRequest(
    26. orgID: orgId!,
    27. name: name,
    28. retentionRules: [RetentionRule(type: RetentionRule.ModelType.expire, everySeconds: retention)])
    29. // Create Bucket
    30. let bucket = try await api.bucketsAPI.postBuckets(postBucketRequest: request)!
    31. // Create Authorization with permission to read/write created bucket
    32. let bucketResource = Resource(
    33. type: Resource.ModelType.buckets,
    34. id: bucket.id,
    35. orgID: orgId
    36. )
    37. // Authorization configuration
    38. let authorizationRequest = AuthorizationPostRequest(
    39. description: "Authorization to read/write bucket: \(name)",
    40. orgID: orgId!,
    41. permissions: [
    42. Permission(action: Permission.Action.read, resource: bucketResource),
    43. Permission(action: Permission.Action.write, resource: bucketResource)
    44. ])
    45. // Create Authorization
    46. let authorization = try await api.authorizationsAPI.postAuthorizations(authorizationPostRequest: authorizationRequest)!
    47. print("The bucket: '\(bucket.name)' is successfully created.")
    48. print("The following token could be use to read/write:")
    49. print("\t\(authorization.token!)")
    50. client.close()
    51. }
    52. }

    Swift - 图27Advanced Usage

    Swift - 图28Default Tags

    Sometimes is useful to store same information in every measurement e.g. hostname, location, customer. The client is able to use static value or env variable as a tag value.

    The expressions:

    • California Miner - static value
    • ${env.HOST_NAME} - environment property

    Swift - 图29Example

    1. client = InfluxDBClient(
    2. url: "http://localhost:8086",
    3. token: "my-token",
    4. options: InfluxDBClient.InfluxDBOptions(bucket: "my-bucket", org: "my-org"))
    5. let tuple: InfluxDBClient.Point.Tuple
    6. = (measurement: "mem", tags: ["tag": "a"], fields: ["value": .int(3)], time: nil)
    7. let records: [Any] = [
    8. InfluxDBClient.Point("mining")
    9. .addTag(key: "sensor_state", value: "normal")
    10. .addField(key: "depth", value: .int(2)),
    11. tuple
    12. ]
    13. let defaultTags = InfluxDBClient.PointSettings()
    14. .addDefaultTag(key: "customer", value: "California Miner")
    15. .addDefaultTag(key: "sensor_id", value: "${env.SENSOR_ID}")
    16. try await client.makeWriteAPI(pointSettings: defaultTags).writeRecords(records: records)
    17. print("Successfully written default tags")
    Swift - 图30Example Output
    1. mining,customer=California\ Miner,sensor_id=123-456-789,sensor_state=normal depth=2i
    2. mining,customer=California\ Miner,sensor_id=123-456-789,sensor_state=normal pressure=3i

    Swift - 图31Proxy and redirects

    ⚠️ The connectionProxyDictionary cannot be defined on Linux. You have to set HTTPS_PROXY or HTTP_PROXY system environment.

    You can configure the client to tunnel requests through an HTTP proxy by connectionProxyDictionary option:

    1. var connectionProxyDictionary = [AnyHashable: Any]()
    2. connectionProxyDictionary[kCFNetworkProxiesHTTPEnable as String] = 1
    3. connectionProxyDictionary[kCFNetworkProxiesHTTPProxy as String] = "localhost"
    4. connectionProxyDictionary[kCFNetworkProxiesHTTPPort as String] = 3128
    5. let options: InfluxDBClient.InfluxDBOptions = InfluxDBClient.InfluxDBOptions(
    6. bucket: "my-bucket",
    7. org: "my-org",
    8. precision: .ns,
    9. connectionProxyDictionary: connectionProxyDictionary)
    10. client = InfluxDBClient(url: "http://localhost:8086", token: "my-token", options: options)

    For more info see - URLSessionConfiguration.connectionProxyDictionary, Global Proxy Settings Constants.

    Swift - 图32Redirects

    Client automatically follows HTTP redirects. You can disable redirects by an urlSessionDelegate configuration:

    1. class DisableRedirect: NSObject, URLSessionTaskDelegate {
    2. func urlSession(_ session: URLSession,
    3. task: URLSessionTask,
    4. willPerformHTTPRedirection response: HTTPURLResponse,
    5. newRequest request: URLRequest,
    6. completionHandler: @escaping (URLRequest?) -> Void) {
    7. completionHandler(nil)
    8. }
    9. }
    10. let options = InfluxDBClient.InfluxDBOptions(
    11. bucket: "my-bucket",
    12. org: "my-org",
    13. urlSessionDelegate: DisableRedirect())
    14. client = InfluxDBClient(url: "http://localhost:8086", token: "my-token", options: options)

    For more info see - URLSessionDelegate.

    Swift - 图33Contributing

    If you would like to contribute code you can do through GitHub by forking the repository and sending a pull request into the master branch.

    Build Requirements:

    • swift 5.3 or higher

    Build source and test targets:

    1. swift build --build-tests

    Run tests:

    1. ./Scripts/influxdb-restart.sh
    2. swift test

    Check code coverage:

    1. ./Scripts/influxdb-restart.sh
    2. swift test --enable-code-coverage

    You could also use a docker-cli without installing the Swift SDK:

    1. make docker-cli
    2. swift build

    Swift - 图34License

    The client is available as open source under the terms of the MIT License.