Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6b9bbc

Browse files
mariapythonrobertwb
authored andcommitted
Update README examples to use the new io APIs
1 parent 020daa9 commit e6b9bbc

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

sdks/python/README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ p = beam.Pipeline('DirectRunner')
175175
# Create a PCollection with names and write it to a file.
176176
(p
177177
| 'add names' >> beam.Create(['Ann', 'Joe'])
178-
| 'save' >> beam.io.Write(beam.io.TextFileSink('./names')))
178+
| 'save' >> beam.io.WriteToText('./names'))
179179
# Execute the pipeline.
180180
p.run()
181181
```
@@ -189,9 +189,9 @@ import apache_beam as beam
189189
p = beam.Pipeline('DirectRunner')
190190
# Read a file containing names, add a greeting to each name, and write to a file.
191191
(p
192-
| 'load names' >> beam.Read(beam.io.TextFileSource('./names'))
192+
| 'load names' >> beam.io.ReadFromText('./names')
193193
| 'add greeting' >> beam.Map(lambda name, msg: '%s, %s!' % (msg, name), 'Hello')
194-
| 'save' >> beam.Write(beam.io.TextFileSink('./greetings')))
194+
| 'save' >> beam.io.WriteToText('./greetings'))
195195
p.run()
196196
```
197197

@@ -207,11 +207,11 @@ import apache_beam as beam
207207
p = beam.Pipeline('DirectRunner')
208208
# Read a file containing names, add two greetings to each name, and write to a file.
209209
(p
210-
| 'load names' >> beam.Read(beam.io.TextFileSource('./names'))
210+
| 'load names' >> beam.io.ReadFromText('./names')
211211
| 'add greetings' >> beam.FlatMap(
212212
lambda name, messages: ['%s %s!' % (msg, name) for msg in messages],
213213
['Hello', 'Hola'])
214-
| 'save' >> beam.Write(beam.io.TextFileSink('./greetings')))
214+
| 'save' >> beam.io.WriteToText('./greetings'))
215215
p.run()
216216
```
217217

@@ -230,9 +230,9 @@ def add_greetings(name, messages):
230230
yield '%s %s!' % (msg, name)
231231

232232
(p
233-
| 'load names' >> beam.Read(beam.io.TextFileSource('./names'))
233+
| 'load names' >> beam.io.ReadFromText('./names')
234234
| 'add greetings' >> beam.FlatMap(add_greetings, ['Hello', 'Hola'])
235-
| 'save' >> beam.Write(beam.io.TextFileSink('./greetings')))
235+
| 'save' >> beam.io.WriteToText('./greetings'))
236236
p.run()
237237
```
238238

@@ -245,11 +245,10 @@ import re
245245
import apache_beam as beam
246246
p = beam.Pipeline('DirectRunner')
247247
(p
248-
| 'read' >> beam.Read(
249-
beam.io.TextFileSource('gs://dataflow-samples/shakespeare/kinglear.txt'))
248+
| 'read' >> beam.io.ReadFromText('gs://dataflow-samples/shakespeare/kinglear.txt')
250249
| 'split' >> beam.FlatMap(lambda x: re.findall(r'\w+', x))
251250
| 'count words' >> beam.combiners.Count.PerElement()
252-
| 'save' >> beam.Write(beam.io.TextFileSink('./word_count')))
251+
| 'save' >> beam.io.WriteToText('./word_count'))
253252
p.run()
254253
```
255254

@@ -271,10 +270,10 @@ class MyCountTransform(beam.PTransform):
271270
| 'count words' >> beam.Map(lambda (word, counts): (word, len(counts))))
272271

273272
(p
274-
| 'read' >> beam.Read(beam.io.TextFileSource('./names*'))
273+
| 'read' >> beam.io.ReadFromText('./names*')
275274
| 'split' >> beam.FlatMap(lambda x: re.findall(r'\w+', x))
276275
| MyCountTransform()
277-
| 'write' >> beam.Write(beam.io.TextFileSink('./word_count')))
276+
| 'write' >> beam.io.WriteToText('./word_count'))
278277
p.run()
279278
```
280279

@@ -288,10 +287,10 @@ import apache_beam as beam
288287
from apache_beam.typehints import typehints
289288
p = beam.Pipeline('DirectRunner')
290289
(p
291-
| 'read' >> beam.Read(beam.io.TextFileSource('./names'))
290+
| 'read' >> beam.io.ReadFromText('./names')
292291
| 'add types' >> beam.Map(lambda x: (x, 1)).with_output_types(typehints.KV[str, int])
293292
| 'group words' >> beam.GroupByKey()
294-
| 'save' >> beam.Write(beam.io.TextFileSink('./typed_names')))
293+
| 'save' >> beam.io.WriteToText('./typed_names'))
295294
p.run()
296295
```
297296

@@ -354,7 +353,7 @@ SAMPLE_DATA = [('a', 1), ('b', 10), ('a', 2), ('a', 3), ('b', 20)]
354353
(p
355354
| beam.Create(SAMPLE_DATA)
356355
| beam.CombinePerKey(sum)
357-
| beam.Write(beam.io.TextFileSink('./sums')))
356+
| beam.io.WriteToText('./sums'))
358357
p.run()
359358
```
360359

0 commit comments

Comments
 (0)