Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Created October 28, 2023 06:55
Show Gist options
  • Save rajathithan/4e48bd33f77f8281769bd8706685dce9 to your computer and use it in GitHub Desktop.
Save rajathithan/4e48bd33f77f8281769bd8706685dce9 to your computer and use it in GitHub Desktop.
Count the words in a sentence
import apache_beam as beam
from log_elements import LogElements
lines = [
"apple orange grape banana apple banana",
"banana orange banana papaya"
]
with beam.Pipeline() as p:
(p | beam.Create(lines)
| beam.FlatMap(lambda sentence: sentence.split())
| beam.combiners.Count.PerElement()
| beam.MapTuple(lambda k, v: k + ":" + str(v))
| LogElements())
'''
Answer:
apple:2
orange:2
grape:1
banana:4
papaya:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment