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



##########
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)))
+
+    df = to_dataframe(words)
+    df['count'] = 1
+    counted = df.groupby('word').sum()
+    counted.to_csv(known_args.output)

Review comment:
       Makes sense, let's do that.

##########
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. 

##########
File path: CHANGES.md
##########
@@ -71,11 +71,14 @@
     More details will be in an upcoming [blog 
post](https://beam.apache.org/blog/python-improved-annotations/index.html).
 * Improved the Interactive Beam API where recording streaming jobs now start a 
long running background recording job. Running ib.show() or ib.collect() 
samples from the recording 
([BEAM-10603](https://issues.apache.org/jira/browse/BEAM-10603)).
 * In Interactive Beam, ib.show() and ib.collect() now have "n" and "duration" 
as parameters. These mean read only up to "n" elements and up to "duration" 
seconds of data read from the recording 
([BEAM-10603](https://issues.apache.org/jira/browse/BEAM-10603)).
+* Initial preview of 
[Dataframes](https://s.apache.org/simpler-python-pipelines-2020#slide=id.g905ac9257b_1_21)
 support.
+    See also example at apache_beam/examples/wordcount_dataframe.py
 * X feature added (Java/Python) 
([BEAM-X](https://issues.apache.org/jira/browse/BEAM-X)).
 
 ## Breaking Changes
 
-* X behavior was changed 
([BEAM-X](https://issues.apache.org/jira/browse/BEAM-X)).
+* Python 2 and Python 3.5 support dropped.
+* Pandas 1.x allowed.  Older version of Pandas may still be used, but may not 
be as well tested.

Review comment:
       I think it makes more sense here--the main implication here is how 
diamond dependencies might get resolved. 




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to