Skip to content

attheodo/SwiftKafka

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftKafka

A Swifty wrapper for librdkafka for consuming and/or producing messages from/to Apache Kafka. The high level API was modeled around Confluent's Python Kafka Client. Confluent is also the author of librdkafka.

Swift 3.0 Platforms OS X | Linux License MIT attheodo Twitter

ATHSwift

TODO

  • Provide usage examples (WIP)
  • Write API documentation
  • Write tests

Installation

SwiftKafka is available through the Swift Package Manager.

Before importing this package, please make sure you have installed librdkafka first:

  • Linux
    • $ sudo apt-get install librdkafka-dev
  • macOS
    • $ brew install librdkafka
    • Please also note that a proper pkg-config path setting is required:
      • $ export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"

Quick Guide

Kafka Producer

Configuration

You can use the GlobalConfig and TopicConfig structs to configure your Kafka producer instance. You can set and get configuration variables either using plain string property names (as described in librdkafka Configuration Docs), or the more Swifty configuration enums.

Initialization & Connecting to Brokers

The producer instance is usually initialised with a GlobalConfig() struct.

import SwiftKafkaLib

let cfg = try GlobalConfig()

try cfg.set(.metadataBrokerList("host1:9092"))
try cfg.set(.clientId("MyProducerClientId"))

let producer = try KafkaProducer(globalConfiguration: cfg)

Instead of setting the metadata.brokers.list configuration property you can also connect to the broker using the Broker struct in conjuction with the connect() method of the producer:

let broker1 = Broker(host: "localhost")
let broker2 = Broker(withProtocol: .ssl, host: "some_host", port: 9092)

producer.connect(toBrokers: [broker1, broker2])

Setting Universal Message Delivery callback

If you want to invoke some code after any message has been produced, you need to set the onMessageDelivery closure to your producer instance. NOTE: Always check the error property to make sure the message was produced successfully.

producer.onMessageDelivery = { message, error in

    guard error == nil else {
        // Do something with the error
    }

    // do something with the message


}

Producing

Async Producing

All writes are asynchronous by default. In order to produce a message you need to call the produce method on the producer instance.

producer.produce(topic: "SomeTopic", key: "A Key", value: "A Value")

Author

Athanasios "attheodo" Theodoridis

License

SwiftKafka is available under the MIT license. See the LICENSE file for more info.

Changelog

  • v0.1.0, Aug 2017
    • Initial release

Releases

No releases published

Packages

No packages published

Languages