DBMAPREDUCE(1)
π DBMAPREDUCE(1) β like, the dbmapreduce manual, but make it make sense π
okay bestie, gather round, because this is a perl manual page from the User Contributed Perl Documentation section and it's genuinely iconic once you get past the formatting. no cap. π¦
β¨ NAME β¨
dbmapreduce β reduce all input rows with the same key. that's it, that's the whole vibe. group by key, then smoosh each group down into one thing. π
πΈ SYNOPSIS πΈ
dbmapreduce [-dMS] [-k KeyField] [-f CodeFile] [-C Filtercode] [--] [ReduceCommand [ReduceArguments...]]
π DESCRIPTION π
so here's the whole deal: you group your input data by KeyField, and then you apply a function β the "reducer" β to each one of those groups. slay.
the reduce function can be either:
- an external program, which you give via
ReduceCommandandReduceArguments, or - a Perl subroutine, which you give via
CodeFileorFilterCode.
and listen: if a -- shows up before the reduce command, then the arguments after the -- get passed straight to that command. periodt. π«Ά
π§ Grouping (The Mapper) π§
by default, the KeyField is literally just the first field in the row. but unlike Hadoop streaming (iykyk), the -k KeyField option can explicitly name where the key lives β any column of each input row. main character energy for real.
by default we sort the data to make sure it's actually grouped by key. if your input is already grouped, the -S option avoids paying that cost. πΈ
π₯ The Reducer π₯
reduce functions default to being shell commands. but with -C, you can use arbitrary Perl code instead (peep the -C option below for the deets). and the -f option is super useful when you want to shove complex Perl code somewhere other than the command line.
finally, as a special case: if there are, like, no rows of input at all, the reducer gets invoked exactly once with the empty value (if it's an external reducer) or with undef (if it's a subroutine). it's expected to generate the output header, and it may generate no data rows itself, or a null data row of its choosing. it's giving "you're on your own, babe". π
π Output π
for non-multi-key-aware reducers, we add the KeyField used for each Reduce into the output stream. (if the reducer passes the key itself, we trust that it gives a correct value. trust fall. π€) we also insure that the output field separator is the same as the input field separator.
heads up bestie: adding the key and adjusting the output field separator is not possible for non-multi-key-aware reducers. that's just how it is.
π Comparison to Related Work π
this program thus implements Google-style map/reduce, but executed sequentially. no cluster, no chaos, just vibes and one machine. β¨
for input, those other systems include a map function and apply it to input data to generate the key. we assume this key generation (the map function) has already happened ahead of time. you do you, we'll do the reducing.
we also allow the grouping key to be in any column. Hadoop Streaming requires it to be in the first column. rude of them, honestly.
by default, the reducer gets exactly (and only) one key. this invariant is stronger than Google and Hadoop β they both pass multiple keys to the reducer, insuring that each key is grouped together. with the -M option, we also pass multiple multiple groups to the reducer. π₯
unlike those systems, with the -S option we do not require the groups arrive in any particular order β just that they be grouped together. (they guarantee they arrive in lexically sorted order.) however, with -S we create lexical ordering. π
with --prepend-key we insure that the KeyField is in the output stream; other systems do not enforce this. we're just built different.
π Assumptions and requirements π
- by default, data can be provided in arbitrary order, and the program consumes O(number of unique tags) memory and O(size of data) disk space.
- with the
-Soption, data must arrive grouped by tags (not necessarily sorted), and the program consumes O(number of tags) memory and no disk space. the program will check and abort if this precondition is not met. π¨ - with two
-S's, the program consumes O(1) memory, but doesn't verify that the data-arrival precondition is met. so likeβ¦ you're on your honor. π
the field separators of the input and the output can now be different (early versions of this tool prohibited such variation β glow-up achieved π). with --copy-fs we copy the input field separator to the output, but only for non-multi-key-aware reducers. (this used to be done automatically.) alternatively, one can specify the output field separator with --fieldseparator, in which case the output had better generate that format. an explicit --fieldseparator takes priority over --copy-fs. no take-backs.
π Known bugs π
as of 2013-09-21, we don't verify key order with options -M -S. it's a known little gremlin. don't say we didn't warn you. π
β OPTIONS β
-kor--key KeyFieldβ specify which column is the key for grouping (default: the first column).
note that dbmapreduce can only operate on one column as the key. to group on the combination of multiple columns, one must merge them, perhaps with dbcolmerge. β¨
-Sor--pre-sortedβ assume data is already grouped by tag. provided twice, it removes the validation of this assertion. (yes, twice. two-S's. we love a double tap. π )-Mor--multiple-okβ assume theReduceCommandcan handle multiple grouped keys, and theReduceCommandis responsible for outputting the with each output row. (by default, a separateReduceCommandis run for each key, anddbmapreduceadds the key to each output row.)-Kor--pass-current-keyβ pass the current key as an argument to the external, non-map-awareReduceCommand. this is only done optionally since some external commands do not expect an extra argument. (internal, non-map-aware Perl reducers are always given the current key as an argument.) π«Ά--prepend-keyβ add the current key into the reducer output for non-multi-key-aware reducers only. not done by default.--copy-fsor--copy-fieldseparatorβ change the field separator of a non-multi-key-aware reducers to match the input's field separator. not done by default.--parallelism=Nor-j Nβ allow up toNreducers to run in parallel. default is the number of CPUs in the machine. π₯-For--fsor--fieldseparator Sβ specify the field (column) separator asS. seedbfilealterfor valid field separators.-C FILTER-CODEor--filter-code=FILTER-CODEβ provideFILTER-CODE, Perl code that generates and returns aFsdb::Filterobject that implements the reduce function. the provided code should be an anonymous sub that creates a Fsdb Filter that implements the reduce object.
the reduce object will then be called with --input and --output parameters that hook it into a the reduce with queues.
one sample fragment that works is just:
dbcolstats(qw(--nolog duration))
so this command:
cat DATA/stats.fsdb | \
dbmapreduce -k experiment -C 'dbcolstats(qw(--nolog duration))'
is the same as the example
cat DATA/stats.fsdb | \
dbmapreduce -k experiment -- dbcolstats duration
except that with -C there is no forking and so things run faster. β‘
if dbmapreduce is invoked from within Perl, then one can use a code SUB as well:
dbmapreduce(-k => 'experiment', -C => sub {
dbcolstats(qw(--nolong duration)) });
the reduce object must consume all input as a Fsdb stream, and close the output Fsdb stream. (if this assumption is not met the map/reduce will be aborted.) π¨
for non-map-reduce-aware filters, when the filter-generator code runs, $_[0] will be the current key.
-f CODE-FILEor--code-file=CODE-FILEβ includesCODE-FILEin the program. this option is useful for more complicated perl reducer functions.
thus, if reducer.pl has the code:
sub make_reducer {
my($current_key) = @_;
dbcolstats(qw(--nolog duration));
}
then the command
cat DATA/stats.fsdb | \
dbmapreduce -k experiment -f reducer.pl -C make_reducer
does the same thing as the example. cute, right? π
-wor--warningsβ enable warnings in user supplied code. warnings are issued if an external reducer fails to consume all input. (default to include warnings.)-T TmpDirβ where to put tmp files. also uses environment variableTMPDIR, if-Tis not specified. default is/tmp.
this module also supports the standard fsdb options:
-dβ enable debugging output.-ior--input InputSourceβ read fromInputSource, typically a file name, or-for standard input, or (if in Perl) aIO::Handle,Fsdb::IOorFsdb::BoundedQueueobjects.-oor--output OutputDestinationβ write toOutputDestination, typically a file name, or-for standard output, or (if in Perl) aIO::Handle,Fsdb::IOorFsdb::BoundedQueueobjects.--autorunor--noautorunβ by default, programs process automatically, butFsdb::Filterobjects in Perl do not run until you invoke therun()method. the--(no)autorunoption controls that behavior within Perl.--header Hβ useHas the full Fsdb header, rather than reading a header from then input.--helpβ show help.--manβ show full manual.
π¦ SAMPLE USAGE π¦
π Input:
#fsdb experiment duration
ufs_mab_sys 37.2
ufs_mab_sys 37.3
ufs_rcp_real 264.5
ufs_rcp_real 277.9
πΈ Command:
cat DATA/stats.fsdb | \
dbmapreduce --prepend-key -k experiment -- dbcolstats duration
π Output:
#fsdb experiment mean stddev pct_rsd conf_range conf_low conf_high conf_pct sum sum_squared min max n
ufs_mab_sys 37.25 0.070711 0.18983 0.6353 36.615 37.885 0.95 74.5 2775.1 37.2 37.3 2
ufs_rcp_real 271.2 9.4752 3.4938 85.13 186.07 356.33 0.95 542.4 1.4719e+05 264.5 277.9 2
# | dbmapreduce -k experiment dbstats duration
π§ SEE ALSO π§
Fsdb. dbmultistats dbrowsplituniq
π AUTHOR and COPYRIGHT π
Copyright (C) 1991-2024 by John Heidemann <johnh@isi.edu>
this program is distributed under terms of the GNU general public license, version 2. see the file COPYING with the distribution for details.
so yeah, that's the whole thing. perl v5.32.1, dated 2024-08-02, DBMAPREDUCE(1) β she's serving documentation realness and honestly? she ate. ππ
β¨
π¦ where this page comes from
- epel 9: perl-Fsdb 2:3.9-1.el9 Β· GPLv2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms