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

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

                Author: ASF GitHub Bot
            Created on: 11/Apr/19 22:53
            Start Date: 11/Apr/19 22:53
    Worklog Time Spent: 10m 
      Work Description: udim commented on pull request #8135: [BEAM-6892] 
Adding support for auto-creating buckets for BigQuery file loads
URL: https://github.com/apache/beam/pull/8135#discussion_r274695249
 
 

 ##########
 File path: sdks/python/apache_beam/io/gcp/bigquery_file_loads.py
 ##########
 @@ -505,28 +511,92 @@ def __init__(
     # If the destination is a single one, we assume that we will have only one
     # job to run - and thus we avoid using temporary tables
     self.temp_tables = True if callable(destination) else False
-
+    self.kms_key = kms_key
     self._validate = validate
-    if self._validate:
-      self.verify()
 
-  def verify(self):
-    if (isinstance(self._custom_gcs_temp_location, str) and
+  def verify(self, options):
+
+    self._custom_gcs_temp_location = (
+        self._custom_gcs_temp_location
+        or options.view_as(GoogleCloudOptions).temp_location)
+
+    if (not self._custom_gcs_temp_location or
         not self._custom_gcs_temp_location.startswith('gs://')):
-      # Only fail if the custom location is provided, and it is not a GCS
-      # location.
+
+      logging.info('No appropriate location was provided to perform file loads'
+                   'to GCS.')
+      bucket = self.try_to_create_default_gcs_bucket(options)
+
+      if bucket:
+        self._custom_gcs_temp_location = 'gs://%s/temp/' % bucket.name
+        return
+
       raise ValueError('Invalid GCS location.\n'
                        'Writing to BigQuery with FILE_LOADS method requires a '
                        'GCS location to be provided to write files to be '
                        'loaded into BigQuery. Please provide a GCS bucket, or '
                        'pass method="STREAMING_INSERTS" to WriteToBigQuery.')
 
+  def try_to_create_default_gcs_bucket(self, options):
+    DEFAULT_BUCKET_NAME = "dataflow-staging-%s-%s"
+    DEFAULT_REGION = "US"
+    logging.info('Attempting to get or create a default GCS bucket.')
+
+    project_name = options.view_as(GoogleCloudOptions).project
+
+    if not project_name and isinstance(self.destination,
+                                       bigquery_api.TableReference):
+      project_name = self.destination.projectId
+
+    region = options.view_as(GoogleCloudOptions).region
+
+    if not project_name:
+      raise ValueError('--project is a required option.'
+                       ' To create a default bucket, Beam needs a project '
+                       'parameter passed to your pipeline.')
+
+    # Retrieve the project number for the default bucket
+    from google.cloud import resource_manager
+    client = resource_manager.Client()
+    project_number = client.fetch_project(project_name).number
+
+    # We get the region, and cut off the zone id if there is one.
+    region = (region or DEFAULT_REGION).split('-')[0].lower()
+
+    bucket_name = DEFAULT_BUCKET_NAME % (region, project_number)
 
 Review comment:
   If a user wants to revoke a key that's associated to a job, and the bucket 
created by this method is using that key, it will affect all jobs in the same 
project and region. This is unintuitive behavior. Instead, 
`try_to_create_default_gcs_bucket` should fail if `dataflow_kms_key` is 
specified. (Java SDK will also change its behavior to not automatically create 
buckets with KMS keys.)
 
----------------------------------------------------------------
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


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

    Worklog Id:     (was: 226396)
    Time Spent: 4h  (was: 3h 50m)

> Use temp_location for BQ FILE_LOADS on DirectRunner, and autocreate it in GCS 
> if not specified by user.
> -------------------------------------------------------------------------------------------------------
>
>                 Key: BEAM-6892
>                 URL: https://issues.apache.org/jira/browse/BEAM-6892
>             Project: Beam
>          Issue Type: Bug
>          Components: sdk-py-core
>            Reporter: Valentyn Tymofieiev
>            Assignee: Pablo Estrada
>            Priority: Major
>             Fix For: 2.13.0
>
>          Time Spent: 4h
>  Remaining Estimate: 0h
>




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

Reply via email to