Sha256: 0286681f6a6ef1d7892f3852ff961f5baa7c510729e996678d1868589ef03f85

Contents?: true

Size: 864 Bytes

Versions: 51

Compression:

Stored size: 864 Bytes

Contents

# Reads lines from STDIN, writing them to Kafka.
#
# You need to define the environment variable KAFKA_BROKERS for this
# to work, e.g.
#
#     export KAFKA_BROKERS=localhost:9092
#

$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))

require "kafka"

logger = Logger.new($stderr)
brokers = ENV.fetch("KAFKA_BROKERS")

# Make sure to create this topic in your Kafka cluster or configure the
# cluster to auto-create topics.
topic = "text"

kafka = Kafka.new(
  seed_brokers: brokers,
  client_id: "simple-producer",
  logger: logger,
)

producer = kafka.producer

begin
  $stdin.each_with_index do |line, index|
    producer.produce(line, topic: topic)

    # Send messages for every 10 lines.
    producer.deliver_messages if index % 10 == 0
  end
ensure
  # Make sure to send any remaining messages.
  producer.deliver_messages

  producer.shutdown
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
ruby-kafka-0.6.0.beta4 examples/simple-producer.rb
ruby-kafka-0.6.0.beta3 examples/simple-producer.rb
ruby-kafka-0.6.0.beta2 examples/simple-producer.rb
ruby-kafka-0.6.0.beta1 examples/simple-producer.rb
ruby-kafka-0.5.5 examples/simple-producer.rb
ruby-kafka-0.5.4 examples/simple-producer.rb
ruby-kafka-0.5.4.beta1 examples/simple-producer.rb
ruby-kafka-0.5.3 examples/simple-producer.rb
ruby-kafka-0.5.2 examples/simple-producer.rb
ruby-kafka-0.5.2.beta3 examples/simple-producer.rb
ruby-kafka-0.5.2.beta2 examples/simple-producer.rb
ruby-kafka-0.5.2.beta1 examples/simple-producer.rb
ruby-kafka-0.5.1 examples/simple-producer.rb
ruby-kafka-0.5.1.beta2 examples/simple-producer.rb
ruby-kafka-0.5.1.beta1 examples/simple-producer.rb
ruby-kafka-0.4.4 examples/simple-producer.rb
ruby-kafka-0.5.0 examples/simple-producer.rb
ruby-kafka-0.5.0.beta6 examples/simple-producer.rb
ruby-kafka-0.5.0.beta5 examples/simple-producer.rb
ruby-kafka-0.5.0.beta4 examples/simple-producer.rb