retl 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +336 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/retl.rb +7 -0
- data/lib/retl/context.rb +29 -0
- data/lib/retl/event_router.rb +22 -0
- data/lib/retl/fork_data_collector.rb +17 -0
- data/lib/retl/handlers/explode_handler.rb +11 -0
- data/lib/retl/handlers/filter_handler.rb +10 -0
- data/lib/retl/handlers/fork_handler.rb +15 -0
- data/lib/retl/handlers/handler.rb +21 -0
- data/lib/retl/handlers/inspect_handler.rb +10 -0
- data/lib/retl/handlers/step_handler.rb +16 -0
- data/lib/retl/handlers/transform_handler.rb +10 -0
- data/lib/retl/path.rb +178 -0
- data/lib/retl/path_builder.rb +62 -0
- data/lib/retl/transformation.rb +69 -0
- data/lib/retl/version.rb +3 -0
- data/retl.gemspec +25 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 65892cccb09b0ee2bde26342b072f3c405b4b735
|
4
|
+
data.tar.gz: b56d3d24285ce3c40aa5e8e98924fe2cf0a09341
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50953eae75e94eb9383320c56268c0a18e3d1289d234509e13a90eb4c8d43ae2686c6adf6f45f45aed880680b0dd389f2a532a22555ed3bb140aa79a9f8f27cc
|
7
|
+
data.tar.gz: 7cd0b87b33d5810820e978722f3c2e3b63f160a01b65ba4105e4f800135b43a2ec68d65eacf034d78542a8da9de6d3aca263a19df9dced3d600ef304952f9940
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,336 @@
|
|
1
|
+
# rETL
|
2
|
+
|
3
|
+
**R**uby
|
4
|
+
**E**xtract
|
5
|
+
**T**ransform
|
6
|
+
**L**oad
|
7
|
+
|
8
|
+
rETL is a gem with a rich DSL for ETL (extract, transform, load) projects in
|
9
|
+
Ruby.
|
10
|
+
|
11
|
+
## Transforming Data
|
12
|
+
|
13
|
+
The core construct for transforming data is called a **Path**. A path describes
|
14
|
+
how data should be transformed. rETL has a rich DSL that is intended to work on
|
15
|
+
Hash objects, but it is not limited strictly to Hashes.
|
16
|
+
|
17
|
+
### Defining a Path
|
18
|
+
|
19
|
+
A path is composed of **Steps**. Each step is intended to act on a single unit
|
20
|
+
of data that is passed into a block (like a row from a database query). The
|
21
|
+
steps are executed in the order they are defined. In other words, the data
|
22
|
+
passes through each step until it is completely transformed. In order to keep
|
23
|
+
the Path easy to understand and maintain, each step should be responsible for a
|
24
|
+
single action on a row.
|
25
|
+
|
26
|
+
This path will `filter` data by `:first_name`, and then `transform` it by adding
|
27
|
+
a `:full_name` key.
|
28
|
+
|
29
|
+
```
|
30
|
+
my_path = Retl::Path.new do
|
31
|
+
filter do |row|
|
32
|
+
row[:first_name] == "David"
|
33
|
+
end
|
34
|
+
|
35
|
+
transform do |row|
|
36
|
+
row[:full_name] = row[:first_name] + " " + row[:last_name]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Once a path is defined, it can be used to transform data with the
|
42
|
+
`#transform(enumerable, options={})` method. A `Transformation` object is
|
43
|
+
returned, which is `Enumerable`.
|
44
|
+
|
45
|
+
```
|
46
|
+
data = [
|
47
|
+
{ full_name: "David" , last_name: "Biehl" },
|
48
|
+
{ full_name: "Indiana", last_name: "Jones" }
|
49
|
+
]
|
50
|
+
|
51
|
+
result = my_path.transform(data)
|
52
|
+
result.to_a
|
53
|
+
#=> [ { full_name: "David", last_name: "Biehl", full_name: "David Biehl" } ]
|
54
|
+
```
|
55
|
+
|
56
|
+
### Available Steps
|
57
|
+
|
58
|
+
There are several steps available in the rETL DSL that will transform data in
|
59
|
+
different ways.
|
60
|
+
|
61
|
+
#### Transform
|
62
|
+
|
63
|
+
The `transform` step will mutate the data passed into the block. The return
|
64
|
+
value of a `transform` step is ignored. See the `:full_name` example above.
|
65
|
+
|
66
|
+
#### Replace
|
67
|
+
|
68
|
+
The `replace` step will replace the data with the return value of the block. A
|
69
|
+
common use is to use `replace` as the first step to convert incoming objects
|
70
|
+
into hashes.
|
71
|
+
|
72
|
+
```
|
73
|
+
users = Users.where(active: true) # a bunch of ActiveRecord objects
|
74
|
+
|
75
|
+
my_path = Retl::Path.new do
|
76
|
+
replace do |user|
|
77
|
+
user.to_hash
|
78
|
+
end
|
79
|
+
|
80
|
+
# perform other steps with the hash
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
#### Filter & Reject
|
85
|
+
|
86
|
+
The `filter` step uses a predicate block to determine if the data should proceed
|
87
|
+
to the next step. Conversely, `reject` will discard data if the predicate is
|
88
|
+
truthy. `select` is an alias for `filter`.
|
89
|
+
|
90
|
+
```
|
91
|
+
data = [
|
92
|
+
{name: "David" , age: 33}
|
93
|
+
{name: "Indiana", age: 50}
|
94
|
+
{name: "Sully" , age: 7}
|
95
|
+
{name: "Boo" , age: 3}
|
96
|
+
]
|
97
|
+
|
98
|
+
my_path = Retl::Path.new do
|
99
|
+
transform do |row|
|
100
|
+
row[:adult_or_child] = row[:age] >= 18 ? "adult" : "child"
|
101
|
+
end
|
102
|
+
|
103
|
+
reject do |row|
|
104
|
+
row[:adult_or_child] == "child"
|
105
|
+
end
|
106
|
+
|
107
|
+
select do |row| # `select` is an alias of `filter`
|
108
|
+
row[:age].odd?
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
result = my_path.transform(data)
|
113
|
+
result.to_a
|
114
|
+
#=> [ { name: "David", ... } ]
|
115
|
+
```
|
116
|
+
|
117
|
+
#### Calculate
|
118
|
+
|
119
|
+
The `calculate` step will calculate a single key on a Hash with the return value
|
120
|
+
of the block. For example, `calculate` can be used instead of `transform` from
|
121
|
+
the first example. `calc` is a short-hand alias for `calculate`.
|
122
|
+
|
123
|
+
```
|
124
|
+
my_path = Retl::Path.new do
|
125
|
+
calculate(:full_name) do |row|
|
126
|
+
row[:first_name] + " " + row[:last_name]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
#### Inspect
|
132
|
+
|
133
|
+
`inspect` steps cannot change the incoming data, and the return value of the
|
134
|
+
block is ignored. These steps are intended for debugging, testing or logging
|
135
|
+
purposes.
|
136
|
+
|
137
|
+
```
|
138
|
+
# rspec example
|
139
|
+
|
140
|
+
it "adds a full name" do
|
141
|
+
rspec = self
|
142
|
+
|
143
|
+
my_path = Retl::Path.new do
|
144
|
+
calc(:full_name) do |row|
|
145
|
+
row[:first_name] + " " + row[:last_name]
|
146
|
+
end
|
147
|
+
|
148
|
+
inspect do |row|
|
149
|
+
rspec.expect(row).to rspec.include(full_name: "Indiana Jones")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
my_path.transform([ { first_name: "Indiana", last_name: "Jones"} ]).to_a
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
#### Fork
|
158
|
+
|
159
|
+
Paths can be forked for alternate results with the `fork(name)` step. The forked
|
160
|
+
data can then be accessed on the result with the `#forks(name)` method. Forks
|
161
|
+
are unaffected by any steps that take place after the fork is defined.
|
162
|
+
|
163
|
+
```
|
164
|
+
data = [
|
165
|
+
{ name: "David" , age: 33 }
|
166
|
+
{ name: "Indiana", age: 50 }
|
167
|
+
{ name: "Sully" , age: 7 }
|
168
|
+
{ name: "Boo" , age: 3 }
|
169
|
+
]
|
170
|
+
|
171
|
+
my_path = Retl::Path.new do
|
172
|
+
transform do |row|
|
173
|
+
row[:adult_or_child] = row[:age] >= 18 ? "adult" : "child"
|
174
|
+
end
|
175
|
+
|
176
|
+
fork(:adults) do
|
177
|
+
filter do |row|
|
178
|
+
row[:adult_or_child] == "adult"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
fork(:children) do
|
183
|
+
filter do |row|
|
184
|
+
row[:adult_or_child] == "child"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
reject do |row|
|
189
|
+
true # oops, rejecting everything after the forks.
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
result = my_path.transform(data)
|
194
|
+
|
195
|
+
result.forks(:adults).to_a
|
196
|
+
#=> [ { name: "David", ... }, { name: "Indiana", ... } ]
|
197
|
+
|
198
|
+
result.forks(:children).to_a
|
199
|
+
#=> [ { name: "Sully", ... }, { name: "Boo", ... } ]
|
200
|
+
|
201
|
+
result.to_a
|
202
|
+
#> [ ]
|
203
|
+
```
|
204
|
+
|
205
|
+
#### Explode
|
206
|
+
|
207
|
+
The `explode` step adds additional data to the Path. The return value of the
|
208
|
+
block should respond to `#each`, like an Array.
|
209
|
+
|
210
|
+
```
|
211
|
+
my_path = Reth::Path.new do
|
212
|
+
explode do |number|
|
213
|
+
number.times.map { |x| x + x + x }
|
214
|
+
end
|
215
|
+
|
216
|
+
filter do |number|
|
217
|
+
number.odd?
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
my_path.transform(6).to_a
|
222
|
+
#=> [3, 9, 15]
|
223
|
+
```
|
224
|
+
|
225
|
+
#### Path Reuse
|
226
|
+
|
227
|
+
In rETL, Paths can be re-used with the `step` step. Common Paths can be defined
|
228
|
+
to ensure that calculations yield consistent results throughout the entire ETL
|
229
|
+
project. Consistent data and meanings will make the data warehouse easier to
|
230
|
+
understand for data consumers.
|
231
|
+
|
232
|
+
```
|
233
|
+
AdultOrChild = Retl::Path.new do
|
234
|
+
calculate(:adult_or_child) do
|
235
|
+
row[:age] >= 18 ? "adult" : "child"
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
my_path = Retl::Path.new do
|
240
|
+
step AdultOrChild
|
241
|
+
|
242
|
+
### perform other steps
|
243
|
+
end
|
244
|
+
```
|
245
|
+
|
246
|
+
### Dependencies
|
247
|
+
|
248
|
+
Dependencies can be defined with the `depends_on(name)`. The value of the
|
249
|
+
dependency is accessible inside of each step by its name. In this example, we'll
|
250
|
+
define an `age_lookup` dependency.
|
251
|
+
|
252
|
+
```
|
253
|
+
my_path = Retl::Path.new do
|
254
|
+
depends_on(:age_lookup) do
|
255
|
+
{
|
256
|
+
"adult" => "Adults are 18 or older",
|
257
|
+
"child" => "Children are younger than 18"
|
258
|
+
}
|
259
|
+
end
|
260
|
+
|
261
|
+
step AdultOrChild # see previous example
|
262
|
+
|
263
|
+
calculate(:age_description) do |row|
|
264
|
+
age_lookup[row[:adult_or_child]]
|
265
|
+
end
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
Dependencies can also be injected when the transformation takes place. This is
|
270
|
+
useful for testing by passing in mocks or stubs. Also, concrete results from
|
271
|
+
other paths can be merged merged into a single path making data integration
|
272
|
+
possible.
|
273
|
+
|
274
|
+
```
|
275
|
+
my_path = Retl::Path.new do
|
276
|
+
depends_on(:age_lookup) do |options| # transformation options are passed into `depends_on`
|
277
|
+
options[:age_lookup] || (raise ArgumentError, "This Path depends on an age lookup hash")
|
278
|
+
end
|
279
|
+
|
280
|
+
step AdultOrChild
|
281
|
+
|
282
|
+
calculate(:age_description) do |row|
|
283
|
+
age_lookup[row[:adult_or_child]]
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
age_lookup_hash = {
|
288
|
+
"adult" => "Adults are 18 or older",
|
289
|
+
"child" => "Children are younger than 18"
|
290
|
+
}
|
291
|
+
|
292
|
+
my_path.transform(data, age_lookup: age_lookup_hash)
|
293
|
+
```
|
294
|
+
|
295
|
+
## Roadmap
|
296
|
+
|
297
|
+
Currently the rETL gem's strengths are transforming data and code reuse. However
|
298
|
+
this is only one part of an ETL project. I haven't even started on extracting or
|
299
|
+
loading. Fortunately, the contract for transformation is very simple.
|
300
|
+
|
301
|
+
```
|
302
|
+
path.transform(Enumerable)
|
303
|
+
#=> Enumerable
|
304
|
+
```
|
305
|
+
|
306
|
+
Enumerales in, Enumerales out. This makes the application of the gem pretty much
|
307
|
+
universal for any type of data transformation requirement in Ruby.
|
308
|
+
|
309
|
+
## Installation
|
310
|
+
|
311
|
+
Add this line to your application's Gemfile:
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
gem 'retl'
|
315
|
+
```
|
316
|
+
|
317
|
+
And then execute:
|
318
|
+
|
319
|
+
$ bundle
|
320
|
+
|
321
|
+
Or install it yourself as:
|
322
|
+
|
323
|
+
$ gem install retl
|
324
|
+
|
325
|
+
## Contributing
|
326
|
+
|
327
|
+
Bug reports and pull requests are welcome on GitHub at
|
328
|
+
https://github.com/davidbiehl/retl. This project is intended to be a safe,
|
329
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
330
|
+
the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
331
|
+
|
332
|
+
|
333
|
+
## License
|
334
|
+
|
335
|
+
The gem is available as open source under the terms of the [MIT
|
336
|
+
License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "retl"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/retl.rb
ADDED
data/lib/retl/context.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "retl/event_router"
|
2
|
+
|
3
|
+
module Retl
|
4
|
+
class Context
|
5
|
+
def initialize(path, options={})
|
6
|
+
path.dependencies.each do |name, dependency|
|
7
|
+
self.class.send(:define_method, name) { dependency.call(options) }
|
8
|
+
end
|
9
|
+
|
10
|
+
@_events = EventRouter.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute_step(step, data)
|
14
|
+
if step.is_a?(Proc)
|
15
|
+
instance_exec(data, self, &step)
|
16
|
+
else
|
17
|
+
if step.method(:call).arity.abs == 2
|
18
|
+
step.call(data, self)
|
19
|
+
else
|
20
|
+
step.call(data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def _events
|
26
|
+
@_events
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Retl
|
2
|
+
class EventRouter
|
3
|
+
def initialize
|
4
|
+
@listeners = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def listen_to(event_name, &block)
|
8
|
+
@listeners[event_name] ||= []
|
9
|
+
@listeners[event_name] << block
|
10
|
+
end
|
11
|
+
|
12
|
+
def trigger(event_name, args={})
|
13
|
+
listeners = @listeners[event_name]
|
14
|
+
|
15
|
+
if listeners
|
16
|
+
listeners.each do |handler|
|
17
|
+
handler.call(args)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Retl
|
2
|
+
class ForkDataCollector
|
3
|
+
def initialize(context)
|
4
|
+
@fork_data = {}
|
5
|
+
|
6
|
+
context._events.listen_to(:fork_data) do |args|
|
7
|
+
fork_name = args[:fork_name]
|
8
|
+
@fork_data[fork_name] ||= []
|
9
|
+
@fork_data[fork_name] << args[:data]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def take(name)
|
14
|
+
@fork_data.delete(name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "handler"
|
2
|
+
|
3
|
+
module Retl
|
4
|
+
class ForkHandler < Handler
|
5
|
+
def initialize(fork)
|
6
|
+
super()
|
7
|
+
@fork = fork
|
8
|
+
end
|
9
|
+
|
10
|
+
def push_in(data, context)
|
11
|
+
context._events.trigger(:fork_data, fork_name: @fork, data: data.dup)
|
12
|
+
push_out data
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Retl
|
2
|
+
class Handler
|
3
|
+
def initialize
|
4
|
+
@output = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def output
|
8
|
+
@output.slice!(0, @output.count)
|
9
|
+
end
|
10
|
+
|
11
|
+
def push_in(data, context)
|
12
|
+
raise NotImplementedError, "Handlers much implement the #push_in(data, context) method"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def push_out(data)
|
18
|
+
@output.push data
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/retl/path.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require "retl/path_builder"
|
2
|
+
require "retl/transformation"
|
3
|
+
require "retl/context"
|
4
|
+
require "retl/handlers/handler"
|
5
|
+
require "retl/handlers/step_handler"
|
6
|
+
require "retl/handlers/explode_handler"
|
7
|
+
require "retl/handlers/fork_handler"
|
8
|
+
|
9
|
+
module Retl
|
10
|
+
# A Path is a blueprint for transforming data
|
11
|
+
#
|
12
|
+
# A Path is a sequence of steps that are executed on data in order to
|
13
|
+
# transform it.
|
14
|
+
#
|
15
|
+
# Paths can be built with a block using the API defined in the {#PathBuilder}.
|
16
|
+
#
|
17
|
+
# Steps are added to the Path with the {#add_step} method.
|
18
|
+
#
|
19
|
+
# A Path can act on a single piece of data with the {#call} method.
|
20
|
+
#
|
21
|
+
# A Path can transform a list of data with the {#transform} method.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# path = Retl::Path.new do
|
25
|
+
# step do |data|
|
26
|
+
# data[:something] = "some value"
|
27
|
+
# data
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# calculate(:something_else) do
|
31
|
+
# "some other value"
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# transform do |data|
|
35
|
+
# data[:something_together] = data[:something] + data[:something_else]
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# path.transform(data)
|
40
|
+
#
|
41
|
+
class Path
|
42
|
+
attr_reader :steps, :source, :dependencies
|
43
|
+
|
44
|
+
# Initializes a new Path
|
45
|
+
#
|
46
|
+
# @param parent [Path] - a Path to inherit from
|
47
|
+
def initialize(parent=nil, &block)
|
48
|
+
@steps = []
|
49
|
+
@dependencies = {}
|
50
|
+
@forks = {}
|
51
|
+
@fork_builders = {}
|
52
|
+
|
53
|
+
if parent
|
54
|
+
@dependencies = parent.dependencies.dup
|
55
|
+
add_step parent.dup, handler: ExplodeHandler
|
56
|
+
end
|
57
|
+
|
58
|
+
build(&block) if block
|
59
|
+
end
|
60
|
+
|
61
|
+
# Builds a Path with the PathBuilder DSL
|
62
|
+
#
|
63
|
+
# @return [void]
|
64
|
+
def build(&block)
|
65
|
+
PathBuilder.new(self, &block)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Initializer when copying a Path
|
69
|
+
#
|
70
|
+
# When a Path is copied, a copy of the Path's steps need to be copied
|
71
|
+
# as well. That was if additional steps are added to the original Path
|
72
|
+
# they won't be part of the copied Path.
|
73
|
+
def initialize_copy(source)
|
74
|
+
@steps = source.steps.dup
|
75
|
+
@forks = {}
|
76
|
+
@fork_builders = {}
|
77
|
+
end
|
78
|
+
|
79
|
+
# Adds a step to the Path
|
80
|
+
#
|
81
|
+
# A step is called with data and is expected to return complete, modified
|
82
|
+
# data.
|
83
|
+
#
|
84
|
+
# Steps are executed in the sequence they are added.
|
85
|
+
#
|
86
|
+
# @param step [#call(data)] the step to take
|
87
|
+
#
|
88
|
+
# @return [void]
|
89
|
+
def add_step(step, handler: StepHandler)
|
90
|
+
add_handler handler.new(step)
|
91
|
+
end
|
92
|
+
|
93
|
+
def add_handler(handler)
|
94
|
+
@steps << handler
|
95
|
+
end
|
96
|
+
|
97
|
+
# Execuutes the Path with the given data
|
98
|
+
#
|
99
|
+
# Currently the DSL mostly supports Hash based data, so this expects a
|
100
|
+
# Hash.
|
101
|
+
#
|
102
|
+
# Since a piece of data can now be exploded, this method will always
|
103
|
+
# return an Array.
|
104
|
+
#
|
105
|
+
# @param data [Hash] the data that will be transformed by the Path
|
106
|
+
# @param context [Context] the execution context for the transformation
|
107
|
+
#
|
108
|
+
# @return [Array<Hash>] the transformed data
|
109
|
+
def call(data, context=Context.new(self))
|
110
|
+
@steps.reduce([data]) do |queue, handler|
|
111
|
+
queue.each do |data|
|
112
|
+
handler.push_in(data, context)
|
113
|
+
end
|
114
|
+
handler.output
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Adds an fork to the Path
|
119
|
+
#
|
120
|
+
# Forks can be accessed via #forks
|
121
|
+
#
|
122
|
+
# @example
|
123
|
+
# path.add_fork(:river) do
|
124
|
+
# filter { |data| data[:is_wet] }
|
125
|
+
# end
|
126
|
+
# path.forks(:river)
|
127
|
+
#
|
128
|
+
# @param name [Symbol] the name of the fork
|
129
|
+
#
|
130
|
+
# @return [void]
|
131
|
+
def add_fork(name, &block)
|
132
|
+
fork = Path.new(&block)
|
133
|
+
add_handler ForkHandler.new(name)
|
134
|
+
@forks[name] = fork
|
135
|
+
end
|
136
|
+
|
137
|
+
# Gets a fork by name
|
138
|
+
#
|
139
|
+
# @param name [Symbol] the name of the fork to get
|
140
|
+
#
|
141
|
+
# @return [Path] the forked path
|
142
|
+
def forks(name)
|
143
|
+
@forks[name]
|
144
|
+
end
|
145
|
+
|
146
|
+
# Adds a fork builder block
|
147
|
+
#
|
148
|
+
# @param name [Symbol] the name of the fork to build
|
149
|
+
# @param &block [Block] the block that builds the fork
|
150
|
+
#
|
151
|
+
# @return [Fork] the built fork
|
152
|
+
def add_fork_builder(name, &block)
|
153
|
+
@fork_builders[name] = block
|
154
|
+
add_fork(name, &block)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Adds a depdency to the Path
|
158
|
+
#
|
159
|
+
# @param name [Symbol] the name of the dependency
|
160
|
+
# (should be a valid Ruby method)
|
161
|
+
# @param source [#call] a callable object that will return the depdency
|
162
|
+
#
|
163
|
+
# @return [void]
|
164
|
+
def add_dependency(name, source)
|
165
|
+
@dependencies[name] = source
|
166
|
+
end
|
167
|
+
|
168
|
+
# Executes the Path with data
|
169
|
+
#
|
170
|
+
# @param [Enumerable] the data that will be processed by the Path
|
171
|
+
# @option options that will be passed to #depends_on for the context
|
172
|
+
#
|
173
|
+
# @return [Transformation] the Tranformation of the data by the Path
|
174
|
+
def transform(enumerable, options={})
|
175
|
+
Transformation.new(enumerable, self, options)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "retl/handlers/step_handler"
|
2
|
+
require "retl/handlers/transform_handler"
|
3
|
+
require "retl/handlers/filter_handler"
|
4
|
+
require "retl/handlers/inspect_handler"
|
5
|
+
require "retl/handlers/explode_handler"
|
6
|
+
|
7
|
+
|
8
|
+
module Retl
|
9
|
+
class PathBuilder
|
10
|
+
def initialize(path, &block)
|
11
|
+
@path = path
|
12
|
+
instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def step(step=nil, handler: StepHandler, &block)
|
16
|
+
step ||= block
|
17
|
+
@path.add_step step, handler: handler
|
18
|
+
end
|
19
|
+
alias_method :replace, :step
|
20
|
+
|
21
|
+
def transform(action=nil, &block)
|
22
|
+
action ||= block
|
23
|
+
step(action, handler: TransformHandler)
|
24
|
+
end
|
25
|
+
|
26
|
+
def filter(predicate=nil, &block)
|
27
|
+
predicate ||= block
|
28
|
+
step(predicate, handler: FilterHandler)
|
29
|
+
end
|
30
|
+
alias_method :select, :filter
|
31
|
+
|
32
|
+
def fork(name, &block)
|
33
|
+
@path.add_fork_builder(name, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def inspect(action=nil, &block)
|
37
|
+
action ||= block
|
38
|
+
step(action, handler: InspectHandler)
|
39
|
+
end
|
40
|
+
|
41
|
+
def reject(predicate=nil, &block)
|
42
|
+
predicate ||= block
|
43
|
+
filter { |data, context| !context.execute_step(predicate, data) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def calculate(key, action=nil, &block)
|
47
|
+
action ||= block
|
48
|
+
transform { |data, context| data[key] = context.execute_step(action, data) }
|
49
|
+
end
|
50
|
+
alias_method :calc, :calculate
|
51
|
+
|
52
|
+
def depends_on(name, source=nil, &block)
|
53
|
+
source ||= block
|
54
|
+
@path.add_dependency(name, source)
|
55
|
+
end
|
56
|
+
|
57
|
+
def explode(action=nil, &block)
|
58
|
+
action ||= block
|
59
|
+
step(action, handler: ExplodeHandler)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "retl/context"
|
2
|
+
require "retl/fork_data_collector"
|
3
|
+
|
4
|
+
module Retl
|
5
|
+
class Transformation
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(enumerable, path, options={})
|
9
|
+
@enumerable, @path, @options = enumerable, path, options
|
10
|
+
@context = Context.new(@path, @options)
|
11
|
+
@fork_data = ForkDataCollector.new(@context)
|
12
|
+
end
|
13
|
+
|
14
|
+
def each(&block)
|
15
|
+
if @each
|
16
|
+
@each.each(&block)
|
17
|
+
else
|
18
|
+
build_each_result(&block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def each_slice(size, &block)
|
23
|
+
@each_slice ||= {}
|
24
|
+
if @each_slice[size]
|
25
|
+
@each_slice[size].each(&block)
|
26
|
+
else
|
27
|
+
build_each_slice_result(size, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def forks(name)
|
32
|
+
build_each_result
|
33
|
+
@path.forks(name).transform(@fork_data.take(name), @options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def load_into(*destinations)
|
37
|
+
destinations = Array(destinations)
|
38
|
+
|
39
|
+
each do |data|
|
40
|
+
destinations.each do |destination|
|
41
|
+
destination << data
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
destinations.each do |destination|
|
46
|
+
destination.close if destination.respond_to?(:close)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def build_each_result(&block)
|
53
|
+
@each ||= @enumerable.reduce([]) do |result, data|
|
54
|
+
@path.call(data, @context).each do |data|
|
55
|
+
yield data if block_given?
|
56
|
+
result << data
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def build_each_slice_result(size, &block)
|
62
|
+
@each_slice[size] ||= @enumerable.each_slice(size).reduce([]) do |result, slice|
|
63
|
+
transformed_slice = Transformation.new(slice, @path, @options)
|
64
|
+
yield transformed_slice if block_given?
|
65
|
+
result << transformed_slice
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/retl/version.rb
ADDED
data/retl.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'retl/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "retl"
|
8
|
+
spec.version = Retl::VERSION
|
9
|
+
spec.authors = ["David Biehl"]
|
10
|
+
spec.email = ["me@davidbiehl.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Path based ETL processing in Ruby}
|
13
|
+
spec.description = %q{Define paths for your data for ETL processing}
|
14
|
+
spec.homepage = "https://github.com/davidbiehl/retl.git"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: retl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Biehl
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-20 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Define paths for your data for ETL processing
|
56
|
+
email:
|
57
|
+
- me@davidbiehl.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/retl.rb
|
73
|
+
- lib/retl/context.rb
|
74
|
+
- lib/retl/event_router.rb
|
75
|
+
- lib/retl/fork_data_collector.rb
|
76
|
+
- lib/retl/handlers/explode_handler.rb
|
77
|
+
- lib/retl/handlers/filter_handler.rb
|
78
|
+
- lib/retl/handlers/fork_handler.rb
|
79
|
+
- lib/retl/handlers/handler.rb
|
80
|
+
- lib/retl/handlers/inspect_handler.rb
|
81
|
+
- lib/retl/handlers/step_handler.rb
|
82
|
+
- lib/retl/handlers/transform_handler.rb
|
83
|
+
- lib/retl/path.rb
|
84
|
+
- lib/retl/path_builder.rb
|
85
|
+
- lib/retl/transformation.rb
|
86
|
+
- lib/retl/version.rb
|
87
|
+
- retl.gemspec
|
88
|
+
homepage: https://github.com/davidbiehl/retl.git
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Path based ETL processing in Ruby
|
112
|
+
test_files: []
|