zendesk / ruby-kafka

A Ruby client library for Apache Kafka
http://www.rubydoc.info/gems/ruby-kafka
Apache License 2.0
1.27k stars 340 forks source link

Batch processing giving error '' uninitialized constant SearchIndex (NameError)" #879

Closed shan2k6 closed 3 years ago

shan2k6 commented 3 years ago

If this is a bug report, please fill out the following:

Please verify that the problem you're seeing hasn't been fixed by the current master of ruby-kafka.

Steps to reproduce
require 'kafka'
khost = 'xx.xx.xx.xx'
kport = 'xxxx'
kafka = Kafka.new(["#{khost}:#{kport}"])
consumer = kafka.consumer(group_id: "my-consumer")
# A mock search index that we'll be keeping up to date with new Kafka messages.
index = SearchIndex.new

consumer.subscribe("topic")

consumer.each_batch do |batch|
  puts "Received batch: #{batch.topic}/#{batch.partition}"

  transaction = index.transaction

  batch.messages.each do |message|
    # Let's assume that adding a document is idempotent.
    transaction.add(id: message.key, body: message.value)
  end

  # Once this method returns, the messages have been successfully written to the
  # search index. The consumer will only checkpoint a batch *after* the block
  # has completed without an exception.
  transaction.commit!
end
Error

ruby batch.rb Traceback (most recent call last): batch.rb:7:in `

': uninitialized constant SearchIndex (NameError)

Looks like method ' SearchIndex' is not available

geoff2k commented 3 years ago

The example from the README that uses SearchIndex isn't meant to be used a production code, it's just there to be an example of how a hypothetical SearchIndex class might absorb batches of messages as they were received by the consumer's #each_batch method. That's why the comment refers to it as a "mock" index.

Could you give a shot at rewriting the documentation to make that clearer?