beam-github mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [beam] robertwb commented on a change in pull request #12889: Dataframe wordcount example.
Date Mon, 21 Sep 2020 19:08:53 GMT

robertwb commented on a change in pull request #12889:
URL: https://github.com/apache/beam/pull/12889#discussion_r492284977



##########
File path: sdks/python/apache_beam/examples/wordcount_dataframe.py
##########
@@ -0,0 +1,73 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""A word-counting workflow using dataframes."""
+
+# pytype: skip-file
+
+from __future__ import absolute_import
+
+import argparse
+import logging
+
+import apache_beam as beam
+from apache_beam.dataframe import io
+from apache_beam.dataframe.convert import to_dataframe
+from apache_beam.io import ReadFromText
+from apache_beam.options.pipeline_options import PipelineOptions
+
+
+def run(argv=None):
+  """Main entry point; defines and runs the wordcount pipeline."""
+  parser = argparse.ArgumentParser()
+  parser.add_argument(
+      '--input',
+      dest='input',
+      default='gs://dataflow-samples/shakespeare/kinglear.txt',
+      help='Input file to process.')
+  parser.add_argument(
+      '--output',
+      dest='output',
+      required=True,
+      help='Output file to write results to.')
+  known_args, pipeline_args = parser.parse_known_args(argv)
+
+  # Import this here to avoid pickling the main session.
+  import re
+
+  # The pipeline will be run on exiting the with block.
+  with beam.Pipeline(options=PipelineOptions(pipeline_args)) as p:
+
+    # Read the text file[pattern] into a PCollection.
+    lines = p | 'Read' >> ReadFromText(known_args.input)
+
+    words = (
+        lines
+        | 'Split' >> beam.FlatMap(
+            lambda line: re.findall(r'[\w]+', line)).with_output_types(str)
+        # Map to Row objects to generate a schema suitable for conversion to a dataframe.
+        | 'ToRows' >> beam.Map(lambda word: beam.Row(word=word)))

Review comment:
       I actually toyed with that, but it's not as natural (or time-saving) for 1-field schemas.





----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message