Sha256: 133ef25eee602b586e81039f76ded45e43bc0828a0daab4c5ef4af8ef216abfb

Contents?: true

Size: 1016 Bytes

Versions: 48

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true

# Reads lines from STDIN, writing them to Kafka.

$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 = "page-visits"

ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.set_params(
  cert: OpenSSL::X509::Certificate.new(ENV.fetch("KAFKA_CLIENT_CERT")),
  key: OpenSSL::PKey::RSA.new(ENV.fetch("KAFKA_CLIENT_CERT_KEY")),
)

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

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

48 entries across 48 versions & 3 rubygems

Version Path
ruby-kafka-1.5.0 examples/ssl-producer.rb
ruby-kafka-aws-iam-1.4.5 examples/ssl-producer.rb
ruby-kafka-aws-iam-1.4.4 examples/ssl-producer.rb
ruby-kafka-aws-iam-1.4.3 examples/ssl-producer.rb
ruby-kafka-aws-iam-1.4.2 examples/ssl-producer.rb
ruby-kafka-aws-iam-1.4.1 examples/ssl-producer.rb
ruby-kafka-1.4.0 examples/ssl-producer.rb
ruby-kafka-temp-fork-0.0.2 examples/ssl-producer.rb
ruby-kafka-temp-fork-0.0.1 examples/ssl-producer.rb
ruby-kafka-1.3.0 examples/ssl-producer.rb
ruby-kafka-1.2.0 examples/ssl-producer.rb
ruby-kafka-1.1.0 examples/ssl-producer.rb
ruby-kafka-1.1.0.beta1 examples/ssl-producer.rb
ruby-kafka-1.0.0 examples/ssl-producer.rb
ruby-kafka-0.7.10 examples/ssl-producer.rb
ruby-kafka-0.7.9 examples/ssl-producer.rb
ruby-kafka-0.7.8 examples/ssl-producer.rb
ruby-kafka-0.7.7 examples/ssl-producer.rb
ruby-kafka-0.7.6 examples/ssl-producer.rb
ruby-kafka-0.7.6.beta2 examples/ssl-producer.rb