Class: Wukong::Processor::Sort
- Inherits:
-
Accumulator
- Object
- Hanuman::Stage
- Wukong::Processor
- Accumulator
- Wukong::Processor::Sort
- Includes:
- DynamicGet
- Defined in:
- lib/wukong/widget/reducers/sort.rb
Overview
Sorts input records.
For many use cases you're better off using native tools like
/bin/sort
because they are faster and already do what you
need.
Other times, you need something that can introspect more on its input:
The sort widget is useful for modeling Hadoop jobs, but don't forget that Hadoop does its own sorting, so the sort widget doesn't belong in your map/reduce jobs.
Constant Summary
Constants inherited from Wukong::Processor
Instance Attribute Summary
Attributes inherited from Accumulator
Attributes included from Hanuman::StageInstanceMethods
Instance Method Summary collapse
-
#accumulate(record) ⇒ Object
Stores the
record
for later sorting. -
#compare(x, y) ⇒ 1, ...
Compare records
x
andy
using their sortable parts. -
#finalize {|record| ... } ⇒ Object
Sorts all the stored records and yields in one sorted according to the field in the right order.
-
#get_key(record) ⇒ :__first__group__
Keeps all the records in a single group so they can be sorted.
-
#setup ⇒ Object
Intializes the array of records that will hold all the values.
-
#sortable(record) ⇒ Object
Extracts the sortable part of the input
record
.
Methods included from DynamicGet
Methods inherited from Accumulator
Methods inherited from Wukong::Processor
configure, description, #perform_action, #process, #receive_action, #stop
Methods included from Logging
Methods inherited from Hanuman::Stage
Methods included from Hanuman::StageClassMethods
#builder, #label, #register, #set_builder
Methods included from Hanuman::StageInstanceMethods
#add_stage_link, #linkable_name, #root
Instance Method Details
#accumulate(record) ⇒ Object
Stores the record
for later sorting.
138 139 140 |
# File 'lib/wukong/widget/reducers/sort.rb', line 138 def accumulate record @records << record end |
#compare(x, y) ⇒ 1, ...
Compare records x
and y
using their sortable parts.
Will use numeric sorting when asked.
168 169 170 171 172 173 174 175 |
# File 'lib/wukong/widget/reducers/sort.rb', line 168 def compare(x, y) a = (sortable(x) or return -1) b = (sortable(y) or return 1) if numeric a = a.to_f ; b = b.to_f end a <=> b end |
#finalize {|record| ... } ⇒ Object
Sorts all the stored records and yields in one sorted according to the field in the right order.
147 148 149 150 151 |
# File 'lib/wukong/widget/reducers/sort.rb', line 147 def finalize sorted = @records.sort{ |x, y| compare(x, y) } sorted.reverse! if reverse sorted.each{ |record| yield record } end |
#get_key(record) ⇒ :__first__group__
Keeps all the records in a single group so they can be sorted.
131 132 133 |
# File 'lib/wukong/widget/reducers/sort.rb', line 131 def get_key(record) :__first_group__ end |
#setup ⇒ Object
Intializes the array of records that will hold all the values.
122 123 124 125 |
# File 'lib/wukong/widget/reducers/sort.rb', line 122 def setup super() @records = [] end |
#sortable(record) ⇒ Object
Extracts the sortable part of the input record
.
157 158 159 |
# File 'lib/wukong/widget/reducers/sort.rb', line 157 def sortable(record) get(self.on, record) end |