Skip to content
Newer
Older
100644 63 lines (49 sloc) 1.94 KB
64c1c4f PyCascading initial commit.
Gabor Szabo authored
1 #
2 # Copyright 2011 Twitter, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
d430956 Added note to README and smaller changes
Gabor Szabo authored
8 #
64c1c4f PyCascading initial commit.
Gabor Szabo authored
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
16 """Example showing how to use filters and buffers.
64c1c4f PyCascading initial commit.
Gabor Szabo authored
17
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
18 A buffer UDF is similar to the built-in Python reduce function. It takes a
19 group of tuples that have been previously grouped by group_by, and yields an
64c1c4f PyCascading initial commit.
Gabor Szabo authored
20 arbitrary number of new tuples for the group (it is most useful though to do
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
21 some aggregation on the group). The tuples are fetched using an iterator.
64c1c4f PyCascading initial commit.
Gabor Szabo authored
22 """
23
24 from pycascading.helpers import *
25
26
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
27 @udf_filter
64c1c4f PyCascading initial commit.
Gabor Szabo authored
28 def starts_with_letter(tuple, letter):
29 try:
30 return tuple.get(1)[0].upper() == letter
31 except:
32 return False
33
34
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
35 @udf_map
64c1c4f PyCascading initial commit.
Gabor Szabo authored
36 def word_count(tuple):
37 return [len(tuple.get(1).split()), tuple.get(1)]
38
39
40 def main():
41 flow = Flow()
3977067 Minor fixes.
Gabor Szabo authored
42 input = flow.source(Hfs(TextLine(), 'pycascading_data/town.txt'))
43 output = flow.tsv_sink('pycascading_data/out')
d430956 Added note to README and smaller changes
Gabor Szabo authored
44
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
45 p = input | filter_by(starts_with_letter('A')) | \
46 map_replace(word_count(), ['word_count', 'line'])
47
48 @udf_buffer(produces=['word_count', 'count', 'first_chars'])
49 def count(group, tuples):
50 """Counts the number of tuples in the group, and also emits a string
51 that is the first character of the 'line' column repeated this many
52 times."""
53 c = 0
54 first_char = ''
55 for tuple in tuples:
56 c += 1
57 first_char += tuple.get('line')[0]
58 yield [group.get(0), c, first_char]
59
60 p | group_by('word_count', count()) | output
d430956 Added note to README and smaller changes
Gabor Szabo authored
61
898ad13 Examples rewritten using the new operators and decorators
Gabor Szabo authored
62 flow.run(num_reducers=2)
Something went wrong with that request. Please try again.