satybald commented on a change in pull request #15602:
URL: https://github.com/apache/beam/pull/15602#discussion_r718909933
##########
File path: sdks/python/apache_beam/io/gcp/bigquery.py
##########
@@ -887,6 +899,380 @@ def _export_files(self, bq):
return table.schema, metadata_list
+class _CustomBigQueryStorageSource(BoundedSource):
+ """A base class for BoundedSource implementations which read from BigQuery
+ using the BigQuery Storage API.
+ Args:
+ table (str, TableReference): The ID of the table. The ID must contain only
+ letters ``a-z``, ``A-Z``, numbers ``0-9``, or underscores ``_`` If
+ **dataset** argument is :data:`None` then the table argument must
+ contain the entire table reference specified as:
+ ``'PROJECT:DATASET.TABLE'`` or must specify a TableReference.
+ dataset (str): Optional ID of the dataset containing this table or
+ :data:`None` if the table argument specifies a TableReference.
+ project (str): Optional ID of the project containing this table or
+ :data:`None` if the table argument specifies a TableReference.
+ selected_fields (List[str]): Optional List of names of the fields in the
+ table that should be read. If empty, all fields will be read. If the
+ specified field is a nested field, all the sub-fields in the field will
be
+ selected. The output field order is unrelated to the order of fields in
+ selected_fields.
+ row_restriction (str): Optional SQL text filtering statement, similar to a
+ WHERE clause in a query. Aggregates are not supported. Restricted to a
+ maximum length for 1 MB.
+ use_native_datetime (bool): If :data:`True`, BigQuery DATETIME fields will
+ be returned as native Python datetime objects. If :data:`False`,
+ DATETIME fields will be returned as formatted strings (for example:
+ 2021-01-01T12:59:59). The default is :data:`False`.
+ """
+
+ # The maximum number of streams which will be requested when creating a read
+ # session, regardless of the desired bundle size.
+ MAX_SPLIT_COUNT = 10000
+ # The minimum number of streams which will be requested when creating a read
+ # session, regardless of the desired bundle size. Note that the server may
+ # still choose to return fewer than ten streams based on the layout of the
+ # table.
+ MIN_SPLIT_COUNT = 10
+
+ def __init__(
+ self,
+ table: Optional[Union[str, TableReference]] = None,
+ dataset: Optional[str] = None,
+ project: Optional[str] = None,
+ query: Optional[str] = None,
+ selected_fields: Optional[List[str]] = None,
+ row_restriction: Optional[str] = None,
+ pipeline_options: Optional[GoogleCloudOptions] = None,
+ unique_id: Optional[uuid.UUID] = None,
+ bigquery_job_labels: Optional[Dict] = None,
+ job_name: Optional[str] = None,
+ step_name: Optional[str] = None,
+ use_standard_sql: Optional[bool] = False,
+ flatten_results: Optional[bool] = True,
+ kms_key: Optional[str] = None,
+ temp_dataset: Optional[DatasetReference] = None,
+ temp_table: Optional[TableReference] = None,
+ use_native_datetime: Optional[bool] = False):
+
+ if table is not None and query is not None:
+ raise ValueError(
+ 'Both a BigQuery table and a query were specified.'
+ ' Please specify only one of these.')
+ elif table is None and query is None:
+ raise ValueError('A BigQuery table or a query must be specified')
+ elif table is not None:
+ self.table_reference = bigquery_tools.parse_table_reference(
+ table, dataset, project)
+ self.query = None
+ self.use_legacy_sql = True
+ else:
+ if isinstance(query, str):
+ query = StaticValueProvider(str, query)
+ self.query = query
+ # TODO(BEAM-1082): Change the internal flag to be standard_sql
+ self.use_legacy_sql = not use_standard_sql
+ self.table_reference = None
+
+ self.project = project
+ self.selected_fields = selected_fields
+ self.row_restriction = row_restriction
+ self.pipeline_options = pipeline_options
+ self.split_result = None
+ self.bigquery_job_labels = bigquery_job_labels or {}
+ self.bq_io_metadata = None # Populate in setup, as it may make an RPC
+ self.flatten_results = flatten_results
+ self.kms_key = kms_key
+ self.temp_table = temp_table
+ self.use_native_datetime = use_native_datetime
+ self._job_name = job_name or 'BQ_DIRECT_READ_JOB'
+ self._step_name = step_name
+ self._source_uuid = unique_id
+
+ def _get_parent_project(self):
+ """Returns the project that will be billed."""
+ if self.temp_table:
+ return self.temp_table.projectId
+
+ project = self.pipeline_options.view_as(GoogleCloudOptions).project
+ if isinstance(project, vp.ValueProvider):
+ project = project.get()
+ if not project:
+ project = self.project
+ return project
+
+ def _get_table_size(self, bq, table_reference):
+ project = (
+ table_reference.projectId
+ if table_reference.projectId else self._get_parent_project())
+ table = bq.get_table(
+ project, table_reference.datasetId, table_reference.tableId)
+ return table.numBytes
+
+ def _get_bq_metadata(self):
+ if not self.bq_io_metadata:
+ self.bq_io_metadata = create_bigquery_io_metadata(self._step_name)
+ return self.bq_io_metadata
+
+ @check_accessible(['query'])
+ def _setup_temporary_dataset(self, bq):
+ if self.temp_table:
+ # Temp dataset was provided by the user so we can just return.
+ return
+ location = bq.get_query_location(
+ self._get_parent_project(), self.query.get(), self.use_legacy_sql)
+ bq.create_temporary_dataset(
+ self._get_parent_project(), location, {'type': 'apache-beam-temp'})
+
+ @check_accessible(['query'])
+ def _execute_query(self, bq):
+ query_job_name = bigquery_tools.generate_bq_job_name(
+ self._job_name,
+ self._source_uuid,
+ bigquery_tools.BigQueryJobTypes.QUERY,
+ '%s_%s' % (int(time.time()), random.randint(0, 1000)))
+ job = bq._start_query_job(
+ self._get_parent_project(),
+ self.query.get(),
+ self.use_legacy_sql,
+ self.flatten_results,
+ job_id=query_job_name,
+ kms_key=self.kms_key)
+ job_ref = job.jobReference
+ bq.wait_for_bq_job(job_ref, max_retries=0)
+ table_reference = bq._get_temp_table(self._get_parent_project())
+ bq.update_table_labels(
+ table_reference.projectId,
+ table_reference.datasetId,
+ table_reference.tableId, {'type': 'apache-beam-temp'})
Review comment:
I am wondering why labeling with `apache-beam-temp` is needed?
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]