kafka_rest_client 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -2
- data/kafka_rest_client.gemspec +1 -1
- data/lib/kafka_rest_client/avro_producer.rb +15 -10
- data/lib/kafka_rest_client/errors.rb +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f604a5dc722bd5ab97847866e46f6326774bc4b5
|
4
|
+
data.tar.gz: 3d0d283bb69fe749ea2beb0c633e8f4119a190e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1f3a451c16bd800b1c521aea53faf45dfd4610d326f7c7b201daf975e6114e2260046de1bbc424f15a3afbb70c46ee63f5b3dbe167bbb331a07f9a7d876fcad
|
7
|
+
data.tar.gz: 2806b9c5e14cddc9df9ca8e17225830f54594860651758aeec031946a196abee1e552d1d53e9a64c478f333669c887875b1a0ae414650ff70f2cd28f90e8433a
|
data/README.md
CHANGED
@@ -31,11 +31,24 @@ end
|
|
31
31
|
producer = KafkaRestClient::AvroProducer.new
|
32
32
|
|
33
33
|
# Produce a single event using the topic name and payload
|
34
|
+
# The schema will be fetched from the schema registry based on the topic name
|
34
35
|
producer.produce('ice-cream-melted', { temperature: 35, unit: 'celsius' })
|
35
36
|
|
36
37
|
# Produce multiple events
|
38
|
+
# The schema will also be fetched from the schema registry based on the topic name
|
37
39
|
producer.produce('ice-cream-melted', [{ temperature: 35, unit: 'celsius' }])
|
38
40
|
|
41
|
+
# Produce an event providing the schema id
|
42
|
+
producer.produce('ice-cream-melted', { temperature: 35, unit: 'celsius' }, value_schema_id: 1)
|
43
|
+
|
44
|
+
# Produce an event providing the full schema
|
45
|
+
schema = {
|
46
|
+
type: 'record',
|
47
|
+
name: 'IceCreamMelted',
|
48
|
+
fields: [{ name: 'Temperature', type: 'string' }, { name: 'Unit', type: 'string' }]
|
49
|
+
}
|
50
|
+
producer.produce('ice-cream-melted', { temperature: 35, unit: 'celsius' }, value_schema: schema.to_json)
|
51
|
+
|
39
52
|
# Produce event objects by relying on #to_json and #as_json when using Rails ActiveSupport
|
40
53
|
class IceCreamMeltedEvent
|
41
54
|
attr_reader :temperature, :unit
|
@@ -58,8 +71,6 @@ rescue KafkaRestClient::SchemaNotFoundError => e
|
|
58
71
|
# Schema has not been registered in the schema registry
|
59
72
|
rescue KafkaRestClient::TopicNotFoundError => e
|
60
73
|
# Topic does not exist in Kafka, create it with confluent tools
|
61
|
-
rescue KafkaRestClient::Error => e
|
62
|
-
# Rescue any error raised by the library
|
63
74
|
end
|
64
75
|
```
|
65
76
|
|
data/kafka_rest_client.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'kafka_rest_client'
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '0.2.0'
|
8
8
|
spec.authors = ['Funding Circle Engineering']
|
9
9
|
spec.email = ['engineering+kafka_rest_client@fundingcircle.com']
|
10
10
|
|
@@ -22,9 +22,9 @@ module KafkaRestClient
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
def produce(topic, events)
|
25
|
+
def produce(topic, events, options = {})
|
26
26
|
if enabled?
|
27
|
-
payload = build_event_payload(topic, Array(events))
|
27
|
+
payload = build_event_payload(topic, Array(events), options)
|
28
28
|
response = post_event_to_kafka(topic, payload)
|
29
29
|
config.logger.debug("Produced to Kafka topic #{topic}: #{payload.inspect}")
|
30
30
|
response
|
@@ -33,11 +33,16 @@ module KafkaRestClient
|
|
33
33
|
|
34
34
|
private
|
35
35
|
|
36
|
-
def build_event_payload(topic, events)
|
37
|
-
{
|
38
|
-
value_schema_id:
|
39
|
-
|
40
|
-
|
36
|
+
def build_event_payload(topic, events, options)
|
37
|
+
payload = { records: events.map { |event| { value: event } } }.merge(
|
38
|
+
options.select { |k, _| [:value_schema_id, :value_schema].include?(k) }
|
39
|
+
)
|
40
|
+
|
41
|
+
if !options[:value_schema_id] && !options[:value_schema]
|
42
|
+
payload[:value_schema_id] = get_latest_schema(topic).fetch('id')
|
43
|
+
end
|
44
|
+
|
45
|
+
payload
|
41
46
|
end
|
42
47
|
|
43
48
|
def get_latest_schema(topic)
|
@@ -55,7 +60,7 @@ module KafkaRestClient
|
|
55
60
|
elsif response.code == 503
|
56
61
|
fail BadGateway, "Bad gateway: #{response.body}"
|
57
62
|
else
|
58
|
-
fail "
|
63
|
+
fail Error, "HTTP status: #{response.code} #{response.body}"
|
59
64
|
end
|
60
65
|
end
|
61
66
|
|
@@ -71,7 +76,7 @@ module KafkaRestClient
|
|
71
76
|
config.logger.debug("#{message[:records].count} event(s) published on #{topic}")
|
72
77
|
JSON.parse(response.body)
|
73
78
|
elsif response.code == 404
|
74
|
-
fail
|
79
|
+
fail TopicNotFoundError,
|
75
80
|
"Topic #{topic} not found in schema registry"
|
76
81
|
elsif response.code == 422
|
77
82
|
fail SchemaValidationError,
|
@@ -82,7 +87,7 @@ module KafkaRestClient
|
|
82
87
|
elsif response.code == 503
|
83
88
|
fail BadGateway, "Bad gateway: #{response.body}"
|
84
89
|
else
|
85
|
-
fail "
|
90
|
+
fail Error, "HTTP status: #{response.code} #{response.body}"
|
86
91
|
end
|
87
92
|
end
|
88
93
|
end
|
@@ -5,6 +5,9 @@ module KafkaRestClient
|
|
5
5
|
# Exception raised when the desired schema is not found (404 response code)
|
6
6
|
class SchemaNotFoundError < Error; end
|
7
7
|
|
8
|
+
# Exception raised when the desired topic is not found (404 response code)
|
9
|
+
class TopicNotFoundError < Error; end
|
10
|
+
|
8
11
|
# Exception raised on an internal server error (500 response code)
|
9
12
|
class InternalServerError < Error; end
|
10
13
|
|