avro2kafka 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 442cbcdebce5cdbf488aff50c61ab23dafbece43
4
+ data.tar.gz: 46a629b705531af0ada32f54987af81814772fd8
5
+ SHA512:
6
+ metadata.gz: 6e9ec180f4fbe39e175fce8caa2dd97180f99faf35dfce6a50bde5b4fd4775380978b937602c57d6c3cb3cfe68fb301568de19a3c543eecb9d4d7b11b169531c
7
+ data.tar.gz: b4ee9b33830dbfbd22b9ee8b7064263c9df79d8b149968353e69c8a70b7035086ca19661d447a5dbd5d161b3fedd7c026af88e0454b838b9611cbef822680a68
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+ This project adheres to [Semantic Versioning](http://semver.org/).
5
+
6
+ ## [Unreleased]
7
+
8
+ ### Added
9
+
10
+ ### Changed
11
+
12
+ ## 0.1.0 (2015-06-09)
13
+ Initial release
14
+
15
+ ### Added
16
+ * CLI (`avro2kafa`) to Avro files to Kafka (#1)
17
+ * Gem packaging
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in avro2kafka.gemspec
4
+ gemspec
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ avro2kafka (0.1.0)
5
+ avro (~> 1.7)
6
+ poseidon (~> 0.0.5)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ avro (1.7.7)
12
+ multi_json
13
+ bump (0.5.2)
14
+ coderay (1.1.0)
15
+ diff-lcs (1.2.5)
16
+ method_source (0.8.2)
17
+ multi_json (1.11.0)
18
+ poseidon (0.0.5)
19
+ pry (0.10.1)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.8.1)
22
+ slop (~> 3.4)
23
+ rake (10.4.2)
24
+ rspec (3.2.0)
25
+ rspec-core (~> 3.2.0)
26
+ rspec-expectations (~> 3.2.0)
27
+ rspec-mocks (~> 3.2.0)
28
+ rspec-core (3.2.3)
29
+ rspec-support (~> 3.2.0)
30
+ rspec-expectations (3.2.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.2.0)
33
+ rspec-mocks (3.2.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.2.0)
36
+ rspec-support (3.2.2)
37
+ slop (3.6.0)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ avro2kafka!
44
+ bump (~> 0.5)
45
+ bundler (~> 1.6)
46
+ pry (~> 0.10)
47
+ rake (~> 10.0)
48
+ rspec (~> 3.2)
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Secret Sauce Partners, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Avro2Kafka
2
+
3
+ Publish data from Avro files to Kafka in JSON format
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'avro2kafka'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install avro2kafka
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ Usage: avro2kafka [options] [file]
25
+ ```
26
+
27
+ ### Options
28
+
29
+ `-b, --broker BROKER`
30
+ The Kafka broker, eg. localhost:9092. This value is required.
31
+
32
+ `-t, --topic TOPIC`
33
+ The Kafka topic to publish to. This value is required.
34
+
35
+ `-k, --key KEY`
36
+ The fields in the avro that will act as keys for Kafka. If not supplied, unique and undefined keys will be generated, hence log compaction won't work.
37
+
38
+ `-h, --help`
39
+ Prints help
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/avro2kafka/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
@@ -0,0 +1,18 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+ require 'bump/tasks'
4
+
5
+ # Remove pre and set rake tasks
6
+ Rake.application.instance_eval do
7
+ %w[bump:pre bump:set].each do |task|
8
+ @tasks.delete(task)
9
+ end
10
+ end
11
+
12
+ # Default directory to look in is `/spec`
13
+ # Run with `rake spec`
14
+ RSpec::Core::RakeTask.new(:spec) do |task|
15
+ task.rspec_opts = ['--color', '--format', 'documentation']
16
+ end
17
+
18
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'avro2kafka/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "avro2kafka"
8
+ spec.version = Avro2Kafka::VERSION
9
+ spec.authors = ["Peter Marton"]
10
+ spec.email = ["martonpe@secretsaucepartners.com"]
11
+ spec.summary = %q{Publish Avro files to Kafka}
12
+ spec.description = %q{Publish Avro files to Kafka in JSON format}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "pry", "~> 0.10"
25
+ spec.add_development_dependency "bump", "~> 0.5"
26
+
27
+ spec.add_dependency "avro", "~> 1.7"
28
+ spec.add_dependency "poseidon", "~> 0.0.5"
29
+ end
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib' if $0 == __FILE__
4
+ require 'optparse'
5
+ require 'avro2kafka'
6
+
7
+ options = {}
8
+
9
+ option_parser = OptionParser.new do |opts|
10
+ opts.banner = "Version #{Avro2Kafka::VERSION} of Avro2Kafka\n" \
11
+ "Usage: #{File.basename(__FILE__)} [options] [file]"
12
+
13
+ opts.on('-b', '--broker BROKER', 'The Kafka broker, eg. localhost:9092. This value is required.') do |broker|
14
+ options[:broker] = broker
15
+ end
16
+
17
+ opts.on('-t', '--topic TOPIC', 'The Kafka topic to publish to. This value is required.') do |topic|
18
+ options[:topic] = topic
19
+ end
20
+
21
+ opts.on('-k', '--key KEY', 'The fields in the avro that will act as keys for Kafka.') do |key|
22
+ options[:key] = key
23
+ end
24
+
25
+ opts.on('-h', '--help', 'Prints help') do
26
+ puts opts
27
+ exit
28
+ end
29
+ end
30
+
31
+ option_parser.parse!
32
+
33
+ begin
34
+ raise OptionParser::MissingArgument.new('--broker') if options[:broker].nil?
35
+ raise OptionParser::MissingArgument.new('--topic') if options[:topic].nil?
36
+
37
+ Avro2Kafka.new(options).publish
38
+ rescue OptionParser::MissingArgument => ex
39
+ puts ex.message
40
+
41
+ puts option_parser
42
+ rescue Exception => e
43
+ puts 'Uh oh, something went wrong!'
44
+ puts e.message
45
+ puts e.backtrace.join("\n")
46
+ end
@@ -0,0 +1,28 @@
1
+ require 'avro2kafka/version'
2
+ require 'avro2kafka/avro_reader'
3
+ require 'avro2kafka/kafka_publisher'
4
+
5
+ class Avro2Kafka
6
+ attr_reader :input_path, :kafka_broker, :kafka_topic, :kafka_key
7
+
8
+ def initialize(options)
9
+ @input_path = ARGV.first
10
+ @kafka_broker = options.delete(:broker)
11
+ @kafka_topic = options.delete(:topic)
12
+ @kafka_key = options[:key]
13
+
14
+ @options = options
15
+ end
16
+
17
+ def reader
18
+ ARGF.lineno = 0
19
+ ARGF
20
+ end
21
+
22
+ def publish
23
+ records = AvroReader.new(reader).read
24
+ KafkaPublisher.new(kafka_broker, kafka_topic, kafka_key).publish(records)
25
+ puts "Avro file published to #{kafka_topic} topic on #{kafka_broker}!"
26
+ end
27
+
28
+ end
@@ -0,0 +1,16 @@
1
+ require 'avro'
2
+
3
+ class Avro2Kafka
4
+ class AvroReader
5
+ attr_reader :io
6
+
7
+ def initialize(io)
8
+ @io = io
9
+ @reader = Avro::IO::DatumReader.new()
10
+ end
11
+
12
+ def read
13
+ Avro::DataFile::Reader.new(io, @reader).to_enum
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ require 'poseidon'
2
+ require 'json'
3
+
4
+ class Avro2Kafka
5
+ class KafkaPublisher
6
+ attr_reader :producer, :topic, :keys
7
+
8
+ def initialize(broker, topic, keys)
9
+ @producer = Poseidon::Producer.new([broker], "avro2kafka")
10
+ @topic = topic
11
+ @keys = keys.split(',').map(&:strip) if keys
12
+ end
13
+
14
+ def publish(records)
15
+ records.each_slice(100) do |batch|
16
+ messages = batch.map do |record|
17
+ if keys
18
+ message_key = keys.map { |key| record[key] }.join
19
+ Poseidon::MessageToSend.new(topic, record.to_json, message_key)
20
+ else
21
+ Poseidon::MessageToSend.new(topic, record.to_json)
22
+ end
23
+ end
24
+ producer.send_messages(messages)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ class Avro2Kafka
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'avro'
3
+
4
+ RSpec.describe Avro2Kafka::AvroReader do
5
+ describe '#read' do
6
+ let(:path) { './spec/support/data.avro' }
7
+
8
+ before do
9
+ @io = File.open(path, 'r')
10
+ @avro = Avro2Kafka::AvroReader.new(@io).read
11
+ end
12
+
13
+ after do
14
+ @io.close
15
+ end
16
+
17
+ it 'should return an Enumerator object' do
18
+ expect(@avro).to be_an_instance_of Enumerator
19
+ end
20
+
21
+ it 'should return 3 rows' do
22
+ expect(@avro.count).to eq 3
23
+ end
24
+
25
+ it 'should return the correct data' do
26
+ expect(@avro.map { |record| record }).to eq(
27
+ [
28
+ { 'id'=> 1, 'name'=> 'dresses', 'description'=> 'Dresses' },
29
+ { 'id'=> 2, 'name'=> 'female-tops', 'description'=> 'Female Tops' },
30
+ { 'id'=> 3, 'name'=> 'bras', 'description'=> 'Bras' }
31
+ ]
32
+ )
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'poseidon'
3
+
4
+ RSpec.describe Avro2Kafka::KafkaPublisher do
5
+ describe '#publish' do
6
+ let(:broker) { 'localhost:9092' }
7
+ let(:topic) { 'feeds' }
8
+ let(:key) { 'name, id' }
9
+ let(:messages) { %w[message1, message2, message3] }
10
+
11
+ it 'should call publisher#send_messages' do
12
+ kafka_publisher = Avro2Kafka::KafkaPublisher.new(broker, topic, key)
13
+ mock_producer = double("Producer")
14
+ allow(kafka_publisher).to receive(:producer).and_return(mock_producer)
15
+ expect(mock_producer).to receive(:send_messages).with(all(be_kind_of(Poseidon::MessageToSend)))
16
+ kafka_publisher.publish(messages)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'poseidon'
3
+ require 'json'
4
+
5
+ RSpec.describe Avro2Kafka do
6
+ describe '#publish' do
7
+ let(:options) do
8
+ {
9
+ broker: 'localhost:9092',
10
+ topic: 'feeds',
11
+ key: 'name, id'
12
+ }
13
+ end
14
+
15
+ before do
16
+ ARGV.replace ['./spec/support/data.avro']
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ require 'avro2kafka'
2
+
3
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each {|f| require f }
Binary file
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avro2kafka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Marton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bump
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: avro
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.7'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: poseidon
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.0.5
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.0.5
111
+ description: Publish Avro files to Kafka in JSON format
112
+ email:
113
+ - martonpe@secretsaucepartners.com
114
+ executables:
115
+ - avro2kafka
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - CHANGELOG.md
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - avro2kafka.gemspec
126
+ - bin/avro2kafka
127
+ - lib/avro2kafka.rb
128
+ - lib/avro2kafka/avro_reader.rb
129
+ - lib/avro2kafka/kafka_publisher.rb
130
+ - lib/avro2kafka/version.rb
131
+ - spec/avro2kafka/avro_reader_spec.rb
132
+ - spec/avro2kafka/kafka_publisher_spec.rb
133
+ - spec/avro2kafka_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/data.avro
136
+ homepage: ''
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.2.2
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Publish Avro files to Kafka
160
+ test_files:
161
+ - spec/avro2kafka/avro_reader_spec.rb
162
+ - spec/avro2kafka/kafka_publisher_spec.rb
163
+ - spec/avro2kafka_spec.rb
164
+ - spec/spec_helper.rb
165
+ - spec/support/data.avro
166
+ has_rdoc: