Repository: beam
Updated Branches:
  refs/heads/master 034bcb4e2 -> 0e11a9368


Making metrics usage in datastore_wordcount consistent


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/f9d3c1ee
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/f9d3c1ee
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/f9d3c1ee

Branch: refs/heads/master
Commit: f9d3c1ee0973aaa2f98f0ff8dc211e334f9fc355
Parents: 034bcb4
Author: Pablo <[email protected]>
Authored: Tue Apr 18 08:43:12 2017 -0700
Committer: Ahmet Altay <[email protected]>
Committed: Tue Apr 18 09:55:37 2017 -0700

----------------------------------------------------------------------
 .../examples/cookbook/datastore_wordcount.py         | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/f9d3c1ee/sdks/python/apache_beam/examples/cookbook/datastore_wordcount.py
----------------------------------------------------------------------
diff --git a/sdks/python/apache_beam/examples/cookbook/datastore_wordcount.py 
b/sdks/python/apache_beam/examples/cookbook/datastore_wordcount.py
index 9fa10d3..6e7ad76 100644
--- a/sdks/python/apache_beam/examples/cookbook/datastore_wordcount.py
+++ b/sdks/python/apache_beam/examples/cookbook/datastore_wordcount.py
@@ -80,14 +80,15 @@ from apache_beam.utils.pipeline_options import 
GoogleCloudOptions
 from apache_beam.utils.pipeline_options import PipelineOptions
 from apache_beam.utils.pipeline_options import SetupOptions
 
-empty_line_counter = Metrics.counter('main', 'empty_lines')
-word_length_counter = Metrics.counter('main', 'word_lengths')
-word_counter = Metrics.counter('main', 'total_words')
-
 
 class WordExtractingDoFn(beam.DoFn):
   """Parse each line of input text into words."""
 
+  def __init__(self):
+    self.empty_line_counter = Metrics.counter('main', 'empty_lines')
+    self.word_length_counter = Metrics.counter('main', 'word_lengths')
+    self.word_counter = Metrics.counter('main', 'total_words')
+
   def process(self, element):
     """Returns an iterator over words in contents of Cloud Datastore entity.
     The element is a line of text.  If the line is blank, note that, too.
@@ -102,11 +103,11 @@ class WordExtractingDoFn(beam.DoFn):
       text_line = content_value.string_value
 
     if not text_line:
-      empty_line_counter.inc()
+      self.empty_line_counter.inc()
     words = re.findall(r'[A-Za-z\']+', text_line)
     for w in words:
-      word_length_counter.inc(len(w))
-      word_counter.inc()
+      self.word_length_counter.inc(len(w))
+      self.word_counter.inc()
     return words
 
 

Reply via email to