kril 0.1.5 → 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/.gitignore +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +10 -3
- data/bin/kril +16 -8
- data/lib/kril/producer.rb +9 -2
- data/lib/kril/schema_handler.rb +39 -11
- data/lib/kril/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc5623bc2f2039f53fbfe78a9fdf24b9aa14ec73
|
4
|
+
data.tar.gz: 05fad289a66fa8886113ec829d1336ba956bc1e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43ad965f8c1c59a0210f80216e127bccc68e631fca4dd2feaf438c3550817ce3d4b377bf3941c50560e3ef830c1f158378e62e4ee7283f0d9b2b16f6106287de
|
7
|
+
data.tar.gz: b4025a0d14a3e16fa3000908627b8120244f9664064477f366d0779f7c9318bc5a03a751a336985caca32855296dfe85effd54c77337134fdade1caf076e5d3a
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -73,7 +73,7 @@ $ kril --consume-all human
|
|
73
73
|
🦐 human: {:key=>nil, :value=>{"age"=>27}, :offset=>0, :create_time=>2018-03-04 00:13:13 -0700, :topic=>"human", :partition=>0}
|
74
74
|
```
|
75
75
|
---
|
76
|
-
The `--schema`
|
76
|
+
The `--schema` option is flexible:
|
77
77
|
```bash
|
78
78
|
$ kril --schema /path/to/schema.avsc
|
79
79
|
$ kril --schema name_of_existing_schema
|
@@ -87,7 +87,14 @@ $ kril -s human -r '{"age":99}'
|
|
87
87
|
```bash
|
88
88
|
🦐 human: {"age"=>99}
|
89
89
|
```
|
90
|
-
|
90
|
+
---
|
91
|
+
To see what schemas are saved in the repository:
|
92
|
+
```bash
|
93
|
+
$ kril --list-schemas
|
94
|
+
```
|
95
|
+
```bash
|
96
|
+
human, another_schema
|
97
|
+
```
|
91
98
|
## Contributing
|
92
99
|
|
93
100
|
1. Fork it ( https://github.com/ChadBowman/kril/fork )
|
@@ -96,7 +103,7 @@ $ kril -s human -r '{"age":99}'
|
|
96
103
|
4. Push to the branch (`git push origin my-new-feature`)
|
97
104
|
5. Create a new Pull Request
|
98
105
|
|
99
|
-
|
106
|
+
Obey 👮[Rubocop](https://github.com/bbatsov/rubocop)! 🚨
|
100
107
|
|
101
108
|
## License
|
102
109
|
|
data/bin/kril
CHANGED
@@ -10,7 +10,7 @@ require 'kril'
|
|
10
10
|
Clamp do
|
11
11
|
$log = Logger.new($STDOUT)
|
12
12
|
$log.level = Logger::ERROR
|
13
|
-
gem_root = File.expand_path('..', __dir__)
|
13
|
+
$gem_root = File.expand_path('..', __dir__)
|
14
14
|
|
15
15
|
option %w[--version], :flag, 'show version' do
|
16
16
|
puts Kril::VERSION
|
@@ -23,7 +23,7 @@ Clamp do
|
|
23
23
|
address_string.split(/,\s*/)
|
24
24
|
end
|
25
25
|
option %w[-g --schema-registry], '', 'address of schema registry', default: 'http://localhost:8081'
|
26
|
-
option %w[-p --schemas-path], '', 'directory of Avro schemas', default:
|
26
|
+
option %w[-p --schemas-path], '', 'directory of Avro schemas', default: File.join($gem_root, 'schemas').to_s do |path|
|
27
27
|
raise ArgumentError, "Schema path: #{path} is not a directory" unless File.directory?(path)
|
28
28
|
end
|
29
29
|
option %w[-v --verbose], :flag, 'print logs, warnings' do
|
@@ -36,11 +36,14 @@ Clamp do
|
|
36
36
|
option %w[-r --record], '', 'record to commit to topic'
|
37
37
|
option %w[-o --synchronous], :flag, 'commit records synchronously'
|
38
38
|
option %w[-s --schema], '', 'schema name, path to schema, or schema contents'
|
39
|
-
option %w[-j --extract-
|
39
|
+
option %w[-j --extract-schemas], '', 'extract schemas from Avro generated java files'
|
40
40
|
|
41
41
|
# consuming
|
42
42
|
option %w[-a --consume-all], :flag, 'consume every record on topic'
|
43
43
|
|
44
|
+
# utility
|
45
|
+
option %w[-l --list-schemas], :flag, 'list saved schemas'
|
46
|
+
|
44
47
|
parameter '[TOPIC]', 'topic to produce to or consume from'
|
45
48
|
|
46
49
|
def execute
|
@@ -52,7 +55,7 @@ Clamp do
|
|
52
55
|
avro = AvroTurf::Messaging.new(registry: registry,
|
53
56
|
schema_store: store,
|
54
57
|
logger: $log)
|
55
|
-
if
|
58
|
+
if extract_schemas
|
56
59
|
Kril::SchemaExtractor.extract(source_dir: extract_from_java_files,
|
57
60
|
output_dir: schemas_path)
|
58
61
|
end
|
@@ -73,17 +76,22 @@ Clamp do
|
|
73
76
|
rescue Kafka::ConnectionError => e
|
74
77
|
print_error(e.message)
|
75
78
|
end
|
79
|
+
if list_schemas?
|
80
|
+
schemas = File.join($gem_root, 'schemas', '**', '*.avsc')
|
81
|
+
puts Dir.glob(schemas).map { |file| File.basename(file, '.avsc') }.join(', ')
|
82
|
+
end
|
76
83
|
end
|
77
84
|
|
78
85
|
private
|
79
86
|
|
80
87
|
def produce_record(kafka, avro, store)
|
81
88
|
producer = Kril::Producer.new(kafka: kafka, avro: avro)
|
82
|
-
|
89
|
+
names = schema_name(store)
|
83
90
|
record_as_json = JSON.parse(record)
|
84
|
-
infered_topic = topic || schema_name
|
91
|
+
infered_topic = topic || names[:schema_name]
|
85
92
|
producer.send(record: record_as_json,
|
86
|
-
schema_name: schema_name,
|
93
|
+
schema_name: names[:schema_name],
|
94
|
+
namespace: names[:namespace],
|
87
95
|
topic: infered_topic,
|
88
96
|
syncronous: synchronous?)
|
89
97
|
print_record(infered_topic, record_as_json)
|
@@ -101,7 +109,7 @@ Clamp do
|
|
101
109
|
end
|
102
110
|
|
103
111
|
def schema_name(schema_store)
|
104
|
-
handler = Kril::SchemaHandler.new(schema_store: schema_store)
|
112
|
+
handler = Kril::SchemaHandler.new(schema_store: schema_store, schemas_path: schemas_path)
|
105
113
|
handler.process(schema || topic)
|
106
114
|
end
|
107
115
|
|
data/lib/kril/producer.rb
CHANGED
@@ -20,11 +20,18 @@ module Kril
|
|
20
20
|
#
|
21
21
|
# record - record to serialize and commit [String]
|
22
22
|
# schema_name - name of schema to encode record from [String]
|
23
|
+
# namespace - namespace of schema (optional) [String]
|
23
24
|
# topic - name of topic. Will be schema_name if nil (optional) [String]
|
24
25
|
# synchronous - blocks until commit if true (optional) [Boolean]
|
25
|
-
def send(record:,
|
26
|
+
def send(record:,
|
27
|
+
schema_name:,
|
28
|
+
namespace: nil,
|
29
|
+
topic: nil,
|
30
|
+
syncronous: false)
|
26
31
|
topic ||= schema_name
|
27
|
-
encoded = @avro.encode(record,
|
32
|
+
encoded = @avro.encode(record,
|
33
|
+
schema_name: schema_name,
|
34
|
+
namespace: namespace)
|
28
35
|
if syncronous
|
29
36
|
@sync.produce(encoded, topic: topic)
|
30
37
|
@sync.deliver_messages
|
data/lib/kril/schema_handler.rb
CHANGED
@@ -5,7 +5,7 @@ module Kril
|
|
5
5
|
class SchemaHandler
|
6
6
|
# schemas_path - directory of schema repository [String]
|
7
7
|
# schema_store - schema store [AvroTurf::SchemaStore]
|
8
|
-
def initialize(schemas_path
|
8
|
+
def initialize(schemas_path:,
|
9
9
|
schema_store: nil)
|
10
10
|
schema_store ||= AvroTurf::SchemaStore.new(path: schemas_path)
|
11
11
|
@schema_store = schema_store
|
@@ -15,21 +15,32 @@ module Kril
|
|
15
15
|
# Handles input to reference or create schema.
|
16
16
|
#
|
17
17
|
# input_string - schema name, schema file, or schema contents [String]
|
18
|
-
# returns - schema name [
|
18
|
+
# returns - schema name, namespace [Hash]
|
19
19
|
def process(input_string)
|
20
20
|
if File.exist?(input_string)
|
21
21
|
copy_schema_to_store(input_string)
|
22
22
|
elsif schema?(input_string)
|
23
23
|
save_schema(input_string)
|
24
24
|
else
|
25
|
-
|
25
|
+
name, namespace = separate_fullname(input_string)
|
26
|
+
namespace = nil if namespace.empty?
|
27
|
+
schema = @schema_store.find(name, namespace)
|
28
|
+
{
|
29
|
+
schema_name: schema&.name,
|
30
|
+
namespace: schema&.namespace
|
31
|
+
}
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
29
35
|
private
|
30
36
|
|
37
|
+
def separate_fullname(fullname)
|
38
|
+
arr = fullname.split('.')
|
39
|
+
[arr.pop, arr.join('.')]
|
40
|
+
end
|
41
|
+
|
31
42
|
def schema?(input)
|
32
|
-
JSON.parse(input)['name']
|
43
|
+
!JSON.parse(input)['name'].nil?
|
33
44
|
rescue StandardError
|
34
45
|
false
|
35
46
|
end
|
@@ -37,18 +48,35 @@ module Kril
|
|
37
48
|
def save_schema(schema)
|
38
49
|
schema = JSON.parse(schema)
|
39
50
|
schema_name = schema['name']
|
40
|
-
|
41
|
-
|
42
|
-
|
51
|
+
namespace = schema['namespace']
|
52
|
+
if namespace
|
53
|
+
path = File.join(@schemas_path, namespace.split('.'))
|
54
|
+
FileUtils.mkdir_p(path)
|
55
|
+
end
|
56
|
+
File.open(File.join(path || @schemas_path, "#{schema_name}.avsc"), 'w') do |file|
|
57
|
+
file.write(JSON.pretty_generate(schema))
|
58
|
+
end
|
59
|
+
{
|
60
|
+
schema_name: schema_name,
|
61
|
+
namespace: namespace
|
62
|
+
}
|
43
63
|
end
|
44
64
|
|
45
65
|
def copy_schema_to_store(path)
|
46
66
|
schema = File.read(path)
|
47
67
|
raise ArgumentError, "Not a valid schema: #{path}" unless schema?(schema)
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
68
|
+
json = JSON.parse(schema)
|
69
|
+
schema_name = json['name']
|
70
|
+
namespace = json['namespace']
|
71
|
+
if namespace
|
72
|
+
schema_path = File.join(@schemas_path, namespace.split('.'))
|
73
|
+
FileUtils.mkdir_p(schema_path)
|
74
|
+
end
|
75
|
+
FileUtils.copy_file(path, File.join(schema_path || @schemas_path, "#{schema_name}.avsc"))
|
76
|
+
{
|
77
|
+
schema_name: schema_name,
|
78
|
+
namespace: namespace
|
79
|
+
}
|
52
80
|
end
|
53
81
|
end
|
54
82
|
end
|
data/lib/kril/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kril
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chad Bowman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: avro_turf
|