| 1 | package Net::Kafka::Producer::Avro; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | Net::Kafka::Producer::Avro - Apache Kafka message producer based on librdkafka, Avro serialization and Confluent Schema Registry validation. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | use Net::Kafka::Producer::Avro; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | use Confluent::SchemaRegistry; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | use AnyEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | use JSON; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | my $producer = Net::Kafka::Producer::Avro->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 'bootstrap.servers' => 'localhost:9092', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 'schema-registry' => Confluent::SchemaRegistry->new(), # defaults to http://localhost:8081 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 'compression.codec' => 'gzip', # optional, one of: 'none' (def.), 'gzip', 'snappy', 'lz4', 'zstd' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 'log_level' => 0, # suppress librdkafka internal logging | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 'error_cb' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | my ($self, $err, $msg) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | die "Connection error:\n\t- err: " . $err . "\n\t- msg: " . $msg . "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # creates the header object (if you need to add headers to the message) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | my $headers = Net::Kafka::Headers->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | $headers->add('my-header-1', 'foo'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | $headers->add('my-header-2', 'bar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | my $condvar = AnyEvent->condvar; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $promise = $producer->produce( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | topic => 'mytopic', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | partition => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | key => 1000, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | key_schema => to_json( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | name => 'id', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | type => 'long' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | payload => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | id => 1210120, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | f1 => 'text message' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | payload_schema => to_json( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | type => 'record', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | name => 'myrecord', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | fields => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | name => 'id', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | type => 'long' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | name => 'f1', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | type => 'string' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | headers => $headers | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | die "Error requesting message production: " . $producer->get_error() . "\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | unless $promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $promise->then( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | my $delivery_report = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | $condvar->send; # resolve the promise | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | print "Message delivered with offset " . $delivery_report->{offset}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | my $error = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | $condvar->send; # resolve the promise | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | die "Unable to produce message: " . $error->{error} . ", code: " . $error->{code}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | $condvar->recv; # wait for the promise resolution | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | print "Message produced", "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | C<Net::Kafka::Producer::Avro> main goal is to provide object-oriented API to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | produce Avro-serialized messages according to I<Confluent SchemaRegistry>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | C<Net::Kafka::Producer::Avro> inerhits from and extends L<Net::Kafka::Producer|Net::Kafka::Producer> module. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | use base 'Net::Kafka::Producer'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | use Memoize; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | use Avro::BinaryEncoder; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | use Avro::Schema; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | use Confluent::SchemaRegistry; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | use constant MAGIC_BYTE => 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | use version; our $VERSION = version->declare('v0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 INSTALL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | Installation of C<Net::Kafka::Producer::Avro> is a canonical: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | perl Makefile.PL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | make | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | make test | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | make install | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | =head2 TESTING TROUBLESHOOTING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | Tests are focused on verifying Avro-formatted messages and theirs interactions with Confluent Schema Registry and are intended to extend the C<Net::Kafka::Producer>'s test suite. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | It's expected that a local Apache Kafka and Schema Registry services are listening on C<localhost:9092> and C<http://localhost:8081>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | You can either set different endpoints by exporting the following environment variables: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =over 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =item C<KAFKA_HOST> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =item C<KAFKA_PORT> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =item C<CONFLUENT_SCHEMA_REGISTY_URL> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | For example: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | export KAFKA_HOST=my-kafka-host.my-domain.org | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | export KAFKA_PORT=9092 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | export CONFLUENT_SCHEMA_REGISTY_URL=http://my-schema-registry-host.my-domain.org | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | =head2 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | =head3 C<new> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | Creates a message producer. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | C<new()> method expects the same arguments set as the L<Net::Kafka::Producer|Net::Kafka::Producer> parent constructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | In addition, takes in the following mandatory argument: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | =over 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | =item C<SchemaRegistry =E<gt> $schema_registry> (B<mandatory>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | Is a L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $this = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table0%0/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $schema_registry_class = 'Confluent::SchemaRegistry'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # Check SchemaRegistry param | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | die "Missing 'schema-registry' param" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | unless exists $params{'schema-registry'}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | die "'schema-registry' param must be a $schema_registry_class instance object" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | unless ref($params{'schema-registry'}) eq $schema_registry_class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my $schema_registry = delete $params{'schema-registry'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # Use parent class constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | my $self = $class->SUPER::new(%params); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # Add ans internal reference to SchemaRegistry | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $self->{__SCHEMA_REGISTRY} = $schema_registry; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | return bless($self, $class); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ##### Class methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # Encode $payload in Avro format according to an Avro schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | my $schema_ref = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my $payload = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | unless defined $payload; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my $encoded = pack('bN', &MAGIC_BYTE, $schema_ref->{id}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | Avro::BinaryEncoder->encode( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | schema => $schema_ref->{schema}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | data => $payload, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | emit_cb => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $encoded .= ${ $_[0] }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | return $encoded; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | ##### Private methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | sub _clear_error { $_[0]->_set_error() } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clear_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub _set_error { $_[0]->{__ERROR} = $_[1] } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub _get_error { $_[0]->{__ERROR} } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # Interact with Schema Registry | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | memoize('_get_avro_schema'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub _get_avro_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_avro_schema: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | my $topic = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | my $supplied_schema = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | my $subject = $topic . '-' . $type; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | my $sr = $self->schema_registry(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | my ($schema_id, $avro_schema); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # If a schema is supplied... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if ($supplied_schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # If the subject exixts... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $subjects = $sr->get_subjects(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if (grep(/^$subject$/, @$subjects) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # ...check if it already exists in registry | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $schema_info = $sr->check_schema( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | SUBJECT => $topic, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | TYPE => $type, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | SCHEMA => $supplied_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if ( defined $schema_info ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | $schema_id = $schema_info->{id}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | $avro_schema = $schema_info->{schema}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # ...if it does not already exist in the registry.... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # ...test new schema compliancy against latest version | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $compliant = $sr->test_schema( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | SUBJECT => $topic, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | TYPE => $type, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | SCHEMA => $supplied_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | $self->_set_error("$type schema not compliant with latest one from registry for topic '$topic'") && | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$compliant || $self->_set_error("$type schema not compliant with latest one from registry for topic '${topic}'") && (return undef)
$self->_set_error("$type schema not compliant with latest one from registry for topic '${topic}'") and return undef
MC/DC0%0/2
$compliant || $self->_set_error("$type schema not compliant with latest one from registry for topic '${topic}'") && (return undef)
$compliant
$self->_set_error("$type schema not compliant with latest one from registry for topic '${topic}'")
Truth table0%0/3
A || B
A= $compliantB= $self->_set_error("$type schema not compliant with latest one from registry for topic '${topic}'")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | unless $compliant; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # ...if a previous id for the schema is not available, try to add the one supplied to the registry.... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | unless ($schema_id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # ...procede adding it to the registry | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | $schema_id = $sr->add_schema( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | SUBJECT => $topic, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | TYPE => $type, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | SCHEMA => $supplied_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | $self->_set_error('Error adding schema to registry: ' . encode_json($sr->get_error())) && | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$schema_id || $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error)) && (return undef)
$self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error)) and return undef
MC/DC0%0/2
$schema_id || $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error)) && (return undef)
$schema_id
$self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error))
Truth table0%0/3
A || B
A= $schema_idB= $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error))
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | unless $schema_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | # ...and bless new schema into an Avro schema object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $avro_schema = Avro::Schema->parse($supplied_schema); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # retreive latest schema for the topic value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | my $schema_info = $sr->get_schema( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | SUBJECT => $topic, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | TYPE => $type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | if ( defined $schema_info ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $schema_id = $schema_info->{id}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $avro_schema = $schema_info->{schema}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | $self->_set_error("No schema in registry for subject " . $topic . '-' . 'value') && | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$schema_info || $self->_set_error('No schema in registry for subject ' . $topic . '-' . 'value') && (return undef)
$self->_set_error('No schema in registry for subject ' . $topic . '-' . "value") and return undef
MC/DC0%0/2
$schema_info || $self->_set_error('No schema in registry for subject ' . $topic . '-' . 'value') && (return undef)
$schema_info
$self->_set_error('No schema in registry for subject ' . $topic . '-' . "value")
Truth table0%0/3
A || B
A= $schema_infoB= $self->_set_error('No schema in registry for subject ' . $topic . '-' . "value")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | unless $schema_info; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | return ($schema_id, $avro_schema); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | ##### Public methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | =head2 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | The following methods are defined for the C<Net::Kafka::Producer::Avro> class: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | =head3 C<schema_registry>() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | Returns the L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance supplied to the construcor. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | sub schema_registry { $_[0]->{__SCHEMA_REGISTRY} } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schema_registry: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | =head3 C<get_error>() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | Returns a string containing last error message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub get_error { $_[0]->_get_error() } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | =head3 C<produce( %named_params )> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | Sends Avro-formatted key/message pairs. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | According to C<Net::Kafka::Producer>, returns a promise value if the message was successfully sent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | In order to handle Avro format, the C<Net::Kafka::Producer|Net::Kafka::Producer>'s C<produce()> method has been | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | extended with two more arguments, C<key_schema> and C<payload_schema>: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | $producer->produce( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | topic => $topic, # scalar | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | partition => $partition, # scalar | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | key_schema => $key_schema, # (optional) scalar representing a JSON string of the Avro schema to use for the key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | key => $key, # (optional) scalar | hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | payload_schema => $payload_schema, # (optional) scalar representing a JSON string of the Avro schema to use for the payload | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | payload => $payload, # scalar | hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | timestamp => $timestamp, # (optional) scalar representing milliseconds since epoch | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | headers => $headers, # (optional) Net::Kafka::Headers object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | # ...other params accepted by Net::Kafka::Producer's produce() method | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | Both C<$key_schema> and C<$payload_schema> parameters are optional and must provide a JSON strings | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | representing the Avro schemas to use for validating and serializing key and payload. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | These schemas will be validated against the C<$schema_registry> supplied to the C<new> method and, if compliant, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | will be added to the registry under the C<$topic+'key'> or C<$topic+'value'> Schema Registry subjects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | If a schema isn't provided, the latest version from Schema Registry will be used accordingly to the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | (topic + key/value) subject. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | ▶ | sub produce { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
produce: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | my $avro_schemas = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | key => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | id => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | schema => undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | value => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | id => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | schema => undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | my ($encoded_key, $encoded_payload); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | $self->_clear_error(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | $self->_set_error('Missing topic param') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | unless defined $params{topic}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $params{'topic'} || $self->_set_error('Missing topic param') && (return undef)
$self->_set_error("Missing topic param") and return undef
MC/DC0%0/2
defined $params{'topic'} || $self->_set_error('Missing topic param') && (return undef)
defined $params{'topic'}
$self->_set_error("Missing topic param")
Truth table0%0/3
A || B
A= defined $params{'topic'}B= $self->_set_error("Missing topic param")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | $self->_set_error('Missing partition param') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | unless defined $params{partition}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $params{'partition'} || $self->_set_error('Missing partition param') && (return undef)
$self->_set_error("Missing partition param") and return undef
MC/DC0%0/2
defined $params{'partition'} || $self->_set_error('Missing partition param') && (return undef)
defined $params{'partition'}
$self->_set_error("Missing partition param")
Truth table0%0/3
A || B
A= defined $params{'partition'}B= $self->_set_error("Missing partition param")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | $self->_set_error('Missing payload') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | ▶ | unless defined $params{payload}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $params{'payload'} || $self->_set_error('Missing payload') && (return undef)
$self->_set_error("Missing payload") and return undef
MC/DC0%0/2
defined $params{'payload'} || $self->_set_error('Missing payload') && (return undef)
defined $params{'payload'}
$self->_set_error("Missing payload")
Truth table0%0/3
A || B
A= defined $params{'payload'}B= $self->_set_error("Missing payload")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | # if key is provided, encode it in Avro format according to the suggested schema or to the retrieved schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | ▶ | if ($params{key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | # Retrieve target Avro schema for the key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ($avro_schemas->{key}->{id}, $avro_schemas->{key}->{schema}) = $self->_get_avro_schema($params{topic}, 'key', $params{key_schema}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ▶ | if (defined $avro_schemas->{key}->{id} && defined $avro_schemas->{key}->{schema}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $avro_schemas->{"key"}{"id"} and defined $avro_schemas->{"key"}{"schema"}
MC/DC0%0/2
defined $avro_schemas->{"key"}{"id"} and defined $avro_schemas->{"key"}{"schema"}
defined $avro_schemas->{"key"}{"id"}
defined $avro_schemas->{"key"}{"schema"}
Truth table0%0/3
A and B
A= defined $avro_schemas->{"key"}{"id"}B= defined $avro_schemas->{"key"}{"schema"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # encode key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | $encoded_key = _encode($avro_schemas->{key}, $params{key}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | } catch {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | $self->_set_error('Key not compliant with supplied schema') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $encoded_key || $self->_set_error('Key not compliant with supplied schema') && (return undef)
$self->_set_error("Key not compliant with supplied schema") and return undef
MC/DC0%0/2
defined $encoded_key || $self->_set_error('Key not compliant with supplied schema') && (return undef)
defined $encoded_key
$self->_set_error("Key not compliant with supplied schema")
Truth table0%0/3
A || B
A= defined $encoded_keyB= $self->_set_error("Key not compliant with supplied schema")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | unless defined $encoded_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # do not encode key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | $encoded_key = $params{key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | $self->_set_error('Key schema not acceptable while key in not provided') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | unless $params{key_schema}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$params{'key_schema'} || $self->_set_error('Key schema not acceptable while key in not provided') && (return undef)
$self->_set_error("Key schema not acceptable while key in not provided") and return undef
MC/DC0%0/2
$params{'key_schema'} || $self->_set_error('Key schema not acceptable while key in not provided') && (return undef)
$params{'key_schema'}
$self->_set_error("Key schema not acceptable while key in not provided")
Truth table0%0/3
A || B
A= $params{'key_schema'}B= $self->_set_error("Key schema not acceptable while key in not provided")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # Retrieve target Avro schema for the key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ($avro_schemas->{value}->{id}, $avro_schemas->{value}->{schema}) = $self->_get_avro_schema($params{topic}, 'value', $params{payload_schema}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # Return if value Avro schema was not found | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | $self->_set_error('No value schema found') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | unless defined $avro_schemas->{value}->{id} && defined $avro_schemas->{value}->{schema}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/8
defined $avro_schemas->{'value'}{'id'} && defined $avro_schemas->{'value'}{'schema'} || $self->_set_error('No value schema found') && (return undef)
defined $avro_schemas->{"value"}{"id"} and defined $avro_schemas->{"value"}{"schema"}
$self->_set_error("No value schema found") and return undef
MC/DC0%0/3
defined $avro_schemas->{'value'}{'id'} && defined $avro_schemas->{'value'}{'schema'} || $self->_set_error('No value schema found') && (return undef)
defined $avro_schemas->{"value"}{"id"}
defined $avro_schemas->{"value"}{"schema"}
$self->_set_error("No value schema found")
Truth table0%0/5
A and B || C
A= defined $avro_schemas->{"value"}{"id"}B= defined $avro_schemas->{"value"}{"schema"}C= $self->_set_error("No value schema found")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | # Avro encoding of message | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | $encoded_payload = _encode($avro_schemas->{value}, $params{payload}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | } catch {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | $self->_set_error('Payload not compliant with supplied schema') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $encoded_payload || $self->_set_error('Payload not compliant with supplied schema') && (return undef)
$self->_set_error("Payload not compliant with supplied schema") and return undef
MC/DC0%0/2
defined $encoded_payload || $self->_set_error('Payload not compliant with supplied schema') && (return undef)
defined $encoded_payload
$self->_set_error("Payload not compliant with supplied schema")
Truth table0%0/3
A || B
A= defined $encoded_payloadB= $self->_set_error("Payload not compliant with supplied schema")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | unless defined $encoded_payload; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # Overwrite key and payload with encoded values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | $params{key} = $encoded_key if $encoded_key; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | $params{payload} = $encoded_payload if $encoded_payload; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # Remove params unknown by the parent class method | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | delete $params{key_schema}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | delete $params{payload_schema}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | # Send message through Net::Kafka::Producer parent class | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | return $self->SUPER::produce(%params); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | Alvaro Livraghi, E<lt>alvarol@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =head1 CONTRIBUTE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | L<https://github.com/alivraghi/Net-Kafka-Producer-Avro> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | Please use GitHub project link above to report problems or contact authors. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | Copyright 2026 by Alvaro Livraghi | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||