Go to file
R. Tyler Croy 1c7543f730
Upgrade to a Kafka (JVM) version which uses Scala 2.11
Scala 2.10 is three years old and it appears to me tha 2.10 is moderately
broken and old, at that 2.11 is the bare minimum folks should be using these
days (see also: https://github.com/twitter/scrooge/pull/198)
2015-09-28 08:43:50 -07:00
ext/hermann Comment that rdcrc32.h and rd_kafka_msg_partitioner_consistent can be removed when librdkafka tags and Hermann updates 2015-09-16 16:04:35 +00:00
lib Remove hermann_jars.rb to gem installation to generate it properly 2015-09-23 07:52:21 -07:00
scripts update api to have #topic(topic) as well as #topics 2015-06-11 15:29:02 -07:00
spec Fix java_producer_spec for modified push_single signature 2015-09-22 18:36:10 +00:00
.gitignore Properly include hermann_jars.rb in the built gem but exclude from the tree 2015-09-23 13:19:40 -07:00
.travis.yml Remove auto-deploy until Java- specific versions can be published too 2015-06-15 06:18:02 -07:00
Gemfile Rework the dependencies and some of the code to support retro rubies better 2015-05-07 13:18:41 -07:00
HACKING.md Update Hacking.md with mri instructions 2015-05-19 10:33:00 +02:00
Jarfile Bring back Jarfile and remove curator from dependency 2015-03-27 17:25:31 -07:00
LICENSE.md Moved license to LICENSE.md and reformatted markdown. Closes #1 2014-08-18 13:48:56 -07:00
README.md yield key and offset into Consumer#consume block 2015-06-30 10:49:37 -07:00
Rakefile move hermann_lib -> hermann_rdkafka / Hermann::Provider::RDKafka 2015-06-23 09:01:31 -07:00
hermann.gemspec Upgrade to a Kafka (JVM) version which uses Scala 2.11 2015-09-28 08:43:50 -07:00

README.md

Hermann

Gitter chat Build Status

A Ruby gem implementing a Kafka Publisher and Consumer

On MRI (C-based Ruby), this library wraps the librdkafka library which is implemented in C.

On JRuby this library declares jar dependencies inside the .gemspec to express dependencies on the Java-based Kafka library provided by the Kafka project. Tools like jbundler will handle these declarations correctly.

Usage

Usage is modelled on the kafka-rb gem and is fairly straightforward.

  • Kafka 0.8 is supported.
  • Ruby 1.9.3, 2.1.1 and JRuby are tested against

Producer

Zookeeper discovery

Discover Kafka brokers through zookeeper. Looks at /brokers/ids in Zookeeper to find the list of brokers.

require 'hermann/producer'
require 'hermann/discovery/zookeeper'

broker_ids_array = Hermann::Discovery::Zookeeper.new('localhost:2181').get_brokers
producer = Hermann::Producer.new('topic', broker_ids_array)

promise = producer.push('hello world') # send message to kafka
promise.value                          # forces the Concurrent::Promise to finish excuting (#value!)
promise.state                          # the state of the promise

MRI only

require 'hermann/producer'

broker_ids_array = Hermann::Discovery::Zookeeper.new('localhost:2181').get_brokers
p = Hermann::Producer.new('topic', broker_ids_array)  # arguments topic, list of brokers
f = p.push('hello world from mri')
f.state
p.tick_reactor
f.state

Consumer

Messages can be consumed by calling the consume method and passing a block to handle the yielded messages. The consume method blocks, so take care to handle that functionality appropriately (i.e. use Concurrent::Promise, Thread, etc).

(JRuby)

require 'hermann'
require 'hermann/consumer'
require 'hermann_jars'

topic     = 'topic'
new_topic = 'other_topic'

the_consumer = Hermann::Consumer.new(topic, zookeepers: "localhost:2181", group_id: "group1")

the_consumer.consume(new_topic) do |msg|   # can change topic with optional argument to .consume
  puts "Recv: #{msg}"
end

(MRI)

MRI currently has no zookeeper / client group support.

require 'hermann'
require 'hermann/consumer'

topic     = 'topic'
new_topic = 'other_topic'

the_consumer = Hermann::Consumer.new(topic, brokers: "localhost:9092", partition: 1)

the_consumer.consume(new_topic) do |msg, key, offset|   # can change topic with optional argument to .consume
  puts "Recv: #{msg}, key: #{key}, offset: #{offset}"
end

Metadata request (MRI-only)

Topic and cluster metadata may be retrieved in the MRI version by querying the Kafka brokers.

require 'hermann'
require 'hermann/discovery/metadata'

c = Hermann::Discovery::Metadata.new( "localhost:9092" )
topic = c.topic("topic")

puts topic.partitions.first

consumers = topic.partitions.map do |partition|
  partition.consumer
end

Testing

To run the integration tests:

  • startup your own instance of zookeeper/kafka
  • rspec spec/integration/producer_spec.rb

How to convert from using jruby-kafka

  • Gemfile
    • remove jruby-kafka
    • add gem "hermann"
    • bundle install
  • Jarfile
    • removed unecessary jars from your Jarfile (i.e. kafka, log4j)
    • jar dependencies are automatically included with Hermann
    • jbundle install
  • Test out one of the Producer/Consumer examples above