wukong 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.textile +107 -0
- data/README.textile +166 -0
- data/bin/cutc +30 -0
- data/bin/cuttab +5 -0
- data/bin/greptrue +8 -0
- data/bin/hdp-cat +3 -0
- data/bin/hdp-catd +3 -0
- data/bin/hdp-du +81 -0
- data/bin/hdp-get +3 -0
- data/bin/hdp-kill +3 -0
- data/bin/hdp-ls +10 -0
- data/bin/hdp-mkdir +3 -0
- data/bin/hdp-mv +3 -0
- data/bin/hdp-parts_to_keys.rb +77 -0
- data/bin/hdp-ps +3 -0
- data/bin/hdp-put +3 -0
- data/bin/hdp-rm +11 -0
- data/bin/hdp-sort +29 -0
- data/bin/hdp-stream +29 -0
- data/bin/hdp-stream-flat +18 -0
- data/bin/hdp-sync +17 -0
- data/bin/hdp-wc +67 -0
- data/bin/md5sort +20 -0
- data/bin/tabchar +5 -0
- data/bin/uniqc +3 -0
- data/bin/wu-hist +3 -0
- data/bin/wu-lign +177 -0
- data/bin/wu-sum +30 -0
- data/doc/INSTALL.textile +41 -0
- data/doc/LICENSE.textile +107 -0
- data/doc/README-tutorial.textile +163 -0
- data/doc/README-wulign.textile +59 -0
- data/doc/README-wutils.textile +128 -0
- data/doc/TODO.textile +61 -0
- data/doc/UsingWukong-part1-setup.textile +2 -0
- data/doc/UsingWukong-part2-scraping.textile +2 -0
- data/doc/UsingWukong-part3-parsing.textile +132 -0
- data/doc/code/api_response_example.txt +20 -0
- data/doc/code/parser_skeleton.rb +38 -0
- data/doc/hadoop-nfs.textile +51 -0
- data/doc/hadoop-setup.textile +29 -0
- data/doc/index.textile +124 -0
- data/doc/intro_to_map_reduce/MapReduceDiagram.graffle +0 -0
- data/doc/links.textile +42 -0
- data/doc/overview.textile +91 -0
- data/doc/pig/PigLatinExpressionsList.txt +122 -0
- data/doc/pig/PigLatinReferenceManual.html +19134 -0
- data/doc/pig/PigLatinReferenceManual.txt +1640 -0
- data/doc/tips.textile +116 -0
- data/doc/usage.textile +102 -0
- data/doc/utils.textile +48 -0
- data/examples/README.txt +17 -0
- data/examples/and_pig/sample_queries.rb +128 -0
- data/examples/apache_log_parser.rb +53 -0
- data/examples/count_keys.rb +56 -0
- data/examples/count_keys_at_mapper.rb +57 -0
- data/examples/graph/adjacency_list.rb +74 -0
- data/examples/graph/breadth_first_search.rb +79 -0
- data/examples/graph/gen_2paths.rb +68 -0
- data/examples/graph/gen_multi_edge.rb +103 -0
- data/examples/graph/gen_symmetric_links.rb +53 -0
- data/examples/package-local.rb +100 -0
- data/examples/package.rb +96 -0
- data/examples/pagerank/README.textile +6 -0
- data/examples/pagerank/gen_initial_pagerank_graph.pig +57 -0
- data/examples/pagerank/pagerank.rb +88 -0
- data/examples/pagerank/pagerank_initialize.rb +46 -0
- data/examples/pagerank/run_pagerank.sh +19 -0
- data/examples/rank_and_bin.rb +173 -0
- data/examples/run_all.sh +47 -0
- data/examples/sample_records.rb +44 -0
- data/examples/size.rb +60 -0
- data/examples/word_count.rb +95 -0
- data/lib/wukong.rb +11 -0
- data/lib/wukong/and_pig.rb +62 -0
- data/lib/wukong/and_pig/README.textile +12 -0
- data/lib/wukong/and_pig/as.rb +37 -0
- data/lib/wukong/and_pig/data_types.rb +30 -0
- data/lib/wukong/and_pig/functions.rb +50 -0
- data/lib/wukong/and_pig/generate.rb +85 -0
- data/lib/wukong/and_pig/generate/variable_inflections.rb +82 -0
- data/lib/wukong/and_pig/junk.rb +51 -0
- data/lib/wukong/and_pig/operators.rb +8 -0
- data/lib/wukong/and_pig/operators/compound.rb +29 -0
- data/lib/wukong/and_pig/operators/evaluators.rb +7 -0
- data/lib/wukong/and_pig/operators/execution.rb +15 -0
- data/lib/wukong/and_pig/operators/file_methods.rb +29 -0
- data/lib/wukong/and_pig/operators/foreach.rb +98 -0
- data/lib/wukong/and_pig/operators/groupies.rb +212 -0
- data/lib/wukong/and_pig/operators/load_store.rb +65 -0
- data/lib/wukong/and_pig/operators/meta.rb +42 -0
- data/lib/wukong/and_pig/operators/relational.rb +129 -0
- data/lib/wukong/and_pig/pig_struct.rb +48 -0
- data/lib/wukong/and_pig/pig_var.rb +95 -0
- data/lib/wukong/and_pig/symbol.rb +29 -0
- data/lib/wukong/and_pig/utils.rb +0 -0
- data/lib/wukong/bad_record.rb +18 -0
- data/lib/wukong/boot.rb +47 -0
- data/lib/wukong/datatypes.rb +24 -0
- data/lib/wukong/datatypes/enum.rb +123 -0
- data/lib/wukong/dfs.rb +80 -0
- data/lib/wukong/encoding.rb +111 -0
- data/lib/wukong/extensions.rb +15 -0
- data/lib/wukong/extensions/array.rb +18 -0
- data/lib/wukong/extensions/blank.rb +93 -0
- data/lib/wukong/extensions/class.rb +189 -0
- data/lib/wukong/extensions/date_time.rb +24 -0
- data/lib/wukong/extensions/emittable.rb +82 -0
- data/lib/wukong/extensions/hash.rb +120 -0
- data/lib/wukong/extensions/hash_like.rb +119 -0
- data/lib/wukong/extensions/hashlike_class.rb +47 -0
- data/lib/wukong/extensions/module.rb +2 -0
- data/lib/wukong/extensions/pathname.rb +27 -0
- data/lib/wukong/extensions/string.rb +65 -0
- data/lib/wukong/extensions/struct.rb +17 -0
- data/lib/wukong/extensions/symbol.rb +11 -0
- data/lib/wukong/logger.rb +53 -0
- data/lib/wukong/models/graph.rb +27 -0
- data/lib/wukong/rdf.rb +104 -0
- data/lib/wukong/schema.rb +37 -0
- data/lib/wukong/script.rb +265 -0
- data/lib/wukong/script/hadoop_command.rb +111 -0
- data/lib/wukong/script/local_command.rb +14 -0
- data/lib/wukong/streamer.rb +13 -0
- data/lib/wukong/streamer/accumulating_reducer.rb +89 -0
- data/lib/wukong/streamer/base.rb +76 -0
- data/lib/wukong/streamer/count_keys.rb +30 -0
- data/lib/wukong/streamer/count_lines.rb +26 -0
- data/lib/wukong/streamer/filter.rb +20 -0
- data/lib/wukong/streamer/line_streamer.rb +12 -0
- data/lib/wukong/streamer/list_reducer.rb +20 -0
- data/lib/wukong/streamer/preprocess_with_pipe_streamer.rb +22 -0
- data/lib/wukong/streamer/rank_and_bin_reducer.rb +145 -0
- data/lib/wukong/streamer/set_reducer.rb +14 -0
- data/lib/wukong/streamer/struct_streamer.rb +48 -0
- data/lib/wukong/streamer/summing_reducer.rb +29 -0
- data/lib/wukong/streamer/uniq_by_last_reducer.rb +44 -0
- data/lib/wukong/typed_struct.rb +12 -0
- data/lib/wukong/wukong_class.rb +21 -0
- data/spec/bin/hdp-wc_spec.rb +4 -0
- data/spec/spec_helper.rb +0 -0
- data/wukong.gemspec +179 -0
- metadata +214 -0
data/bin/wu-sum
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'wukong'
|
3
|
+
require 'wukong/streamer/summing_reducer'
|
4
|
+
|
5
|
+
#
|
6
|
+
#
|
7
|
+
class Summer < Wukong::Streamer::SummingReducer
|
8
|
+
attr_accessor :sample_line
|
9
|
+
|
10
|
+
def initialize *args
|
11
|
+
self.summing_elements = [0]
|
12
|
+
super *args
|
13
|
+
end
|
14
|
+
|
15
|
+
def start! *args
|
16
|
+
self.sample_line = args
|
17
|
+
super *args
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_key *fields
|
21
|
+
fields.values_at(2,3)
|
22
|
+
end
|
23
|
+
|
24
|
+
def finalize
|
25
|
+
summing_elements.each{|idx| sample_line[idx] = sums[idx]}
|
26
|
+
yield sample_line
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Wukong::Script.new(Summer, nil).run
|
data/doc/INSTALL.textile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
---
|
2
|
+
layout: default
|
3
|
+
title: Install Notes
|
4
|
+
---
|
5
|
+
|
6
|
+
|
7
|
+
h1(gemheader). {{ site.gemname }} %(small):: install%
|
8
|
+
|
9
|
+
<notextile><div class="toggle"></notextile>
|
10
|
+
|
11
|
+
h2. Get the code
|
12
|
+
|
13
|
+
This code is available as a gem:
|
14
|
+
|
15
|
+
pre. $ sudo gem install mrflip-{{ site.gemname }}
|
16
|
+
|
17
|
+
You can instead download this project in either "zip":http://github.com/mrflip/{{ site.gemname }}/zipball/master or "tar":http://github.com/mrflip/{{ site.gemname }}/tarball/master formats.
|
18
|
+
|
19
|
+
Better yet, you can also clone the project with "Git":http://git-scm.com by running:
|
20
|
+
|
21
|
+
pre. $ git clone git://github.com/mrflip/{{ site.gemname }}
|
22
|
+
|
23
|
+
<notextile></div><div class="toggle"></notextile>
|
24
|
+
|
25
|
+
h2. Get the Dependencies
|
26
|
+
|
27
|
+
* Hadoop, pig
|
28
|
+
* extlib, YAML, JSON
|
29
|
+
* Optional gems: trollop, addressable/uri, htmlentities
|
30
|
+
|
31
|
+
|
32
|
+
<notextile></div><div class="toggle"></notextile>
|
33
|
+
|
34
|
+
h2. Setup
|
35
|
+
|
36
|
+
1. Allow Wukong to discover where his elephant friend lives: either
|
37
|
+
** set a $HADOOP_HOME environment variable,
|
38
|
+
** or create a file 'config/wukong-site.yaml' with a line that points to the top-level directory of your hadoop install: @:hadoop_home: /usr/local/share/hadoop@
|
39
|
+
2. Add wukong's @bin/@ directory to your $PATH, so that you may use its filesystem shortcuts.
|
40
|
+
|
41
|
+
<notextile></div></notextile>
|
data/doc/LICENSE.textile
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
---
|
2
|
+
layout: default
|
3
|
+
title: Apache License
|
4
|
+
---
|
5
|
+
|
6
|
+
|
7
|
+
h1(gemheader). {{ site.gemname }} %(small):: license%
|
8
|
+
|
9
|
+
|
10
|
+
The wukong code is __Copyright (c) 2009 Philip (flip) Kromer__
|
11
|
+
|
12
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
13
|
+
|
14
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
15
|
+
|
16
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
17
|
+
|
18
|
+
h1. Apache License
|
19
|
+
|
20
|
+
Apache License
|
21
|
+
Version 2.0, January 2004
|
22
|
+
http://www.apache.org/licenses/
|
23
|
+
|
24
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
25
|
+
|
26
|
+
<notextile><div class="toggle"></notextile>
|
27
|
+
|
28
|
+
h2. 1. Definitions.
|
29
|
+
|
30
|
+
* **License** shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
31
|
+
* **Licensor** shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
32
|
+
* **Legal Entity** shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, **control** means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
33
|
+
* **You** (or **Your**) shall mean an individual or Legal Entity exercising permissions granted by this License.
|
34
|
+
* **Source** form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
35
|
+
* **Object** form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
36
|
+
* **Work** shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
37
|
+
* **Derivative Works** shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
38
|
+
* **Contribution** shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, **submitted** means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
39
|
+
* **Contributor** shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
40
|
+
|
41
|
+
<notextile></div><div class="toggle"></notextile>
|
42
|
+
|
43
|
+
h2. 2. Grant of Copyright License.
|
44
|
+
|
45
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
46
|
+
|
47
|
+
|
48
|
+
<notextile></div><div class="toggle"></notextile>
|
49
|
+
|
50
|
+
h2. 3. Grant of Patent License.
|
51
|
+
|
52
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
53
|
+
|
54
|
+
<notextile></div><div class="toggle"></notextile>
|
55
|
+
|
56
|
+
h2. 4. Redistribution.
|
57
|
+
|
58
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
59
|
+
|
60
|
+
# You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
61
|
+
# You must cause any modified files to carry prominent notices stating that You changed the files; and
|
62
|
+
# You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
63
|
+
# If the Work includes a __NOTICE__ text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
64
|
+
|
65
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
66
|
+
|
67
|
+
<notextile></div><div class="toggle"></notextile>
|
68
|
+
|
69
|
+
h2. 5. Submission of Contributions.
|
70
|
+
|
71
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
72
|
+
|
73
|
+
<notextile></div><div class="toggle"></notextile>
|
74
|
+
|
75
|
+
h2. 6. Trademarks.
|
76
|
+
|
77
|
+
This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
78
|
+
|
79
|
+
<notextile></div><div class="toggle"></notextile>
|
80
|
+
|
81
|
+
h2. 7. Disclaimer of Warranty.
|
82
|
+
|
83
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
84
|
+
|
85
|
+
<notextile></div><div class="toggle"></notextile>
|
86
|
+
|
87
|
+
h2. 8. Limitation of Liability.
|
88
|
+
|
89
|
+
In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
90
|
+
|
91
|
+
<notextile></div><div class="toggle"></notextile>
|
92
|
+
|
93
|
+
h2. 9. Accepting Warranty or Additional Liability.
|
94
|
+
|
95
|
+
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
96
|
+
|
97
|
+
END OF TERMS AND CONDITIONS
|
98
|
+
|
99
|
+
<notextile></div><div class="toggle"></notextile>
|
100
|
+
|
101
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
102
|
+
|
103
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
104
|
+
|
105
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
106
|
+
|
107
|
+
<notextile></div></notextile>
|
@@ -0,0 +1,163 @@
|
|
1
|
+
Here's a script to count words in a text stream:
|
2
|
+
|
3
|
+
require 'wukong'
|
4
|
+
module WordCount
|
5
|
+
class Mapper < Wukong::Streamer::LineStreamer
|
6
|
+
# Emit each word in the line.
|
7
|
+
def process line
|
8
|
+
words = line.strip.split(/\W+/).reject(&:blank?)
|
9
|
+
words.each{|word| yield [word, 1] }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Reducer < Wukong::Streamer::ListReducer
|
14
|
+
def finalize
|
15
|
+
yield [ key, values.map(&:last).map(&:to_i).sum ]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Wukong::Script.new(
|
21
|
+
WordCount::Mapper,
|
22
|
+
WordCount::Reducer
|
23
|
+
).run # Execute the script
|
24
|
+
|
25
|
+
The first class, the Mapper, eats lines and craps @[word, count]@ records. Here
|
26
|
+
the /key/ is the word, and the /value/ is its count.
|
27
|
+
|
28
|
+
The second class is an example of an accumulated list reducer. The values for
|
29
|
+
each key are stacked up into a list; then the record(s) yielded by @#finalize@
|
30
|
+
are emitted.
|
31
|
+
|
32
|
+
Here's another way to write the Reducer: accumulate the count of each line, then
|
33
|
+
yield the sum in @#finalize@:
|
34
|
+
|
35
|
+
class Reducer2 < Wukong::Streamer::AccumulatingReducer
|
36
|
+
attr_accessor :key_count
|
37
|
+
def start! *args
|
38
|
+
self.key_count = 0
|
39
|
+
end
|
40
|
+
def accumulate(word, count)
|
41
|
+
self.key_count += count.to_i
|
42
|
+
end
|
43
|
+
def finalize
|
44
|
+
yield [ key, key_count ]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Of course you can be really lazy (that is, smart) and write your script instead as
|
49
|
+
|
50
|
+
class Script < Wukong::Script
|
51
|
+
def reducer_command
|
52
|
+
'uniq -c'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
h2. Structured data
|
58
|
+
|
59
|
+
All of these deal with unstructured data. Wukong also lets you view your data
|
60
|
+
as a stream of structured objects.
|
61
|
+
|
62
|
+
Let's say you have a blog; its records look like
|
63
|
+
|
64
|
+
Post = Struct.new( :id, :created_at, :user_id, :title, :body, :link )
|
65
|
+
Comment = Struct.new( :id, :created_at, :post_id, :user_id, :body )
|
66
|
+
User = Struct.new( :id, :username, :fullname, :homepage, :description )
|
67
|
+
UserLoc = Struct.new( :user_id, :text, :lat, :lng )
|
68
|
+
|
69
|
+
You've been using "twitter":http://twitter.com for a long time, and you've
|
70
|
+
written something that from now on will inject all your tweets as Posts, and all
|
71
|
+
replies to them as Comments (by a common 'twitter_bot' account on your blog).
|
72
|
+
What about the past two years' worth of tweets? Let's assume you're so chatty that
|
73
|
+
a Map/Reduce script is warranted to handle the volume.
|
74
|
+
|
75
|
+
Cook up something that scrapes your tweets and all replies to your tweets:
|
76
|
+
|
77
|
+
Tweet = Struct.new( :id, :created_at, :twitter_user_id,
|
78
|
+
:in_reply_to_user_id, :in_reply_to_status_id, :text )
|
79
|
+
TwitterUser = Struct.new( :id, :username, :fullname,
|
80
|
+
:homepage, :location, :description )
|
81
|
+
|
82
|
+
Now we'll just process all those in a big pile, converting to Posts, Comments
|
83
|
+
and Users as appropriate. Serialize your scrape results so that each Tweet and
|
84
|
+
each TwitterUser is a single lines containing first the class name ('tweet' or
|
85
|
+
'twitter_user') followed by its constituent fields, in order, separated by tabs.
|
86
|
+
|
87
|
+
The RecordStreamer takes each such line, constructs its corresponding class, and
|
88
|
+
instantiates it with the
|
89
|
+
|
90
|
+
require 'wukong'
|
91
|
+
require 'my_blog' #defines the blog models
|
92
|
+
module TwitBlog
|
93
|
+
class Mapper < Wukong::Streamer::RecordStreamer
|
94
|
+
# Watch for tweets by me
|
95
|
+
MY_USER_ID = 24601
|
96
|
+
# structs for our input objects
|
97
|
+
Tweet = Struct.new( :id, :created_at, :twitter_user_id,
|
98
|
+
:in_reply_to_user_id, :in_reply_to_status_id, :text )
|
99
|
+
TwitterUser = Struct.new( :id, :username, :fullname,
|
100
|
+
:homepage, :location, :description )
|
101
|
+
#
|
102
|
+
# If this is a tweet is by me, convert it to a Post.
|
103
|
+
#
|
104
|
+
# If it is a tweet not by me, convert it to a Comment that
|
105
|
+
# will be paired with the correct Post.
|
106
|
+
#
|
107
|
+
# If it is a TwitterUser, convert it to a User record and
|
108
|
+
# a user_location record
|
109
|
+
#
|
110
|
+
def process record
|
111
|
+
case record
|
112
|
+
when TwitterUser
|
113
|
+
user = MyBlog::User.new.merge(record) # grab the fields in common
|
114
|
+
user_loc = MyBlog::UserLoc.new(record.id, record.location, nil, nil)
|
115
|
+
yield user
|
116
|
+
yield user_loc
|
117
|
+
when Tweet
|
118
|
+
if record.twitter_user_id == MY_USER_ID
|
119
|
+
post = MyBlog::Post.new.merge record
|
120
|
+
post.link = "http://twitter.com/statuses/show/#{record.id}"
|
121
|
+
post.body = record.text
|
122
|
+
post.title = record.text[0..65] + "..."
|
123
|
+
yield post
|
124
|
+
else
|
125
|
+
comment = MyBlog::Comment.new.merge record
|
126
|
+
comment.body = record.text
|
127
|
+
comment.post_id = record.in_reply_to_status_id
|
128
|
+
yield comment
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
Wukong::Script.new( TwitBlog::Mapper, nil ).run # identity reducer
|
135
|
+
|
136
|
+
h2. Uniqifying
|
137
|
+
|
138
|
+
The script above uses the identity reducer: every record from the mapper is sent
|
139
|
+
to the output. But what if you had grabbed the replying user's record every time
|
140
|
+
you saw a reply?
|
141
|
+
|
142
|
+
Fine, so pass it through @uniq@. But what if a user updated their location or
|
143
|
+
description during this time? You'll want to probably use UniqByLastReducer
|
144
|
+
|
145
|
+
Location might want to take the most /frequent/, and might want as well to
|
146
|
+
geolocate the location text. Use a ListReducer, find the most frequent element,
|
147
|
+
then finally call the expensive geolocation method.
|
148
|
+
|
149
|
+
h2. A note about keys
|
150
|
+
|
151
|
+
Now we're going to write this using the synthetic keys already extant in the
|
152
|
+
twitter records, making the unwarranted assumption that they won't collide with
|
153
|
+
the keys in your database.
|
154
|
+
|
155
|
+
Map/Reduce paradigm does badly with synthetic keys. Synthetic keys demand
|
156
|
+
locality, and map/reduce's remarkable scaling comes from not assuming
|
157
|
+
locality. In general, write your map/reduce scripts to use natural keys (the scre
|
158
|
+
|
159
|
+
h1. More info
|
160
|
+
|
161
|
+
There are many useful examples (including an actually-useful version of this
|
162
|
+
WordCount script) in examples/ directory.
|
163
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
h1. wu-lign -- format a tab-separated file as aligned columns
|
2
|
+
|
3
|
+
wu-lign will intelligently reformat a tab-separated file into a tab-separated, space aligned file that is still suitable for further processing. For example, given the log-file input
|
4
|
+
|
5
|
+
<pre><code>
|
6
|
+
2009-07-21T21:39:40 day 65536 3.15479 68750 1171316
|
7
|
+
2009-07-21T21:39:45 doing 65536 1.04533 26230 1053956
|
8
|
+
2009-07-21T21:41:53 hapaxlegomenon 65536 0.87574e-05 23707 10051141
|
9
|
+
2009-07-21T21:44:00 concert 500 0.29290 13367 9733414
|
10
|
+
2009-07-21T21:44:29 world 65536 1.09110 32850 200916
|
11
|
+
2009-07-21T21:44:39 world+series 65536 0.49380 9929 7972025
|
12
|
+
2009-07-21T21:44:54 iranelection 65536 2.91775 14592 136342
|
13
|
+
</code></pre>
|
14
|
+
|
15
|
+
wu-lign will reformat it to read
|
16
|
+
|
17
|
+
<pre><code>
|
18
|
+
2009-07-21T21:39:40 day 65536 3.154791234 68750 1171316
|
19
|
+
2009-07-21T21:39:45 doing 65536 1.045330000 26230 1053956
|
20
|
+
2009-07-21T21:41:53 hapaxlegomenon 65536 0.000008757 23707 10051141
|
21
|
+
2009-07-21T21:44:00 concert 500 0.292900000 13367 9733414
|
22
|
+
2009-07-21T21:44:29 world 65536 1.091100000 32850 200916
|
23
|
+
2009-07-21T21:44:39 world+series 65536 0.493800000 9929 7972025
|
24
|
+
2009-07-21T21:44:54 iranelection 65536 2.917750000 14592 136342
|
25
|
+
</code></pre>
|
26
|
+
|
27
|
+
The fields are still tab-delimited by exactly one tab -- only spaces are used to pad out fields. You can still use cuttab and friends to manipulate columns.
|
28
|
+
|
29
|
+
wu-lign isn't intended to be smart, or correct, or reliable -- only to be useful for previewing and organizing tab-formatted files. In general @wu-lign(foo).split("\t").map(&:strip)@ *should* give output semantically equivalent to its input. (That is, the only changes should be insertion of spaces and re-formatting of numerics.) But still -- reserve its use for human inspection only.
|
30
|
+
|
31
|
+
(Note: tab characters in this source code file have been converted to spaces; replace whitespace with tab in the first example if you'd like to play along at home.)
|
32
|
+
|
33
|
+
h2. How it works
|
34
|
+
|
35
|
+
Wu-Lign takes the first 1000 lines, splits by TAB characters into fields, and tries to guess the format -- int, float, or string -- for each. It builds a consensus of the width and type for corresponding columns in the chunk. If a column has mixed numeric and string formats it degrades to :mixed, which is basically treated as :string. If a column has mixed :float and :int elements all of them are formatted as float.
|
36
|
+
|
37
|
+
h2. Command-line arguments
|
38
|
+
|
39
|
+
You can give sprintf-style positional arguments on the command line that will be applied to the corresponding columns. (Blank args are used for placeholding and auto-formatting is still applied). So with the example above,
|
40
|
+
|
41
|
+
@cat foo | wu-lign '' '' '' '%8.4e'@
|
42
|
+
|
43
|
+
will format the fourth column with "%8.4e", while the first three columns and fifth-and-higher columns are formatted as usual.
|
44
|
+
|
45
|
+
<pre><code>
|
46
|
+
...
|
47
|
+
2009-07-21T21:39:45 doing 65536 1.0453e+00 26230 1053956
|
48
|
+
2009-07-21T21:41:53 hapaxlegomenon 65536 8.7574e-06 23707 10051141
|
49
|
+
2009-07-21T21:44:00 concert 500 2.9290e-01 13367 9733414
|
50
|
+
....
|
51
|
+
</code></pre>
|
52
|
+
|
53
|
+
h2. Notes
|
54
|
+
|
55
|
+
* It has no knowledge of header rows. An all-text first line will screw everything up.
|
56
|
+
* It also requires a unanimous vote. One screwy line can coerce the whole mess to :mixed; width formatting will still be applied, though.
|
57
|
+
* It won't set columns wider than 70 chars -- this allows for the occasional super-wide column without completely breaking your screen.
|
58
|
+
* For :float values, wu-lign tries to guess at the right number of significant digits to the left and right of the decimal point.
|
59
|
+
* wu-lign does not parse 'TSV files' in their strict sense -- there is no quoting or escaping; every tab delimits a field, every newline a record.
|
@@ -0,0 +1,128 @@
|
|
1
|
+
h1. Wukong Utility Scripts
|
2
|
+
|
3
|
+
h2. Stupid command-line tricks
|
4
|
+
|
5
|
+
h3. Histogram
|
6
|
+
|
7
|
+
Given data with a date column:
|
8
|
+
|
9
|
+
message 235623 20090423012345 Now is the winter of our discontent Made glorious summer by this son of York
|
10
|
+
message 235623 20080101230900 These pretzels are making me THIRSTY!
|
11
|
+
...
|
12
|
+
|
13
|
+
You can calculate number of messages sent by day with
|
14
|
+
|
15
|
+
cat messages | cuttab 3 | cutc 8 | sort | uniq -c
|
16
|
+
|
17
|
+
(see the wuhist command, below.)
|
18
|
+
|
19
|
+
h3. Simple intersection, union, etc
|
20
|
+
|
21
|
+
For two datasets (batch_1 and batch_2) with unique entries (no repeated lines),
|
22
|
+
|
23
|
+
* Their union is simple:
|
24
|
+
|
25
|
+
cat batch_1 batch_2 | sort -u
|
26
|
+
|
27
|
+
|
28
|
+
* Their intersection:
|
29
|
+
|
30
|
+
cat batch_1 batch_2 | sort | uniq -c | egrep -v '^ *1 '
|
31
|
+
|
32
|
+
This concatenates the two sets and filters out everything that only occurred once.
|
33
|
+
|
34
|
+
* For the complement of the intersection, use "... | egrep '^ *1 '"
|
35
|
+
|
36
|
+
* In both cases, if the files are each internally sorted, the commandline sort takes a --merge flag:
|
37
|
+
|
38
|
+
sort --merge -u batch_1 batch_2
|
39
|
+
|
40
|
+
h2. Command Listing
|
41
|
+
|
42
|
+
h3. cutc
|
43
|
+
|
44
|
+
@cutc [colnum]@
|
45
|
+
|
46
|
+
Ex.
|
47
|
+
|
48
|
+
echo -e 'foo\tbar\tbaz' | cutc 6
|
49
|
+
foo ba
|
50
|
+
|
51
|
+
Cuts from beginning of line to given column (default 200). A tab is one character, so right margin can still be ragged.
|
52
|
+
|
53
|
+
h3. cuttab
|
54
|
+
|
55
|
+
@cuttab [colspec]@
|
56
|
+
|
57
|
+
Cuts given tab-separated columns. You can give a comma separated list of numbers
|
58
|
+
or ranges 1-4. columns are numbered from 1.
|
59
|
+
|
60
|
+
Ex.
|
61
|
+
|
62
|
+
echo -e 'foo\tbar\tbaz' | cuttab 1,3
|
63
|
+
foo baz
|
64
|
+
|
65
|
+
h3. hdp-*
|
66
|
+
|
67
|
+
These perform the corresponding commands on the HDFS filesystem. In general,
|
68
|
+
where they accept command-line flags, they go with the GNU-style ones, not the
|
69
|
+
hadoop-style: so, @hdp-du -s dir@ or @hdp-rm -r foo/@
|
70
|
+
|
71
|
+
* @hdp-cat@
|
72
|
+
* @hdp-catd@ -- cats the files that don't start with '_' in a directory. Use this for a pile of @.../part-00000@ files
|
73
|
+
* @hdp-du@
|
74
|
+
* @hdp-get@
|
75
|
+
* @hdp-kill@
|
76
|
+
* @hdp-ls@
|
77
|
+
* @hdp-mkdir@
|
78
|
+
* @hdp-mv@
|
79
|
+
* @hdp-ps@
|
80
|
+
* @hdp-put@
|
81
|
+
* @hdp-rm@
|
82
|
+
* @hdp-sync@
|
83
|
+
|
84
|
+
h3. hdp-sort, hdp-stream, hdp-stream-flat
|
85
|
+
|
86
|
+
* @hdp-sort@
|
87
|
+
* @hdp-stream@
|
88
|
+
* @hdp-stream-flat@
|
89
|
+
|
90
|
+
<code><pre>
|
91
|
+
hdp-stream input_filespec output_file map_cmd reduce_cmd num_key_fields
|
92
|
+
</pre></code>
|
93
|
+
|
94
|
+
h3. tabchar
|
95
|
+
|
96
|
+
Outputs a single tab character.
|
97
|
+
|
98
|
+
h3. wuhist
|
99
|
+
|
100
|
+
Occasionally useful to gather a lexical histogram of a single column:
|
101
|
+
|
102
|
+
Ex.
|
103
|
+
|
104
|
+
<code><pre>
|
105
|
+
$ echo -e 'foo\nbar\nbar\nfoo\nfoo\nfoo\n7' | ./wuhist
|
106
|
+
4 foo
|
107
|
+
2 bar
|
108
|
+
1 7
|
109
|
+
</pre></code>
|
110
|
+
|
111
|
+
(the output will have a tab between the first and second column, for futher processing.)
|
112
|
+
|
113
|
+
h3. wulign
|
114
|
+
|
115
|
+
Intelligently format a tab-separated file into aligned columns (while remaining tab-separated for further processing). See README-wulign.textile.
|
116
|
+
|
117
|
+
h3. hdp-parts_to_keys.rb
|
118
|
+
|
119
|
+
A *very* clumsy script to rename reduced hadoop output files by their initial key.
|
120
|
+
|
121
|
+
If your output file has an initial key in the first column and you pass it
|
122
|
+
through hdp-sort, they will be distributed across reducers and thus output
|
123
|
+
files. (Because of the way hadoop hashes the keys, there's no guarantee that
|
124
|
+
each file will get a distinct key. You could have 2 keys with a million entries
|
125
|
+
and they could land sequentially on the same reducer, always fun.)
|
126
|
+
|
127
|
+
If you're willing to roll the dice, this script will rename files according to
|
128
|
+
the first key in the first line.
|