Skip to content

Commit df61084

Browse files
committed
plumb pipeline options
1 parent 9f471c3 commit df61084

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

sdks/python/apache_beam/io/gcp/bigquery.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,8 @@ def __init__(
13981398
with_batched_input=False,
13991399
ignore_unknown_columns=False,
14001400
max_retries=MAX_INSERT_RETRIES,
1401-
max_insert_payload_size=MAX_INSERT_PAYLOAD_SIZE):
1401+
max_insert_payload_size=MAX_INSERT_PAYLOAD_SIZE,
1402+
options=None):
14021403
"""Initialize a WriteToBigQuery transform.
14031404
14041405
Args:
@@ -1489,6 +1490,7 @@ def __init__(
14891490
self.ignore_unknown_columns = ignore_unknown_columns
14901491
self._max_retries = max_retries
14911492
self._max_insert_payload_size = max_insert_payload_size
1493+
self.options = options
14921494

14931495
def display_data(self):
14941496
return {
@@ -1524,6 +1526,10 @@ def get_table_schema(schema):
15241526
return bigquery_tools.parse_table_schema_from_json(schema)
15251527
elif isinstance(schema, dict):
15261528
return bigquery_tools.parse_table_schema_from_json(json.dumps(schema))
1529+
elif isinstance(schema, (tuple, list)):
1530+
return tuple(schema)
1531+
elif hasattr(schema, 'fields'):
1532+
return BigQueryWriteFn.get_table_schema(schema.fields)
15271533
else:
15281534
raise TypeError('Unexpected schema argument: %s.' % schema)
15291535

@@ -1532,7 +1538,7 @@ def start_bundle(self):
15321538

15331539
if not self.bigquery_wrapper:
15341540
self.bigquery_wrapper = bigquery_tools.BigQueryWrapper(
1535-
client=self.test_client)
1541+
client=self.test_client, pipeline_options=self.options)
15361542

15371543
(
15381544
bigquery_tools.BigQueryWrapper.HISTOGRAM_METRIC_LOGGER.
@@ -1876,7 +1882,8 @@ def expand(self, input):
18761882
ignore_unknown_columns=self.ignore_unknown_columns,
18771883
with_batched_input=self.with_auto_sharding,
18781884
max_retries=self._max_retries,
1879-
max_insert_payload_size=self._max_insert_payload_size)
1885+
max_insert_payload_size=self._max_insert_payload_size,
1886+
options=input.pipeline.options)
18801887

18811888
def _add_random_shard(element):
18821889
key = element[0]

sdks/python/apache_beam/io/gcp/bigquery_tools.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,14 @@ class BigQueryWrapper(object):
369369

370370
HISTOGRAM_METRIC_LOGGER = MetricLogger()
371371

372-
def __init__(self, client=None, temp_dataset_id=None, temp_table_ref=None):
373-
self.client = client or BigQueryWrapper._bigquery_client(PipelineOptions())
372+
def __init__(
373+
self,
374+
client=None,
375+
temp_dataset_id=None,
376+
temp_table_ref=None,
377+
pipeline_options=None):
378+
self.client = client or BigQueryWrapper._bigquery_client(
379+
pipeline_options or PipelineOptions())
374380
self.gcp_bq_client = self.client
375381

376382
self._unique_row_id = 0
@@ -1362,7 +1368,8 @@ def convert_row_to_dict(self, row, schema):
13621368
@staticmethod
13631369
def from_pipeline_options(pipeline_options: PipelineOptions):
13641370
return BigQueryWrapper(
1365-
client=BigQueryWrapper._bigquery_client(pipeline_options))
1371+
client=BigQueryWrapper._bigquery_client(pipeline_options),
1372+
pipeline_options=pipeline_options)
13661373

13671374
@staticmethod
13681375
def _bigquery_client(pipeline_options: PipelineOptions):
@@ -1650,6 +1657,8 @@ def table_schema_to_dict(table_schema):
16501657
def get_table_field(field):
16511658
"""Create a dictionary representation of a table field
16521659
"""
1660+
if isinstance(field, dict):
1661+
return field
16531662
result = {}
16541663
result['name'] = field.name
16551664
result['type'] = getattr(field, 'field_type', getattr(field, 'type', None))
@@ -1705,14 +1714,16 @@ def get_bq_tableschema(schema):
17051714
Returns:
17061715
Sequence[``google.cloud.bigquery.schema.SchemaField``]: The schema as a TableSchema object.
17071716
"""
1708-
if (isinstance(schema, (tuple, value_provider.ValueProvider)) or
1717+
if (isinstance(schema, (tuple, list, value_provider.ValueProvider)) or
17091718
callable(schema) or schema is None):
1710-
return schema
1719+
return tuple(schema) if isinstance(schema, list) else schema
17111720
elif isinstance(schema, str):
17121721
return get_table_schema_from_string(schema)
17131722
elif isinstance(schema, dict):
17141723
schema_string = json.dumps(schema)
17151724
return parse_table_schema_from_json(schema_string)
1725+
elif hasattr(schema, 'fields'):
1726+
return get_bq_tableschema(schema.fields)
17161727
else:
17171728
raise TypeError('Unexpected schema argument: %s.' % schema)
17181729

0 commit comments

Comments
 (0)