mrflip-wukong 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +202 -0
- data/README-tutorial.textile +163 -0
- data/README.textile +165 -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/README-wulign.textile +59 -0
- data/doc/README-wutils.textile +128 -0
- data/doc/UsingWukong-part1.textile +2 -0
- data/doc/UsingWukong-part2.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-setup.textile +21 -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 +65 -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 +85 -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 +112 -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 +40 -0
- data/lib/wukong/models/graph.rb +27 -0
- data/lib/wukong/rdf.rb +104 -0
- data/lib/wukong/schema.rb +39 -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 +20 -0
- data/spec/bin/hdp-wc_spec.rb +4 -0
- data/spec/spec_helper.rb +0 -0
- data/wukong.gemspec +173 -0
- metadata +208 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
@@ -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
|
+
|
data/README.textile
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
h1. Wukong
|
2
|
+
|
3
|
+
Wukong makes "Hadoop":http://hadoop.apache.org/core so easy a chimpanzee can use it.
|
4
|
+
|
5
|
+
Treat your dataset like a
|
6
|
+
* stream of lines when it's efficient to process by lines
|
7
|
+
* stream of field arrays when it's efficient to deal directly with fields
|
8
|
+
* stream of lightweight objects when it's efficient to deal with objects
|
9
|
+
|
10
|
+
Wukong is friends with "Hadoop":http://hadoop.apache.org/core the elephant, "Pig":http://hadoop.apache.org/pig/ the query language, and the @cat@ on your command line.
|
11
|
+
|
12
|
+
h2. How to write a Wukong script
|
13
|
+
|
14
|
+
Here's a script to count words in a text stream:
|
15
|
+
|
16
|
+
<pre><code>
|
17
|
+
require 'wukong'
|
18
|
+
module WordCount
|
19
|
+
class Mapper < Wukong::Streamer::LineStreamer
|
20
|
+
# Emit each word in the line.
|
21
|
+
def process line
|
22
|
+
words = line.strip.split(/\W+/).reject(&:blank?)
|
23
|
+
words.each{|word| yield [word, 1] }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Reducer < Wukong::Streamer::ListReducer
|
28
|
+
def finalize
|
29
|
+
yield [ key, values.map(&:last).map(&:to_i).sum ]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Wukong::Script.new(
|
35
|
+
WordCount::Mapper,
|
36
|
+
WordCount::Reducer
|
37
|
+
).run # Execute the script
|
38
|
+
</code></pre>
|
39
|
+
|
40
|
+
The first class, the Mapper, eats lines and craps @[word, count]@ records: word is the /key/, its count is the /value/.
|
41
|
+
|
42
|
+
In the reducer, the values for each key are stacked up into a list; then the record(s) yielded by @#finalize@ are emitted. There are many other ways to write the reducer (most of them are better) -- see the ["examples":examples/]
|
43
|
+
|
44
|
+
h3. Structured data stream
|
45
|
+
|
46
|
+
You can also use structs to treat your dataset as a stream of objects:
|
47
|
+
|
48
|
+
<pre><code>
|
49
|
+
require 'wukong'
|
50
|
+
require 'my_blog' #defines the blog models
|
51
|
+
# structs for our input objects
|
52
|
+
Tweet = Struct.new( :id, :created_at, :twitter_user_id,
|
53
|
+
:in_reply_to_user_id, :in_reply_to_status_id, :text )
|
54
|
+
TwitterUser = Struct.new( :id, :username, :fullname,
|
55
|
+
:homepage, :location, :description )
|
56
|
+
module TwitBlog
|
57
|
+
class Mapper < Wukong::Streamer::RecordStreamer
|
58
|
+
# Watch for tweets by me
|
59
|
+
MY_USER_ID = 24601
|
60
|
+
#
|
61
|
+
# If this is a tweet is by me, convert it to a Post.
|
62
|
+
#
|
63
|
+
# If it is a tweet not by me, convert it to a Comment that
|
64
|
+
# will be paired with the correct Post.
|
65
|
+
#
|
66
|
+
# If it is a TwitterUser, convert it to a User record and
|
67
|
+
# a user_location record
|
68
|
+
#
|
69
|
+
def process record
|
70
|
+
case record
|
71
|
+
when TwitterUser
|
72
|
+
user = MyBlog::User.new.merge(record) # grab the fields in common
|
73
|
+
user_loc = MyBlog::UserLoc.new(record.id, record.location, nil, nil)
|
74
|
+
yield user
|
75
|
+
yield user_loc
|
76
|
+
when Tweet
|
77
|
+
if record.twitter_user_id == MY_USER_ID
|
78
|
+
post = MyBlog::Post.new.merge record
|
79
|
+
post.link = "http://twitter.com/statuses/show/#{record.id}"
|
80
|
+
post.body = record.text
|
81
|
+
post.title = record.text[0..65] + "..."
|
82
|
+
yield post
|
83
|
+
else
|
84
|
+
comment = MyBlog::Comment.new.merge record
|
85
|
+
comment.body = record.text
|
86
|
+
comment.post_id = record.in_reply_to_status_id
|
87
|
+
yield comment
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
Wukong::Script.new( TwitBlog::Mapper, nil ).run # identity reducer
|
94
|
+
</code></pre>
|
95
|
+
|
96
|
+
h3. More info
|
97
|
+
|
98
|
+
There are many useful examples (including an actually-useful version of the WordCount script) in examples/ directory.
|
99
|
+
|
100
|
+
h2. Setup
|
101
|
+
|
102
|
+
1. Allow Wukong to discover where his elephant friend lives: either
|
103
|
+
|
104
|
+
* set a $HADOOP_HOME environment variable,
|
105
|
+
|
106
|
+
* or create a file 'config/wukong-site.yaml' with a line that points to the top-level directory of your hadoop install:
|
107
|
+
|
108
|
+
:hadoop_home: /usr/local/share/hadoop
|
109
|
+
|
110
|
+
2. Add wukong's @bin/@ directory to your $PATH, so that you may use its filesystem shortcuts.
|
111
|
+
|
112
|
+
|
113
|
+
h2. How to run a Wukong script
|
114
|
+
|
115
|
+
To run your script using local files and no connection to a hadoop cluster,
|
116
|
+
|
117
|
+
your/script.rb --run=local path/to/input_files path/to/output_dir
|
118
|
+
|
119
|
+
To run the command across a Hadoop cluster,
|
120
|
+
|
121
|
+
your/script.rb --run=hadoop path/to/input_files path/to/output_dir
|
122
|
+
|
123
|
+
You can set the default in the config/wukong-site.yaml file, and then just use @--run@ instead of @--run=something@ --it will just use the default run mode.
|
124
|
+
|
125
|
+
If you're running @--run=hadoop@, all file paths are HDFS paths. If you're running @--run=local@, all file paths are local paths. (your/script path, of course, lives on the local filesystem).
|
126
|
+
|
127
|
+
You can supply arbitrary command line arguments (they wind up as key-value pairs in the options path your mapper and reducer receive), and you can use the hadoop syntax to specify more than one input file:
|
128
|
+
|
129
|
+
./path/to/your/script.rb --any_specific_options --options=can_have_vals \
|
130
|
+
--run "input_dir/part_*,input_file2.tsv,etc.tsv" path/to/output_dir
|
131
|
+
|
132
|
+
Note that all @--options@ must precede (in any order) all non-options.
|
133
|
+
|
134
|
+
h2. How to test your scripts
|
135
|
+
|
136
|
+
To run mapper on its own:
|
137
|
+
|
138
|
+
cat ./local/test/input.tsv | ./examples/word_count.rb --map | more
|
139
|
+
|
140
|
+
or if your test data lies on the HDFS,
|
141
|
+
|
142
|
+
hdp-cat test/input.tsv | ./examples/word_count.rb --map | more
|
143
|
+
|
144
|
+
Next graduate to running @--run=local@ mode so you can inspect the reducer.
|
145
|
+
|
146
|
+
|
147
|
+
h2. What's up with Wukong::AndPig?
|
148
|
+
|
149
|
+
@Wukong::AndPig@ is a small library to more easily generate code for the
|
150
|
+
"Pig":http://hadoop.apache.org/pig data analysis language. See its
|
151
|
+
"README":wukong/and_pig/README.textile for more.
|
152
|
+
|
153
|
+
h2. Why is it called Wukong?
|
154
|
+
|
155
|
+
Hadoop, as you may know, is "named after a stuffed elephant.":http://en.wikipedia.org/wiki/Hadoop Since Wukong was started by the "infochimps":http://infochimps.org team, we needed a simian analog. A Monkey King who journeyed to the land of the Elephant seems to fit the bill:
|
156
|
+
|
157
|
+
bq. Sun Wukong (孙悟空), known in the West as the Monkey King, is the main character in the classical Chinese epic novel Journey to the West. In the novel, he accompanies the monk Xuanzang on the journey to retrieve Buddhist sutras from India.
|
158
|
+
|
159
|
+
bq. Sun Wukong possesses incredible strength, being able to lift his 13,500 jīn (8,100 kg) Ruyi Jingu Bang with ease. He also has superb speed, traveling 108,000 li (54,000 kilometers) in one somersault. Sun knows 72 transformations, which allows him to transform into various animals and objects; he is, however, shown with slight problems transforming into other people, since he is unable to complete the transformation of his tail. He is a skilled fighter, capable of holding his own against the best generals of heaven. Each of his hairs possesses magical properties, and is capable of transforming into a clone of the Monkey King himself, or various weapons, animals, and other objects. He also knows various spells in order to command wind, part water, conjure protective circles against demons, freeze humans, demons, and gods alike. -- ["Sun Wukong's Wikipedia entry":http://en.wikipedia.org/wiki/Wukong]
|
160
|
+
|
161
|
+
The "Jaime Hewlett / Damon Albarn short":http://news.bbc.co.uk/sport1/hi/olympics/monkey that the BBC made for their 2008 Olympics coverage gives the general idea.
|
162
|
+
|
163
|
+
h2. What tools does Wukong work with?
|
164
|
+
|
165
|
+
Wukong is friends with "Hadoop":http://hadoop.apache.org/core the elephant, "Pig":http://hadoop.apache.org/pig/ the query language, and the @cat@ on your command line. We're looking forward to being friends with "martinis":http://datamapper.org and "express trains":http://wiki.rubyonrails.org/rails/pages/ActiveRecord down the road.
|