| 1 | package Kafka::Consumer::Avro; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Kafka::Consumer::Avro - Avro message consumer for Apache Kafka. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Kafka::Connection; | ||
| 12 | use Kafka::Consumer::Avro; | ||
| 13 | |||
| 14 | my $connection = Kafka::Connection->new( host => 'localhost' ); | ||
| 15 | |||
| 16 | my $consumer = Kafka::Consumer::Avro->new( Connection => $connection ); | ||
| 17 | |||
| 18 | # Do some interactions with Avro & SchemaRegistry before sending messages | ||
| 19 | |||
| 20 | # Fetch messages | ||
| 21 | my $response = $consumer->fetch(...); | ||
| 22 | |||
| 23 | # Closes the consumer and cleans up | ||
| 24 | undef $consumer; | ||
| 25 | $connection->close; | ||
| 26 | undef $connection; | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | C<Kafka::Consumer::Avro> main feature is to provide object-oriented API to | ||
| 31 | consume messages according to I<Confluent SchemaRegistry> and I<Avro> serialization. | ||
| 32 | |||
| 33 | C<Kafka::Consumer::Avro> inerhits from and extends L<Kafka::Consumer|Kafka::Consumer>. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | use 5.010; | ||
| 38 | use strict; | ||
| 39 | use warnings; | ||
| 40 | |||
| 41 | use JSON::XS; | ||
| 42 | use IO::String; | ||
| 43 | |||
| 44 | use base 'Kafka::Consumer'; | ||
| 45 | |||
| 46 | use Avro::BinaryDecoder; | ||
| 47 | use Avro::Schema; | ||
| 48 | use Confluent::SchemaRegistry; | ||
| 49 | |||
| 50 | use constant MAGIC_BYTE => 0; | ||
| 51 | |||
| 52 | our $VERSION = '0.01'; | ||
| 53 | |||
| 54 | |||
| 55 | =head2 CONSTRUCTOR | ||
| 56 | |||
| 57 | =head3 C<new> | ||
| 58 | |||
| 59 | Creates new consumer client object. | ||
| 60 | |||
| 61 | C<new()> takes arguments in key-value pairs as described in L<Kafka::Consumer|Kafka::Consumer> from which it inherits. | ||
| 62 | |||
| 63 | In addition, takes in the following arguments: | ||
| 64 | |||
| 65 | =over 3 | ||
| 66 | |||
| 67 | =item C<SchemaRegistry =E<gt> $schema_registry> (B<mandatory>) | ||
| 68 | |||
| 69 | Is a L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance. | ||
| 70 | |||
| 71 | =back | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub new { | ||
| 76 | my $this = shift; | ||
| 77 | my $class = ref($this) || $this; | ||
| 78 | my $schema_registry_class = 'Confluent::SchemaRegistry'; | ||
| 79 | my %params = @_; | ||
| 80 | |||
| 81 | # Check SchemaRegistry param | ||
| 82 | die "Missing SchemaRegistry param" | ||
| 83 | unless exists $params{SchemaRegistry}; | ||
| 84 | die "SchemaRegistry param must be a $schema_registry_class instance object" | ||
| 85 | unless ref($params{SchemaRegistry}) eq $schema_registry_class; | ||
| 86 | my $schema_registry = delete $params{SchemaRegistry}; | ||
| 87 | |||
| 88 | # Use parent class constructor | ||
| 89 | my $self = $class->SUPER::new(%params); | ||
| 90 | |||
| 91 | # Add ans internal reference to SchemaRegistry | ||
| 92 | $self->{__SCHEMA_REGISTRY} = $schema_registry; | ||
| 93 | |||
| 94 | return bless($self, $class); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | ##### Class methods | ||
| 100 | our $schemas = []; | ||
| 101 | |||
| 102 | # Decode $payload from Avro format according to an Avro schema | ||
| 103 | sub _decode { | ||
| 104 | my $message = shift; | ||
| 105 | die "Unknown message format" | ||
| 106 | unless ref($message) eq 'Kafka::Message'; | ||
| 107 | my $sr = shift; | ||
| 108 | my $reader = IO::String->new($message->payload); | ||
| 109 | seek($reader, 1, 0); # Skip magic byte | ||
| 110 | my $buf = "\0\0\0\0"; | ||
| 111 | read($reader, $buf, 4); # Read schema version stored in avro message header | ||
| 112 | my $schema_id = unpack("N", $buf); # Retreive schema id from unsigned long (32 byte) | ||
| 113 | unless (defined $schemas->[$schema_id]) { | ||
| 114 | $schemas->[$schema_id] = $sr->get_schema_by_id(SCHEMA_ID => $schema_id) || die "Unavailable schema for id $schema_id"; | ||
| 115 | } | ||
| 116 | my $decoded = Avro::BinaryDecoder->decode( | ||
| 117 | writer_schema => $schemas->[$schema_id], | ||
| 118 | reader_schema => $schemas->[$schema_id], | ||
| 119 | reader => $reader | ||
| 120 | ); | ||
| 121 | return Kafka::Message->new( | ||
| 122 | { | ||
| 123 | payload => $decoded, | ||
| 124 | key => $message->key, | ||
| 125 | Timestamp => $message->Timestamp, | ||
| 126 | valid => $message->valid, | ||
| 127 | error => $message->error, | ||
| 128 | offset => $message->offset, | ||
| 129 | next_offset => $message->next_offset, | ||
| 130 | Attributes => $message->Attributes, | ||
| 131 | HighwaterMarkOffset => $message->HighwaterMarkOffset, | ||
| 132 | MagicByte => $message->MagicByte | ||
| 133 | } | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | ##### Private methods | ||
| 139 | |||
| 140 | sub _clear_error { $_[0]->_set_error() } | ||
| 141 | sub _set_error { $_[0]->{__ERROR} = $_[1] } | ||
| 142 | sub _get_error { $_[0]->{__ERROR} } | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | ##### Public methods | ||
| 148 | |||
| 149 | =head2 METHODS | ||
| 150 | |||
| 151 | The following methods are defined for the C<Kafka::Avro::Consumer> class: | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | |||
| 156 | =head3 C<schema_registry>() | ||
| 157 | |||
| 158 | Returns the L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance supplied to the construcor. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub schema_registry { $_[0]->{__SCHEMA_REGISTRY} } | ||
| 163 | |||
| 164 | |||
| 165 | =head3 C<get_error>() | ||
| 166 | |||
| 167 | Returns a string containing last error message. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub get_error { $_[0]->_get_error() } | ||
| 172 | |||
| 173 | |||
| 174 | =head3 C<fetch( %params )> | ||
| 175 | |||
| 176 | Gets messages froma a Kafka topic. | ||
| 177 | |||
| 178 | Please, see L<Kafka::Consumer|Kafka::Consumer-E<gt>fetch()> for more details. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | sub fetch { | ||
| 182 | my $self = shift; | ||
| 183 | my $messages = $self->SUPER::fetch(@_); | ||
| 184 | my $sr = $self->schema_registry(); | ||
| 185 | foreach my $message (@$messages) { | ||
| 186 | $message = _decode($message, $sr); | ||
| 187 | } | ||
| 188 | return $messages; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | =head1 AUTHOR | ||
| 194 | |||
| 195 | Alvaro Livraghi, E<lt>alvarol@cpan.orgE<gt> | ||
| 196 | |||
| 197 | =head1 CONTRIBUTE | ||
| 198 | |||
| 199 | L<https://github.com/alivraghi/Kafka-Consumer-Avro> | ||
| 200 | |||
| 201 | =head1 BUGS | ||
| 202 | |||
| 203 | Please use GitHub project link above to report problems or contact authors. | ||
| 204 | |||
| 205 | =head1 COPYRIGHT AND LICENSE | ||
| 206 | |||
| 207 | Copyright 2018 by Alvaro Livraghi | ||
| 208 | |||
| 209 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | 1; |