[ 
https://issues.apache.org/jira/browse/BEAM-3342?focusedWorklogId=183811&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-183811
 ]

ASF GitHub Bot logged work on BEAM-3342:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 10/Jan/19 17:05
            Start Date: 10/Jan/19 17:05
    Worklog Time Spent: 10m 
      Work Description: juan-rael commented on pull request #7367: [BEAM-3342] 
Create a Cloud Bigtable Python connector Write
URL: https://github.com/apache/beam/pull/7367#discussion_r246843576
 
 

 ##########
 File path: sdks/python/apache_beam/io/gcp/bigtable_io_test.py
 ##########
 @@ -0,0 +1,174 @@
+#
+# 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.
+#
+
+"""Unittest for GCP Bigtable testing."""
+from __future__ import absolute_import
+
+import datetime
+import logging
+import random
+import string
+import unittest
+import uuid
+
+import apache_beam as beam
+from apache_beam.io.gcp.bigtable_io_write import BigtableWriteConfiguration
+from apache_beam.io.gcp.bigtable_io_write import WriteToBigtable
+from apache_beam.metrics.metric import MetricsFilter
+from apache_beam.options.pipeline_options import PipelineOptions
+from apache_beam.runners.runner import PipelineState
+from apache_beam.testing.test_pipeline import TestPipeline
+
+# Protect against environments where bigtable library is not available.
+# pylint: disable=wrong-import-order, wrong-import-position
+try:
+  from google.cloud.bigtable import row, column_family, Client
+except ImportError:
+  Client = None
+
+
+class GenerateDirectRows(beam.DoFn):
+  """ Generates an iterator of DirectRow object to process on beam pipeline.
+
+  """
+  def process(self, row_values):
+    """ Process beam pipeline using an element.
+
+    :type row_value: dict
+    :param row_value: dict: dict values with row_key and row_content having
+    family, column_id and value of row.
+    """
+    direct_row = row.DirectRow(row_key=row_values["row_key"])
+
+    for row_value in row_values["row_content"]:
+      direct_row.set_cell(row_value["column_family_id"],
+                          row_value["column_id"],
+                          row_value["value"],
+                          datetime.datetime.now())
+
+
+@unittest.skipIf(Client is None, 'GCP Bigtable dependencies are not installed')
+class BigtableIOWriteIT(unittest.TestCase):
+  """ Bigtable Write Connector Test
+
+  """
+  DEFAULT_TABLE_PREFIX = "python-test"
+  instance_id = DEFAULT_TABLE_PREFIX + "-" + str(uuid.uuid4())[:8]
+  cluster_id = DEFAULT_TABLE_PREFIX + "-" + str(uuid.uuid4())[:8]
+  table_id = DEFAULT_TABLE_PREFIX + "-" + str(uuid.uuid4())[:8]
+  number = 500
+  LOCATION_ID = "us-east1-b"
+
+  def setUp(self):
+    try:
+      from google.cloud.bigtable import enums
+      self.STORAGE_TYPE = enums.StorageType.HDD
+    except ImportError:
+      self.STORAGE_TYPE = 2
+
+    self.test_pipeline = TestPipeline(is_integration_test=True)
+    self.runner_name = type(self.test_pipeline.runner).__name__
+    self.project = self.test_pipeline.get_option('project')
+    self.client = Client(project=self.project, admin=True)
+    self._create_instance_table()
+
+  def tearDown(self):
+    instance = self.client.instance(self.instance_id)
+    if instance.exists():
+      instance.delete()
+
+  def test_bigtable_write_python(self):
+    number = self.number
+    config = BigtableWriteConfiguration(self.project, self.instance_id,
+                                        self.table_id)
+    pipeline_args = self.test_pipeline.options_list
+    pipeline_options = PipelineOptions(pipeline_args)
+
+    row_values = self._generate_mutation_data(number)
+
+    with beam.Pipeline(options=pipeline_options) as pipeline:
+      _ = (
+          pipeline
+          | 'Generate Row Values' >> beam.Create(row_values)
+          | 'Generate Direct Rows' >> beam.ParDo(GenerateDirectRows())
+          | 'Write to BT' >> beam.ParDo(WriteToBigtable(config)))
+
+      result = pipeline.run()
+      result.wait_until_finish()
+
+      assert result.state == PipelineState.DONE
+
+      if not hasattr(result, 'has_job') or result.has_job:
+        read_filter = MetricsFilter().with_name('Written Row')
+        query_result = result.metrics().query(read_filter)
+        if query_result['counters']:
+          read_counter = query_result['counters'][0]
+
+          logging.info('Number of Rows: %d', read_counter.committed)
+          assert read_counter.committed == number
+
+  def _create_instance_table(self):
+    """ Prepare all necesary for the test
+    """
+    self.instance = self.client.instance(self.instance_id)
+
+    serve_nodes = 3
+    cluster = self.instance.cluster(self.cluster_id,
+                                    self.LOCATION_ID,
+                                    serve_nodes=serve_nodes,
+                                    default_storage_type=self.STORAGE_TYPE)
+    if not cluster.exists():
 
 Review comment:
   But, to create the instance, I need at least one cluster
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 183811)
    Time Spent: 4h 20m  (was: 4h 10m)

> Create a Cloud Bigtable Python connector
> ----------------------------------------
>
>                 Key: BEAM-3342
>                 URL: https://issues.apache.org/jira/browse/BEAM-3342
>             Project: Beam
>          Issue Type: Bug
>          Components: sdk-py-core
>            Reporter: Solomon Duskis
>            Assignee: Solomon Duskis
>            Priority: Major
>          Time Spent: 4h 20m
>  Remaining Estimate: 0h
>
> I would like to create a Cloud Bigtable python connector.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to