ul-wukong 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +60 -0
- data/.gitmodules +6 -0
- data/.rspec +2 -0
- data/.travis.yml +19 -0
- data/.yardopts +6 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +17 -0
- data/Guardfile +12 -0
- data/LICENSE.md +95 -0
- data/NOTES-travis.md +31 -0
- data/README-old.md +422 -0
- data/README.md +1308 -0
- data/Rakefile +28 -0
- data/TODO.md +99 -0
- data/bin/cutc +30 -0
- data/bin/cuttab +5 -0
- data/bin/greptrue +6 -0
- data/bin/md5sort +20 -0
- data/bin/setcat +11 -0
- data/bin/tabchar +5 -0
- data/bin/uniq-ord +59 -0
- data/bin/uniqc +3 -0
- data/bin/wu +34 -0
- data/bin/wu-clean-encoding +31 -0
- data/bin/wu-date +13 -0
- data/bin/wu-datetime +13 -0
- data/bin/wu-hist +3 -0
- data/bin/wu-lign +186 -0
- data/bin/wu-local +4 -0
- data/bin/wu-plus +9 -0
- data/bin/wu-source +5 -0
- data/bin/wu-sum +31 -0
- data/diagrams/wu_local.dot +39 -0
- data/diagrams/wu_local.dot.png +0 -0
- data/examples/Gemfile +38 -0
- data/examples/README.md +9 -0
- data/examples/basic/string_reverser.rb +23 -0
- data/examples/basic/tiny_count.rb +8 -0
- data/examples/basic/word_count/accumulator.rb +26 -0
- data/examples/basic/word_count/tokenizer.rb +13 -0
- data/examples/basic/word_count/word_count.rb +6 -0
- data/examples/dataflow/scraper_macro_flow.rb +28 -0
- data/examples/deploy_pack/Gemfile +6 -0
- data/examples/deploy_pack/README.md +6 -0
- data/examples/deploy_pack/a/b/c/.gitkeep +0 -0
- data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
- data/examples/deploy_pack/config/environment.rb +1 -0
- data/examples/dsl/dataflow/fibonacci_series.rb +101 -0
- data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
- data/examples/dsl/dataflow/simple.rb +12 -0
- data/examples/dsl/dataflow/telegram.rb +45 -0
- data/examples/dsl/workflow/cherry_pie.dot +97 -0
- data/examples/dsl/workflow/cherry_pie.md +104 -0
- data/examples/dsl/workflow/cherry_pie.png +0 -0
- data/examples/dsl/workflow/cherry_pie.rb +101 -0
- data/examples/empty/.gitkeep +0 -0
- data/examples/examples_helper.rb +9 -0
- data/examples/geo.rb +4 -0
- data/examples/geo/geo_grids.numbers +0 -0
- data/examples/geo/geolocated.rb +331 -0
- data/examples/geo/quadtile.rb +69 -0
- data/examples/geo/spec/geolocated_spec.rb +247 -0
- data/examples/geo/tile_fetcher.rb +77 -0
- data/examples/graph/implied_geolocation/README.md +63 -0
- data/examples/graph/minimum_spanning_tree/airfares_graphviz.rb +73 -0
- data/examples/improver/tweet_summary.rb +73 -0
- data/examples/loadable.rb +2 -0
- data/examples/munging/airline_flights/airline_flights.rake +83 -0
- data/examples/munging/airline_flights/airplane.rb +0 -0
- data/examples/munging/airline_flights/airport_id_unification.rb +129 -0
- data/examples/munging/airline_flights/airport_ok_chars.rb +4 -0
- data/examples/munging/airline_flights/indexable.rb +75 -0
- data/examples/munging/airline_flights/indexable_spec.rb +90 -0
- data/examples/munging/airline_flights/reconcile_airports.rb +142 -0
- data/examples/munging/airline_flights/tasks.rake +83 -0
- data/examples/munging/airline_flights/topcities.rb +167 -0
- data/examples/munging/geo/geo_json.rb +54 -0
- data/examples/munging/geo/geo_models.rb +69 -0
- data/examples/munging/geo/geonames_models.rb +107 -0
- data/examples/munging/geo/iso_codes.rb +172 -0
- data/examples/munging/geo/reconcile_countries.rb +124 -0
- data/examples/munging/geo/tasks.rake +71 -0
- data/examples/munging/wikipedia/articles/extract_articles-parsed.rb +79 -0
- data/examples/munging/wikipedia/articles/extract_articles-templated.rb +136 -0
- data/examples/munging/wikipedia/articles/textualize_articles.rb +54 -0
- data/examples/munging/wikipedia/articles/verify_structure.rb +43 -0
- data/examples/munging/wikipedia/articles/wp2txt-LICENSE.txt +22 -0
- data/examples/munging/wikipedia/articles/wp2txt_article.rb +259 -0
- data/examples/munging/wikipedia/articles/wp2txt_utils.rb +452 -0
- data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +5 -0
- data/examples/munging/wikipedia/dbpedia/dbpedia_extract_geocoordinates.rb +78 -0
- data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
- data/examples/munging/wikipedia/dbpedia/extract_links.rb +260 -0
- data/examples/munging/wikipedia/dbpedia/sameas_extractor.rb +20 -0
- data/examples/rake_helper.rb +97 -0
- data/examples/ruby_project/Gemfile +6 -0
- data/examples/ruby_project/README.md +6 -0
- data/examples/ruby_project/a/b/c/.gitkeep +0 -0
- data/examples/server_logs/geo_ip_mapping/munge_geolite.rb +82 -0
- data/examples/server_logs/logline.rb +95 -0
- data/examples/server_logs/models.rb +66 -0
- data/examples/server_logs/page_counts.pig +48 -0
- data/examples/server_logs/server_logs-01-parse-script.rb +13 -0
- data/examples/server_logs/server_logs-02-histograms-full.rb +33 -0
- data/examples/server_logs/server_logs-02-histograms-mapper.rb +14 -0
- data/examples/server_logs/server_logs-03-breadcrumbs-full.rb +71 -0
- data/examples/server_logs/server_logs-04-page_page_edges-full.rb +40 -0
- data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
- data/examples/serverlogs/models/logline.rb +102 -0
- data/examples/serverlogs/parser/apache_parser_widget.rb +46 -0
- data/examples/serverlogs/visit_paths/common.rb +4 -0
- data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
- data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
- data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
- data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
- data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
- data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
- data/examples/splitter.rb +94 -0
- data/examples/string_reverser.rb +7 -0
- data/examples/text/pig_latin/pig_latinizer.rb +35 -0
- data/examples/text/pig_latin/pig_latinizer_widget.rb +16 -0
- data/examples/text/regional_flavor/README.md +14 -0
- data/examples/text/regional_flavor/article_wordbags.pig +39 -0
- data/examples/text/regional_flavor/j01-article_wordbags.rb +4 -0
- data/examples/text/regional_flavor/simple_pig_script.pig +27 -0
- data/examples/twitter.rb +5 -0
- data/lib/hanuman.rb +36 -0
- data/lib/hanuman/graph.rb +97 -0
- data/lib/hanuman/graphvizzer.rb +206 -0
- data/lib/hanuman/graphvizzer/gv_models.rb +161 -0
- data/lib/hanuman/graphvizzer/gv_presenter.rb +97 -0
- data/lib/hanuman/link.rb +35 -0
- data/lib/hanuman/registry.rb +46 -0
- data/lib/hanuman/stage.rb +128 -0
- data/lib/hanuman/tree.rb +67 -0
- data/lib/wu/geo.rb +4 -0
- data/lib/wu/geo/geo_grids.numbers +0 -0
- data/lib/wu/geo/geolocated.rb +331 -0
- data/lib/wu/geo/quadtile.rb +69 -0
- data/lib/wu/graph/union_find.rb +62 -0
- data/lib/wu/model/reconcilable.rb +63 -0
- data/lib/wu/munging.rb +71 -0
- data/lib/wu/social/models/twitter.rb +31 -0
- data/lib/wu/wikipedia/models.rb +20 -0
- data/lib/wukong.rb +54 -0
- data/lib/wukong/dataflow.rb +43 -0
- data/lib/wukong/doc_helpers.rb +14 -0
- data/lib/wukong/doc_helpers/dataflow_handler.rb +29 -0
- data/lib/wukong/doc_helpers/field_handler.rb +91 -0
- data/lib/wukong/doc_helpers/processor_handler.rb +29 -0
- data/lib/wukong/driver.rb +214 -0
- data/lib/wukong/driver/event_machine_driver.rb +15 -0
- data/lib/wukong/driver/wiring.rb +68 -0
- data/lib/wukong/local.rb +42 -0
- data/lib/wukong/local/runner.rb +96 -0
- data/lib/wukong/local/stdio_driver.rb +104 -0
- data/lib/wukong/logger.rb +102 -0
- data/lib/wukong/model/faker.rb +136 -0
- data/lib/wukong/model/flatpack_parser/flat.rb +60 -0
- data/lib/wukong/model/flatpack_parser/flatpack.rb +4 -0
- data/lib/wukong/model/flatpack_parser/lang.rb +46 -0
- data/lib/wukong/model/flatpack_parser/parser.rb +55 -0
- data/lib/wukong/model/flatpack_parser/tokens.rb +130 -0
- data/lib/wukong/plugin.rb +48 -0
- data/lib/wukong/processor.rb +110 -0
- data/lib/wukong/rake_helper.rb +6 -0
- data/lib/wukong/runner.rb +169 -0
- data/lib/wukong/runner/boot_sequence.rb +123 -0
- data/lib/wukong/runner/code_loader.rb +52 -0
- data/lib/wukong/runner/command_runner.rb +44 -0
- data/lib/wukong/runner/deploy_pack_loader.rb +75 -0
- data/lib/wukong/runner/help_message.rb +42 -0
- data/lib/wukong/source.rb +33 -0
- data/lib/wukong/source/source_driver.rb +74 -0
- data/lib/wukong/source/source_runner.rb +38 -0
- data/lib/wukong/spec_helpers.rb +74 -0
- data/lib/wukong/spec_helpers/integration_tests.rb +150 -0
- data/lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb +207 -0
- data/lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb +97 -0
- data/lib/wukong/spec_helpers/shared_examples.rb +22 -0
- data/lib/wukong/spec_helpers/unit_tests.rb +135 -0
- data/lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb +132 -0
- data/lib/wukong/spec_helpers/unit_tests/unit_test_matchers.rb +169 -0
- data/lib/wukong/spec_helpers/unit_tests/unit_test_runner.rb +60 -0
- data/lib/wukong/version.rb +3 -0
- data/lib/wukong/widget/echo.rb +55 -0
- data/lib/wukong/widget/extract.rb +122 -0
- data/lib/wukong/widget/filters.rb +452 -0
- data/lib/wukong/widget/logger.rb +56 -0
- data/lib/wukong/widget/operators.rb +82 -0
- data/lib/wukong/widget/reducers.rb +10 -0
- data/lib/wukong/widget/reducers/accumulator.rb +73 -0
- data/lib/wukong/widget/reducers/bin.rb +368 -0
- data/lib/wukong/widget/reducers/count.rb +73 -0
- data/lib/wukong/widget/reducers/group.rb +128 -0
- data/lib/wukong/widget/reducers/group_concat.rb +98 -0
- data/lib/wukong/widget/reducers/improver.rb +71 -0
- data/lib/wukong/widget/reducers/join_xml.rb +37 -0
- data/lib/wukong/widget/reducers/moments.rb +72 -0
- data/lib/wukong/widget/reducers/sort.rb +180 -0
- data/lib/wukong/widget/reducers/uniq.rb +91 -0
- data/lib/wukong/widget/serializers.rb +317 -0
- data/lib/wukong/widget/utils.rb +46 -0
- data/lib/wukong/widgets.rb +7 -0
- data/spec/examples/dataflow/fibonacci_series_spec.rb +18 -0
- data/spec/examples/dataflow/parse_apache_logs_spec.rb +8 -0
- data/spec/examples/dataflow/parsing_spec.rb +14 -0
- data/spec/examples/dataflow/simple_spec.rb +34 -0
- data/spec/examples/dataflow/telegram_spec.rb +43 -0
- data/spec/examples/graph/minimum_spanning_tree_spec.rb +34 -0
- data/spec/examples/munging/airline_flights/identifiers_spec.rb +16 -0
- data/spec/examples/munging/airline_flights_spec.rb +202 -0
- data/spec/examples/text/pig_latin_spec.rb +18 -0
- data/spec/examples/workflow/cherry_pie_spec.rb +36 -0
- data/spec/hanuman/graph_spec.rb +119 -0
- data/spec/hanuman/hanuman_spec.rb +10 -0
- data/spec/hanuman/registry_spec.rb +123 -0
- data/spec/hanuman/stage_spec.rb +81 -0
- data/spec/hanuman/tree_spec.rb +119 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/support/example_test_helpers.rb +95 -0
- data/spec/support/hanuman_test_helpers.rb +92 -0
- data/spec/support/integration_helper.rb +38 -0
- data/spec/support/model_test_helpers.rb +115 -0
- data/spec/support/shared_context_for_graphs.rb +57 -0
- data/spec/support/shared_context_for_reducers.rb +37 -0
- data/spec/support/shared_examples_for_builders.rb +94 -0
- data/spec/support/shared_examples_for_shortcuts.rb +57 -0
- data/spec/wu/model/reconcilable_spec.rb +152 -0
- data/spec/wukong/dataflow_spec.rb +87 -0
- data/spec/wukong/driver_spec.rb +154 -0
- data/spec/wukong/local/runner_spec.rb +29 -0
- data/spec/wukong/local/stdio_driver_spec.rb +73 -0
- data/spec/wukong/local_spec.rb +6 -0
- data/spec/wukong/logger_spec.rb +49 -0
- data/spec/wukong/model/faker_spec.rb +132 -0
- data/spec/wukong/processor_spec.rb +21 -0
- data/spec/wukong/runner_spec.rb +132 -0
- data/spec/wukong/source_spec.rb +6 -0
- data/spec/wukong/widget/extract_spec.rb +101 -0
- data/spec/wukong/widget/filters_spec.rb +79 -0
- data/spec/wukong/widget/logger_spec.rb +23 -0
- data/spec/wukong/widget/operators_spec.rb +25 -0
- data/spec/wukong/widget/reducers/bin_spec.rb +92 -0
- data/spec/wukong/widget/reducers/count_spec.rb +11 -0
- data/spec/wukong/widget/reducers/group_spec.rb +21 -0
- data/spec/wukong/widget/reducers/join_xml_spec.rb +25 -0
- data/spec/wukong/widget/reducers/moments_spec.rb +36 -0
- data/spec/wukong/widget/reducers/sort_spec.rb +26 -0
- data/spec/wukong/widget/reducers/uniq_spec.rb +14 -0
- data/spec/wukong/widget/serializers_spec.rb +114 -0
- data/spec/wukong/widget/sink_spec.rb +19 -0
- data/spec/wukong/widget/source_spec.rb +65 -0
- data/spec/wukong/wu-local_spec.rb +109 -0
- data/spec/wukong/wu-source_spec.rb +32 -0
- data/spec/wukong/wu_spec.rb +14 -0
- data/spec/wukong/wukong_spec.rb +10 -0
- data/wukong.gemspec +35 -0
- metadata +465 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
module Wukong
|
2
|
+
module SpecHelpers
|
3
|
+
|
4
|
+
# A class for controlling the Wukong boot sequence from within
|
5
|
+
# unit tests.
|
6
|
+
#
|
7
|
+
# Subclasses the Wukong::Local::LocalRunner with which it shares
|
8
|
+
# most of its behavior:
|
9
|
+
#
|
10
|
+
# * Initialization is slightly different, to allow for each
|
11
|
+
# separate unit test in a suite to use a different
|
12
|
+
# Configliere::Param object for settings
|
13
|
+
#
|
14
|
+
# * The driver is the UnitTestDriver instead of the usual driver
|
15
|
+
# to allow for easily passing in records and getting them back
|
16
|
+
# out
|
17
|
+
#
|
18
|
+
# * The `run` method is a no-op so that control flow will exit out
|
19
|
+
# of the unit test back into the test suite
|
20
|
+
class UnitTestRunner < Wukong::Local::LocalRunner
|
21
|
+
|
22
|
+
# Initialize a new UnitTestRunner for the processor with the
|
23
|
+
# given `label` and `settings`.
|
24
|
+
#
|
25
|
+
# @param [Symbol] label
|
26
|
+
# @param [Hash] settings
|
27
|
+
def initialize label, settings={}
|
28
|
+
@dataflow = label
|
29
|
+
params = Configliere::Param.new
|
30
|
+
params.merge!(settings)
|
31
|
+
super(params)
|
32
|
+
end
|
33
|
+
|
34
|
+
def dataflow
|
35
|
+
@dataflow
|
36
|
+
end
|
37
|
+
|
38
|
+
# Override the LocalDriver with the UnitTestDriver so we can
|
39
|
+
# more easily pass in and retrieve processed records.
|
40
|
+
#
|
41
|
+
# @return [UnitTestDriver]
|
42
|
+
def driver
|
43
|
+
@driver ||= UnitTestDriver.new(dataflow, settings)
|
44
|
+
end
|
45
|
+
|
46
|
+
# No need to load commandline arguments when we are testing
|
47
|
+
# There are other mechanisms for passing them in, plus
|
48
|
+
# RSpec goes into an infinite loop if you load a spec file
|
49
|
+
# from within a spec file
|
50
|
+
def load_args
|
51
|
+
end
|
52
|
+
|
53
|
+
# Do nothing. This prevents control flow within the Ruby
|
54
|
+
# interpreter from staying within this runner, as it would
|
55
|
+
# ordinarly do for `wu-local`.
|
56
|
+
def run
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Wukong
|
2
|
+
class Processor
|
3
|
+
|
4
|
+
# A widget that yields whatever you instantiate it with.
|
5
|
+
#
|
6
|
+
# This is most useful when you have a small but predictable input
|
7
|
+
# that you don't want or can't pass via usual input channels like
|
8
|
+
# STDIN.
|
9
|
+
#
|
10
|
+
# @example Works just like you think on the command line
|
11
|
+
#
|
12
|
+
# $ echo something else | wu-local echo --input=hello
|
13
|
+
# hello
|
14
|
+
#
|
15
|
+
# @example Pass some fixed input to your downstream code.
|
16
|
+
#
|
17
|
+
# # my_flow.rb
|
18
|
+
# Wukong.dataflow(:my_flow) do
|
19
|
+
# echo(input: {key: 'value'}) | my_proc | ...
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# This differs from from the `:identity` processor because it
|
23
|
+
# doesn't pass on what it receives but what you instantiate it
|
24
|
+
# with.
|
25
|
+
#
|
26
|
+
# @see Identity
|
27
|
+
class Echo < Processor
|
28
|
+
|
29
|
+
description <<EOF
|
30
|
+
A widget that yields whatever you instantiate it with.
|
31
|
+
|
32
|
+
This is most useful when you have a small but predictable input
|
33
|
+
that you don't want or can't pass via usual input channels like
|
34
|
+
STDIN.
|
35
|
+
|
36
|
+
Works just like you think on the command line (the process won't terminate)
|
37
|
+
|
38
|
+
$ echo something else | wu-local echo --input=hello
|
39
|
+
hello
|
40
|
+
EOF
|
41
|
+
|
42
|
+
field :input, Whatever, :default => nil, :doc => "The record to echo"
|
43
|
+
|
44
|
+
# Yields the `input` no matter what you pass it.
|
45
|
+
#
|
46
|
+
# @param [Object] _ the new input record which is ignored
|
47
|
+
# @yield [input]
|
48
|
+
# @yieldparam [Object] input the original input
|
49
|
+
def process _
|
50
|
+
yield input
|
51
|
+
end
|
52
|
+
register
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require_relative('utils')
|
2
|
+
|
3
|
+
module Wukong
|
4
|
+
class Processor
|
5
|
+
|
6
|
+
# A widget that extracts parts of incoming records.
|
7
|
+
#
|
8
|
+
# This widget can extract part of the following kinds of objects:
|
9
|
+
#
|
10
|
+
# - Hash
|
11
|
+
# - Array
|
12
|
+
# - JSON string
|
13
|
+
# - delimited string ("\t" or "," or other)
|
14
|
+
# - models
|
15
|
+
#
|
16
|
+
# In each case it will attempt to appropriately parse its
|
17
|
+
# <tt>:part</tt> argument.
|
18
|
+
#
|
19
|
+
# @example Extracting a column from an input TSV record on the command-line
|
20
|
+
#
|
21
|
+
# $ cat input
|
22
|
+
# snap crackle pop
|
23
|
+
# 1 2 3
|
24
|
+
# $ cat input | wu-local extract --part=2
|
25
|
+
# crackle
|
26
|
+
# pop
|
27
|
+
#
|
28
|
+
# @example Extracting a column from delimited data with a different delimiter
|
29
|
+
#
|
30
|
+
# $ cat input
|
31
|
+
# snap,crackle,pop
|
32
|
+
# 1,2,3
|
33
|
+
# $ cat input | wu-local extract --part=2 --delimiter=,
|
34
|
+
# crackle
|
35
|
+
# pop
|
36
|
+
#
|
37
|
+
# @example Extracting a field from within some JSON record on the command-line
|
38
|
+
#
|
39
|
+
# $ cat input
|
40
|
+
# {"id": 1, "text": "hi there"}
|
41
|
+
# {"id": 2, "text": "goodbye"}
|
42
|
+
# $ cat input | wu-local extract --part="text"
|
43
|
+
# hi there
|
44
|
+
# goodbye
|
45
|
+
#
|
46
|
+
# This even works on nested keys using a dot ('.') to separate the
|
47
|
+
# keys:
|
48
|
+
#
|
49
|
+
# @example Extracting a nested field from within some JSON record on the command-line
|
50
|
+
#
|
51
|
+
# $ cat input
|
52
|
+
# {"id": 1, {"data": {"text": "hi there"}}
|
53
|
+
# {"id": 2, {"data": {"text": "goodbye"}}
|
54
|
+
# $ cat input | wu-local extract --part="data.text"
|
55
|
+
# hi there
|
56
|
+
# goodbye
|
57
|
+
#
|
58
|
+
# Objects like Hashes, Arrays, and models, which would have to
|
59
|
+
# serialize within a command-line flow, can also be extracted from
|
60
|
+
# within a dataflow:
|
61
|
+
#
|
62
|
+
# @example Extracting a field from within a Hash in a dataflow
|
63
|
+
#
|
64
|
+
# Wukong.dataflow(:uses_extract) do
|
65
|
+
# ... | extract(part: 'data.text') | ...
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# @see DynamicGet
|
69
|
+
class Extract < Processor
|
70
|
+
include DynamicGet
|
71
|
+
|
72
|
+
description <<EOF
|
73
|
+
This processor will pass extracted parts of input records.
|
74
|
+
|
75
|
+
It can be used to extract a field from a delimited input
|
76
|
+
|
77
|
+
$ cat input
|
78
|
+
snap crackle pop
|
79
|
+
a b c
|
80
|
+
$ cat input | wu-local extract --part=2
|
81
|
+
crackle
|
82
|
+
b
|
83
|
+
|
84
|
+
The default separator is a tab character but you can specify this as
|
85
|
+
well
|
86
|
+
|
87
|
+
$ cat input
|
88
|
+
snap,crackle,pop
|
89
|
+
a,b,c
|
90
|
+
$ cat input | wu-local extract --part=2 --separator=,
|
91
|
+
crackle
|
92
|
+
b
|
93
|
+
|
94
|
+
It can also be used on JSON records, even those with nested fields
|
95
|
+
|
96
|
+
$ cat input
|
97
|
+
{"id": 1, {"data": {"text": "hi there"}}
|
98
|
+
{"id": 2, {"data": {"text": "goodbye"}}
|
99
|
+
$ cat input | wu-local extract --part=id
|
100
|
+
1
|
101
|
+
2
|
102
|
+
$ cat input | wu-local extract --part=data.text
|
103
|
+
hi there
|
104
|
+
goodbye
|
105
|
+
|
106
|
+
If no --part argument is given, the original record will be yielded.
|
107
|
+
EOF
|
108
|
+
|
109
|
+
field :part, Whatever, :default => nil, :doc => "Part of the record to extract"
|
110
|
+
|
111
|
+
# Extract a `part` of a `record`.
|
112
|
+
#
|
113
|
+
# @param [Object] record
|
114
|
+
# @yield [part]
|
115
|
+
# @yieldparam [Object] part the part extracted from the record
|
116
|
+
def process record
|
117
|
+
yield get(self.part, record)
|
118
|
+
end
|
119
|
+
register
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,452 @@
|
|
1
|
+
module Wukong
|
2
|
+
class Processor
|
3
|
+
|
4
|
+
# A widget which filters input records according to some
|
5
|
+
# criterion.
|
6
|
+
class Filter < Processor
|
7
|
+
|
8
|
+
description <<EOF
|
9
|
+
A processor which filters input records according to some criterion.
|
10
|
+
EOF
|
11
|
+
|
12
|
+
# Process a `record` by yielding it only if it should be
|
13
|
+
# selected by this filter.
|
14
|
+
#
|
15
|
+
# @param [Object] record an input record
|
16
|
+
# @yield [record] yielded if this record should pass the filter
|
17
|
+
# @yieldparam [Object] record
|
18
|
+
# @see #select?
|
19
|
+
# @see #reject?
|
20
|
+
def process(record)
|
21
|
+
yield(record) if select?(record)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Should the given `record` be passed by this filter?
|
25
|
+
#
|
26
|
+
# @param [Object] record
|
27
|
+
# @return [true, false]
|
28
|
+
# @see #reject?
|
29
|
+
def select?(record)
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
# Should the given `record` be rejected by this filter?
|
34
|
+
#
|
35
|
+
# @param [Object] record
|
36
|
+
# @return [true, false]
|
37
|
+
# @see #select?
|
38
|
+
def reject?(record)
|
39
|
+
not select?(record)
|
40
|
+
end
|
41
|
+
|
42
|
+
register
|
43
|
+
end
|
44
|
+
|
45
|
+
# A widget which passes all records, i.e. - it acts just like
|
46
|
+
# `cat`.
|
47
|
+
#
|
48
|
+
# @example Pass all records unmodified on the command line
|
49
|
+
#
|
50
|
+
# $ cat input
|
51
|
+
# 1
|
52
|
+
# 2
|
53
|
+
# 3
|
54
|
+
# $ cat input | wu-local identity
|
55
|
+
# 1
|
56
|
+
# 2
|
57
|
+
# 3
|
58
|
+
#
|
59
|
+
# @example Pass all records unmodified in a dataflow
|
60
|
+
#
|
61
|
+
# Wukong.dataflow(:uses_identity) do
|
62
|
+
# ... | identity | ...
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# @see Filter
|
66
|
+
# @see Null
|
67
|
+
class Identity < Filter
|
68
|
+
description "This processor passes all records unmodified."
|
69
|
+
register
|
70
|
+
end
|
71
|
+
|
72
|
+
# A widget which doesn't pass any records, i.e. - it acts just
|
73
|
+
# like <tt>/dev/null</tt>.
|
74
|
+
#
|
75
|
+
# @example Filter all records on the command line
|
76
|
+
#
|
77
|
+
# $ cat input
|
78
|
+
# 1
|
79
|
+
# 2
|
80
|
+
# 3
|
81
|
+
# $ cat input | wu-local null
|
82
|
+
#
|
83
|
+
# @example Filter all records from a dataflow
|
84
|
+
#
|
85
|
+
# Wukong.dataflow(:uses_null) do
|
86
|
+
# ... | null | ...
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# @see Filter
|
90
|
+
# @see All
|
91
|
+
class Null < Filter
|
92
|
+
|
93
|
+
description "This processor acts as a filter which passes no records at all."
|
94
|
+
|
95
|
+
# Prevents any records from passing because it always returns
|
96
|
+
# `false`.
|
97
|
+
#
|
98
|
+
# @param [Object] record
|
99
|
+
# @return false
|
100
|
+
def select? record
|
101
|
+
false
|
102
|
+
end
|
103
|
+
register
|
104
|
+
end
|
105
|
+
|
106
|
+
# A widget which only passes records if they match a regular
|
107
|
+
# expression.
|
108
|
+
#
|
109
|
+
# @example Passing records which match a given expression on the command-line
|
110
|
+
#
|
111
|
+
# $ cat input
|
112
|
+
# apple
|
113
|
+
# banana
|
114
|
+
# cat
|
115
|
+
# $ cat input | wu-local regexp --match='^a'
|
116
|
+
# apple
|
117
|
+
#
|
118
|
+
# @example Passing records which match a given expression in a dataflow
|
119
|
+
#
|
120
|
+
# Wukong.dataflow(:uses_regexp) do
|
121
|
+
# ... | regexp(match: /^a/) | ...
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# @see Filter
|
125
|
+
# @see NotRegexpFilter
|
126
|
+
class RegexpFilter < Filter
|
127
|
+
|
128
|
+
description <<EOF
|
129
|
+
This processor only passes records which match against a given regular
|
130
|
+
expression.
|
131
|
+
|
132
|
+
$ cat input
|
133
|
+
apple
|
134
|
+
banana
|
135
|
+
cat
|
136
|
+
$ cat input | wu-local regexp --match='^a'
|
137
|
+
apple
|
138
|
+
|
139
|
+
If no --match argument is given, all records will be passed.
|
140
|
+
EOF
|
141
|
+
|
142
|
+
field :match, Regexp, :doc => "Regular expression to match against"
|
143
|
+
|
144
|
+
# Selects a `record` only if it matches this widget's `match`
|
145
|
+
# field.
|
146
|
+
#
|
147
|
+
# @param [Object] record
|
148
|
+
# @return [true, false]
|
149
|
+
def select?(record)
|
150
|
+
return true unless match
|
151
|
+
match =~ record.to_s
|
152
|
+
end
|
153
|
+
register(:regexp)
|
154
|
+
end
|
155
|
+
|
156
|
+
# A widget which only passes records if they *don't* match a
|
157
|
+
# regular expression.
|
158
|
+
#
|
159
|
+
# @example Passing records which don't match a given expression on the command-line
|
160
|
+
#
|
161
|
+
# $ cat input
|
162
|
+
# apple
|
163
|
+
# banana
|
164
|
+
# cat
|
165
|
+
# $ cat input | wu-local not_regexp --match='^a'
|
166
|
+
# banana
|
167
|
+
# cat
|
168
|
+
#
|
169
|
+
# @example Passing records which don't match a given expression in a dataflow
|
170
|
+
#
|
171
|
+
# Wukong.dataflow(:uses_not_regexp) do
|
172
|
+
# ... | not_regexp(match: /^a/) | ...
|
173
|
+
# end
|
174
|
+
#
|
175
|
+
# @see Filter
|
176
|
+
# @see NotRegexpFilter
|
177
|
+
class NotRegexpFilter < RegexpFilter
|
178
|
+
|
179
|
+
description <<EOF
|
180
|
+
This processor only passes records which fail to match against a given
|
181
|
+
regular expression.
|
182
|
+
|
183
|
+
$ cat input
|
184
|
+
apple
|
185
|
+
banana
|
186
|
+
cat
|
187
|
+
$ cat input | wu-local not_regexp --match='^a'
|
188
|
+
banana
|
189
|
+
cat
|
190
|
+
|
191
|
+
If no --match argument is given, all records will be passed.
|
192
|
+
EOF
|
193
|
+
|
194
|
+
# Select a `record` only if it <b>doesn't</b> match this
|
195
|
+
# widget's `match` field.
|
196
|
+
#
|
197
|
+
# @param [Object] record
|
198
|
+
# @return [true, false]
|
199
|
+
def select?(record)
|
200
|
+
return true unless match
|
201
|
+
not match =~ record.to_s
|
202
|
+
end
|
203
|
+
register(:not_regexp)
|
204
|
+
end
|
205
|
+
|
206
|
+
# A widget which only lets a certain number of records through.
|
207
|
+
#
|
208
|
+
# @example Letting the first 3 records through on the command-line
|
209
|
+
#
|
210
|
+
# $ cat input
|
211
|
+
# 1
|
212
|
+
# 2
|
213
|
+
# 3
|
214
|
+
# 4
|
215
|
+
# $ cat input | wu-local limit --max=3
|
216
|
+
# 1
|
217
|
+
# 2
|
218
|
+
# 3
|
219
|
+
#
|
220
|
+
# @example Letting the first 3 records through in a dataflow
|
221
|
+
#
|
222
|
+
# Wukong.dataflow(:uses_limit) do
|
223
|
+
# ... | limit(max: 3) | ...
|
224
|
+
# end
|
225
|
+
#
|
226
|
+
# @see Filter
|
227
|
+
class Limit < Filter
|
228
|
+
|
229
|
+
description <<EOF
|
230
|
+
This processor passes a certain number of records and then stops
|
231
|
+
passing any, acting as a limit.
|
232
|
+
|
233
|
+
$ cat input
|
234
|
+
1
|
235
|
+
2
|
236
|
+
3
|
237
|
+
$ cat input | wu-local limit --max=2
|
238
|
+
1
|
239
|
+
2
|
240
|
+
|
241
|
+
If no --max argument is given, all records will be passed.
|
242
|
+
EOF
|
243
|
+
|
244
|
+
field :max, Integer, :doc => "Maximum number of records to let pass"
|
245
|
+
|
246
|
+
# The current record count.
|
247
|
+
attr_accessor :count
|
248
|
+
|
249
|
+
# Initializes the record count to zero.
|
250
|
+
def setup
|
251
|
+
self.count = 0
|
252
|
+
end
|
253
|
+
|
254
|
+
# Select a record only if we're below the max count. Increments
|
255
|
+
# the count for this widget.
|
256
|
+
#
|
257
|
+
# @param [Object] record
|
258
|
+
# @return [true, false]
|
259
|
+
def select?(record)
|
260
|
+
keep = (max ? @count < max : true)
|
261
|
+
@count += 1
|
262
|
+
keep
|
263
|
+
end
|
264
|
+
register
|
265
|
+
end
|
266
|
+
|
267
|
+
# A widget which samples a certain fraction of input records.
|
268
|
+
#
|
269
|
+
# @example Sampling records on the command line
|
270
|
+
#
|
271
|
+
# $ cat input
|
272
|
+
# 1
|
273
|
+
# 2
|
274
|
+
# 3
|
275
|
+
# 4
|
276
|
+
# $ cat input | wu-local sample --fraction=0.5
|
277
|
+
# 1
|
278
|
+
# 3
|
279
|
+
#
|
280
|
+
# @example Sampling records in a dataflow
|
281
|
+
#
|
282
|
+
# Wukong.dataflow(:uses_sample) do
|
283
|
+
# ... | sample(fraction: 0.5) ...
|
284
|
+
# end
|
285
|
+
#
|
286
|
+
# @see Filter
|
287
|
+
# @see Limit
|
288
|
+
class Sample < Filter
|
289
|
+
|
290
|
+
description <<EOF
|
291
|
+
This processor will pass input records with a certain frequency,
|
292
|
+
acting as a random sampler.
|
293
|
+
|
294
|
+
$ cat input
|
295
|
+
1
|
296
|
+
2
|
297
|
+
3
|
298
|
+
4
|
299
|
+
$ cat input | wu-local sample --fraction=0.5
|
300
|
+
1
|
301
|
+
4
|
302
|
+
|
303
|
+
If no --fraction is given, all records will be passed.
|
304
|
+
EOF
|
305
|
+
|
306
|
+
field :fraction, Float, :default => 1.0, :doc => "Fraction of records to let pass. Must be between 0 and 1.0"
|
307
|
+
|
308
|
+
# Selects a `record` randomly, with a probability given the the
|
309
|
+
# `fraction` for this widget.
|
310
|
+
#
|
311
|
+
# @param [Object] record
|
312
|
+
# @return [true, false]
|
313
|
+
def select?(record)
|
314
|
+
rand() < fraction
|
315
|
+
end
|
316
|
+
register
|
317
|
+
end
|
318
|
+
|
319
|
+
# A widget useful for creating filters on the fly in a dataflow.
|
320
|
+
#
|
321
|
+
# When writing a filtering processor out as a class, just use the
|
322
|
+
# DSL for creating processors:
|
323
|
+
#
|
324
|
+
# @example Creating a select filter the usual way
|
325
|
+
#
|
326
|
+
# Wukong.processor(:my_filter, Wukong::Processor::Filter) do
|
327
|
+
# def select? record
|
328
|
+
# record.length > 3
|
329
|
+
# end
|
330
|
+
# end
|
331
|
+
#
|
332
|
+
# When in a dataflow, sometimes it's easier to create a processor
|
333
|
+
# like this on the fly.
|
334
|
+
#
|
335
|
+
# @example Creating a select filter on the fly in a dataflow
|
336
|
+
#
|
337
|
+
# Wukong.dataflow(:my_flow) do
|
338
|
+
# ... | select { |record| record.length > 3 } | ...
|
339
|
+
# end
|
340
|
+
#
|
341
|
+
# @see Filter
|
342
|
+
# @see Reject
|
343
|
+
class Select < Filter
|
344
|
+
|
345
|
+
# Selects the given `record` by delegating to the
|
346
|
+
# `perform_action` method, which will automatically be
|
347
|
+
# populating by the block used to create this filter in the
|
348
|
+
# dataflow DSL.
|
349
|
+
#
|
350
|
+
# @param [Object] record
|
351
|
+
# @return [true, false]
|
352
|
+
# @see Processor#perform_action
|
353
|
+
def select?(record)
|
354
|
+
perform_action(record)
|
355
|
+
end
|
356
|
+
register
|
357
|
+
end
|
358
|
+
|
359
|
+
# A widget useful for creating filters on the fly in a dataflow.
|
360
|
+
#
|
361
|
+
# @see Select
|
362
|
+
class Reject < Filter
|
363
|
+
# Rejects the given `record` by delegating to the
|
364
|
+
# `perform_action` method.
|
365
|
+
#
|
366
|
+
# @param [Object] record
|
367
|
+
# @return [true, false]
|
368
|
+
# @see Processor#perform_action
|
369
|
+
def select?(record)
|
370
|
+
not perform_action(record)
|
371
|
+
end
|
372
|
+
register
|
373
|
+
end
|
374
|
+
|
375
|
+
# Emit only the first `n` records.
|
376
|
+
#
|
377
|
+
# @see Filter
|
378
|
+
class Head < Filter
|
379
|
+
|
380
|
+
field :n, Integer, :default => 10, :doc => "Number of records to let pass"
|
381
|
+
|
382
|
+
# The current record count.
|
383
|
+
attr_accessor :count
|
384
|
+
|
385
|
+
# Initializes the record count to zero.
|
386
|
+
def setup
|
387
|
+
self.count = 0
|
388
|
+
end
|
389
|
+
|
390
|
+
# Select a record only if we're below the maximum number of
|
391
|
+
# records.
|
392
|
+
#
|
393
|
+
# @param [Object] record
|
394
|
+
# @return [true, false]
|
395
|
+
def select?(record)
|
396
|
+
keep = @count < n
|
397
|
+
@count += 1
|
398
|
+
keep
|
399
|
+
end
|
400
|
+
register
|
401
|
+
end
|
402
|
+
|
403
|
+
# Skip the first `n` records.
|
404
|
+
#
|
405
|
+
# Works slightly differently than the UNIX `tail` command which
|
406
|
+
# prints the last `n` records. This notion is less useful in a
|
407
|
+
# streaming context, so think of this filter as the equivalent of
|
408
|
+
# `tail -n+`.
|
409
|
+
#
|
410
|
+
# @see Filter
|
411
|
+
class Tail < Filter
|
412
|
+
|
413
|
+
field :n, Integer, :default => 0, :doc => "Number of records to skip before letting records pass"
|
414
|
+
|
415
|
+
# The current record count
|
416
|
+
attr_accessor :count
|
417
|
+
|
418
|
+
# Initializes the record count to zero.
|
419
|
+
def setup
|
420
|
+
self.count = 0
|
421
|
+
end
|
422
|
+
|
423
|
+
# Select a record only if we've already skipped the first `n`
|
424
|
+
# records.
|
425
|
+
#
|
426
|
+
# @param [Object]
|
427
|
+
# @return [true, false]
|
428
|
+
def select?(record)
|
429
|
+
keep = (@count >= n)
|
430
|
+
@count += 1
|
431
|
+
keep
|
432
|
+
end
|
433
|
+
register
|
434
|
+
end
|
435
|
+
|
436
|
+
# Select a record only if it is non-nil.
|
437
|
+
#
|
438
|
+
# @see Filter
|
439
|
+
class Compact < Filter
|
440
|
+
|
441
|
+
# Select a record only if it is non-nil.
|
442
|
+
#
|
443
|
+
# @param [Object] record
|
444
|
+
# @return [true, false]
|
445
|
+
def select?(record)
|
446
|
+
! record.nil?
|
447
|
+
end
|
448
|
+
register
|
449
|
+
end
|
450
|
+
|
451
|
+
end
|
452
|
+
end
|