Class: Avro2Kafka::KafkaPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/avro2kafka/kafka_publisher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(broker_list:, topic:, keys: [], data: {}) ⇒ KafkaPublisher

Returns a new instance of KafkaPublisher.



8
9
10
11
12
13
# File 'lib/avro2kafka/kafka_publisher.rb', line 8

def initialize(broker_list:, topic:, keys: [], data: {})
  @producer = Poseidon::Producer.new(broker_list, 'avro2kafka')
  @topic = topic
  @keys = keys
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/avro2kafka/kafka_publisher.rb', line 6

def data
  @data
end

#keysObject (readonly)

Returns the value of attribute keys.



6
7
8
# File 'lib/avro2kafka/kafka_publisher.rb', line 6

def keys
  @keys
end

#producerObject (readonly)

Returns the value of attribute producer.



6
7
8
# File 'lib/avro2kafka/kafka_publisher.rb', line 6

def producer
  @producer
end

#topicObject (readonly)

Returns the value of attribute topic.



6
7
8
# File 'lib/avro2kafka/kafka_publisher.rb', line 6

def topic
  @topic
end

Instance Method Details

#publish(records) ⇒ Object



15
16
17
18
19
20
# File 'lib/avro2kafka/kafka_publisher.rb', line 15

def publish(records)
  records.each_slice(100) do |batch|
    messages = batch.map { |record| prepare_record(record) }
    producer.send_messages(messages)
  end
end