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
.
- Provide usage examples (WIP)
- Write API documentation
- Write tests
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"
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.
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])
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
}
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")
Athanasios "attheodo" Theodoridis
SwiftKafka is available under the MIT license. See the LICENSE file for more info.
- v0.1.0, Aug 2017
- Initial release