forked from brunoripa/beam-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_count_exxample.py
More file actions
41 lines (28 loc) · 1.21 KB
/
Copy pathword_count_exxample.py
File metadata and controls
41 lines (28 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import print_function
import logging
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import SetupOptions
from apache_beam.options.pipeline_options import GoogleCloudOptions
from apache_beam.options.pipeline_options import StandardOptions
from apache_beam.io.textio import ReadFromText, WriteToText
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
input_filename = "./input.txt"
output_filename = "./output.txt"
options = PipelineOptions()
# gcloud_options = options.view_as(GoogleCloudOptions)
# gcloud_options.job_name = "test-job"
options.view_as(StandardOptions).runner = "direct"
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
with beam.Pipeline(options=PipelineOptions()) as p:
lines = p | 'Create' >> beam.Create([{'cat dog':'b', 'snake cat':'c', 'dog':'b' }],
)
counts = (
lines
| 'Split' >> (beam.ParDo(lambda x: x))
| 'PairWithOne' >> beam.Map(lambda x: (x, 1))
| 'GroupAndSum' >> beam.CombinePerKey(sum)
)
counts | 'Write' >> WriteToText(output_filename)