-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathcloudsql.py
More file actions
665 lines (567 loc) · 25.2 KB
/
cloudsql.py
File metadata and controls
665 lines (567 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#
# 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.
#
import re
from abc import ABC
from abc import abstractmethod
from collections.abc import Callable
from collections.abc import Mapping
from dataclasses import dataclass
from dataclasses import field
from enum import Enum
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
import pg8000
import pymysql
import pytds
from google.cloud.sql.connector import Connector as CloudSQLConnector
from google.cloud.sql.connector.enums import RefreshStrategy
from sqlalchemy import create_engine
from sqlalchemy import text
from sqlalchemy.engine import Connection as DBAPIConnection
import apache_beam as beam
from apache_beam.transforms.enrichment import EnrichmentSourceHandler
QueryFn = Callable[[beam.Row], str]
ConditionValueFn = Callable[[beam.Row], list[Any]]
@dataclass
class CustomQueryConfig:
"""Configuration for using a custom query function."""
query_fn: QueryFn
def __post_init__(self):
if not self.query_fn:
raise ValueError("CustomQueryConfig must provide a valid query_fn")
@dataclass
class TableFieldsQueryConfig:
"""Configuration for using table name, where clause, and field names."""
table_id: str
where_clause_template: str
where_clause_fields: List[str]
def __post_init__(self):
if not self.table_id or not self.where_clause_template:
raise ValueError(
"TableFieldsQueryConfig must provide table_id and " +
"where_clause_template")
if not self.where_clause_fields:
raise ValueError(
"TableFieldsQueryConfig must provide non-empty " +
"where_clause_fields")
@dataclass
class TableFunctionQueryConfig:
"""Configuration for using table name, where clause, and a value function."""
table_id: str
where_clause_template: str
where_clause_value_fn: ConditionValueFn
def __post_init__(self):
if not self.table_id or not self.where_clause_template:
raise ValueError(
"TableFunctionQueryConfig must provide table_id and " +
"where_clause_template")
if not self.where_clause_value_fn:
raise ValueError(
"TableFunctionQueryConfig must provide " + "where_clause_value_fn")
class DatabaseTypeAdapter(Enum):
POSTGRESQL = "pg8000"
MYSQL = "pymysql"
SQLSERVER = "pytds"
def to_sqlalchemy_dialect(self):
"""Map the adapter type to its corresponding SQLAlchemy dialect.
Returns:
str: SQLAlchemy dialect string.
"""
if self == DatabaseTypeAdapter.POSTGRESQL:
return f"postgresql+{self.value}"
elif self == DatabaseTypeAdapter.MYSQL:
return f"mysql+{self.value}"
elif self == DatabaseTypeAdapter.SQLSERVER:
return f"mssql+{self.value}"
else:
raise ValueError(f"Unsupported database adapter type: {self.name}")
class ConnectionConfig(ABC):
@abstractmethod
def get_connector_handler(self) -> Callable[[], DBAPIConnection]:
pass
@abstractmethod
def get_db_url(self) -> str:
pass
@dataclass
class CloudSQLConnectionConfig(ConnectionConfig):
"""Connects to Google Cloud SQL using Cloud SQL Python Connector.
Args:
db_adapter: The database adapter type (PostgreSQL, MySQL, SQL Server).
instance_connection_uri: URI for connecting to the Cloud SQL instance.
user: Username for authentication.
password: Password for authentication. Defaults to None.
db_id: Database identifier/name.
refresh_strategy: Strategy for refreshing connection (default: LAZY).
connector_kwargs: Additional keyword arguments for the
Cloud SQL Python Connector. Enables forward compatibility.
connect_kwargs: Additional keyword arguments for the client connect
method. Enables forward compatibility.
"""
db_adapter: DatabaseTypeAdapter
instance_connection_uri: str
user: str = field(default_factory=str)
password: str = field(default_factory=str)
db_id: str = field(default_factory=str)
refresh_strategy: RefreshStrategy = RefreshStrategy.LAZY
connector_kwargs: Dict[str, Any] = field(default_factory=dict)
connect_kwargs: Dict[str, Any] = field(default_factory=dict)
def __post_init__(self):
if not self.instance_connection_uri:
raise ValueError("Instance connection URI cannot be empty")
def get_connector_handler(self) -> Callable[[], DBAPIConnection]:
"""Returns a function that creates a new database connection.
The returned connector function creates database connections that should
be properly closed by the caller when no longer needed.
"""
cloudsql_client = CloudSQLConnector(
refresh_strategy=self.refresh_strategy, **self.connector_kwargs)
cloudsql_connector = lambda: cloudsql_client.connect(
instance_connection_string=self.instance_connection_uri, driver=self.
db_adapter.value, user=self.user, password=self.password, db=self.db_id,
**self.connect_kwargs)
return cloudsql_connector
def get_db_url(self) -> str:
return self.db_adapter.to_sqlalchemy_dialect() + "://"
@dataclass
class ExternalSQLDBConnectionConfig(ConnectionConfig):
"""Connects to External SQL DBs (PostgreSQL, MySQL, SQL Server) over TCP.
Args:
db_adapter: The database adapter type (PostgreSQL, MySQL, SQL Server).
host: Hostname or IP address of the database server.
port: Port number for the database connection.
user: Username for authentication.
password: Password for authentication.
db_id: Database identifier/name.
connect_kwargs: Additional keyword arguments for the client connect
method. Enables forward compatibility.
"""
db_adapter: DatabaseTypeAdapter
host: str
port: int
user: str = field(default_factory=str)
password: str = field(default_factory=str)
db_id: str = field(default_factory=str)
connect_kwargs: Dict[str, Any] = field(default_factory=dict)
def __post_init__(self):
if not self.host:
raise ValueError("Database host cannot be empty")
def get_connector_handler(self) -> Callable[[], DBAPIConnection]:
"""Returns a function that creates a new database connection.
The returned connector function creates database connections that should
be properly closed by the caller when no longer needed.
"""
if self.db_adapter == DatabaseTypeAdapter.POSTGRESQL:
return lambda: pg8000.connect(
host=self.host, port=self.port, database=self.db_id, user=self.user,
password=self.password, **self.connect_kwargs)
elif self.db_adapter == DatabaseTypeAdapter.MYSQL:
return lambda: pymysql.connect(
host=self.host, port=self.port, database=self.db_id, user=self.user,
password=self.password, **self.connect_kwargs)
elif self.db_adapter == DatabaseTypeAdapter.SQLSERVER:
return lambda: pytds.connect(
dsn=self.host, port=self.port, database=self.db_id, user=self.user,
password=self.password, **self.connect_kwargs)
else:
raise ValueError(f"Unsupported database adapter: {self.db_adapter}")
def get_db_url(self) -> str:
return self.db_adapter.to_sqlalchemy_dialect() + "://"
QueryConfig = Union[CustomQueryConfig,
TableFieldsQueryConfig,
TableFunctionQueryConfig]
class CloudSQLEnrichmentHandler(EnrichmentSourceHandler[beam.Row, beam.Row]):
"""Enrichment handler for Cloud SQL databases.
This handler is designed to work with the
:class:`apache_beam.transforms.enrichment.Enrichment` transform.
To use this handler, you need to provide one of the following query configs:
* CustomQueryConfig - For providing a custom query function
* TableFieldsQueryConfig - For specifying table, where clause, and fields
* TableFunctionQueryConfig - For specifying table, where clause, and val fn
By default, the handler retrieves all columns from the specified table.
To limit the columns, use the `column_names` parameter to specify
the desired column names.
This handler queries the Cloud SQL database per element by default.
To enable batching, set the `min_batch_size`, `max_batch_size`, and
`max_batch_duration_secs` parameters. These values control batching behavior
in the
:class:`apache_beam.transforms.utils.BatchElements` transform.
NOTE: Batching is not supported when using the CustomQueryConfig.
"""
def __init__(
self,
connection_config: ConnectionConfig,
*,
query_config: QueryConfig,
column_names: Optional[list[str]] = None,
min_batch_size: int = 1,
max_batch_size: int = 10000,
max_batch_duration_secs: Optional[float] = None,
**kwargs,
):
"""
Example usage::
connection_config = CloudSQLConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
instance_connection_uri="apache-beam-testing:us-central1:itests",
user='postgres',
password= os.getenv("CLOUDSQL_PG_PASSWORD"))
query_config=TableFieldsQueryConfig('my_table',"id = :param0",['id']),
cloudsql_handler = CloudSQLEnrichmentHandler(
connection_config=connection_config,
query_config=query_config,
min_batch_size=2,
max_batch_size=100)
Args:
connection_config (ConnectionConfig): Configuration for connecting to
the SQL database. Must be an instance of a subclass of
`ConnectionConfig`, such as `CloudSQLConnectionConfig` or
`ExternalSQLDBConnectionConfig`. This determines how the handler
connects to the target SQL database.
query_config: Configuration for database queries. Must be one of:
* CustomQueryConfig: For providing a custom query function
* TableFieldsQueryConfig: specifies table, where clause, and field names
* TableFunctionQueryConfig: specifies table, where clause, and val func
column_names (Optional[list[str]]): List of column names to select from
the Cloud SQL table. If not provided, all columns (`*`) are selected.
min_batch_size (int): Minimum number of rows to batch together when
querying the database. Defaults to 1 if `query_fn` is not used.
max_batch_size (int): Maximum number of rows to batch together. Defaults
to 10,000 if `query_fn` is not used.
max_batch_duration_secs (float): Maximum amount of time in seconds to
buffer a batch before emitting it. If not provided, batching duration
is determined by `BatchElements` defaults.
**kwargs: Additional keyword arguments for database connection or query
handling.
Note:
* `min_batch_size`, `max_batch_size`, and `max_batch_duration_secs`
are not used with `query_fn`.
* Either `where_clause_fields` or `where_clause_value_fn` must be provided
for query construction if `query_fn` is not provided.
* Ensure that the database user has the necessary permissions to query the
specified table.
"""
self._connection_config = connection_config
self._query_config = query_config
self._column_names = ",".join(column_names) if column_names else "*"
self.kwargs = kwargs
self._batching_kwargs = {}
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
if isinstance(query_config, table_query_configs):
self.query_template = (
f"SELECT {self._column_names} "
f"FROM {query_config.table_id} "
f"WHERE {query_config.where_clause_template}")
self._batching_kwargs['min_batch_size'] = min_batch_size
self._batching_kwargs['max_batch_size'] = max_batch_size
if max_batch_duration_secs is not None:
self._batching_kwargs[
'max_batch_duration_secs'] = max_batch_duration_secs
def __enter__(self):
connector = self._connection_config.get_connector_handler()
self._engine = create_engine(
url=self._connection_config.get_db_url(), creator=connector)
def __call__(
self, request: Union[beam.Row, list[beam.Row]], *_args, **_kwargs):
"""Handle requests by delegating to single or batch processing."""
if isinstance(request, list):
return self._process_batch_request(request)
else:
return self._process_single_request(request)
def _process_single_request(self, request: beam.Row):
"""Process a single request and return with its response."""
response: Union[List[Dict[str, Any]], Dict[str, Any]]
if isinstance(self._query_config, CustomQueryConfig):
query = self._query_config.query_fn(request)
response = self._execute_query(query, is_batch=False)
else:
values = self._extract_values_from_request(request)
param_dict = self._build_single_param_dict(values)
response = self._execute_query(
self.query_template, params=param_dict, is_batch=False)
return request, beam.Row(**response) # type: ignore[arg-type]
def _process_batch_request(self, requests: list[beam.Row]):
"""Process batch requests and match responses to original requests."""
values, responses = [], []
requests_map: dict[Any, Any] = {}
batch_size = len(requests)
# Build the appropriate query (single or batched).
raw_query = self._build_batch_query(requests, batch_size)
# Extract where_clause_fields values and map the generated request key to
# the original request object..
for req in requests:
current_values = self._extract_values_from_request(req)
values.extend(current_values)
requests_map[self.create_row_key(req)] = req
# Build named parameters dictionary for parameterized query.
param_dict = self._build_parameters_dict(requests, batch_size)
# Execute the parameterized query with validated parameters.
result: Union[List[Dict[str, Any]], Dict[str, Any]] = self._execute_query(
raw_query, params=param_dict, is_batch=True)
for response in result:
response_row = beam.Row(**response) # type: ignore[arg-type]
response_key = self.create_row_key(response_row)
if response_key in requests_map:
responses.append((requests_map[response_key], response_row))
return responses
def _execute_query(
self,
query: str,
params: Optional[dict] = None,
is_batch: bool = False) -> Union[List[Dict[str, Any]], Dict[str, Any]]:
connection = None
try:
connection = self._engine.connect()
transaction = connection.begin()
try:
if params:
result = connection.execute(text(query), params)
else:
result = connection.execute(text(query))
# Materialize results while transaction is active.
data: Union[List[Dict[str, Any]], Dict[str, Any]]
if is_batch:
data = [row._asdict() for row in result]
else:
result_row = result.first()
data = result_row._asdict() if result_row else {}
# Explicitly commit the transaction.
transaction.commit()
return data
except Exception as e:
transaction.rollback()
raise RuntimeError(f"Database operation failed: {e}") from e
except Exception as e:
raise Exception(
f'Could not execute the query. Please check if the query is properly '
f'formatted and the table exists. {e}') from e
finally:
if connection:
connection.close()
def _build_batch_query(
self, requests: list[beam.Row], batch_size: int) -> str:
"""Build batched query with unique parameter names for multiple requests.
This method extracts parameter placeholders from the where_clause_template
using regex and creates unique parameter names for each batch item. The
parameter names in the template can be any valid identifiers (e.g., :id,
:param_0, :user_name) and don't need to match field names exactly.
For batch queries, placeholders are replaced with unique names like
:batch_0_id, :batch_1_param_0, etc., based on the actual parameter names
found in the template.
Args:
requests: List of beam.Row requests to process
batch_size: Number of requests in the batch
Returns:
SQL query string with batched WHERE clauses using unique parameter names
"""
# Single request - return original query.
if batch_size <= 1:
return self.query_template
# Only batch table-based query configs.
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
if not isinstance(self._query_config, table_query_configs):
return self.query_template
# Build batched WHERE clauses.
where_clauses = [self._create_batch_clause(i) for i in range(batch_size)]
# Combine clauses and update query.
where_clause_batched = ' OR '.join(where_clauses)
# We know this is a table-based config from the check above.
assert isinstance(self._query_config, table_query_configs)
return self.query_template.replace(
self._query_config.where_clause_template, where_clause_batched)
def _create_batch_clause(self, batch_index: int) -> str:
"""Create a WHERE clause for a single batch item with unique parameter
names."""
# This method is only called for table-based query configs
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
assert isinstance(self._query_config, table_query_configs)
clause = self._query_config.where_clause_template
# Extract parameter names from the template using regex.
param_names = self._extract_parameter_names(
self._query_config.where_clause_template)
for param_name in param_names:
old_param = f':{param_name}'
new_param = f':batch_{batch_index}_{param_name}'
clause = clause.replace(old_param, new_param)
return f'({clause})'
def _build_parameters_dict(
self, requests: list[beam.Row], batch_size: int) -> dict:
"""Build named parameters dictionary for parameterized queries.
Args:
requests: List of beam.Row requests to process
batch_size: Number of requests in the batch
Returns:
Dictionary mapping parameter names to validated values
"""
param_dict = {}
for i, req in enumerate(requests):
current_values = self._extract_values_from_request(req)
# For batched queries, use unique parameter names per batch item.
if batch_size > 1:
# Batching is only used with table-based query configs.
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
assert isinstance(self._query_config, table_query_configs)
batch_param_dict = self._build_single_param_dict(current_values)
# Prefix batch parameters to make them globally unique.
for param_name, val in batch_param_dict.items():
param_dict[f'batch_{i}_{param_name}'] = val
else:
single_param_dict = self._build_single_param_dict(current_values)
param_dict.update(single_param_dict)
return param_dict
def _build_single_param_dict(self, values: list[Any]) -> dict[str, Any]:
"""Build parameter dictionary for single request processing.
Args:
values: List of parameter values
Returns:
Dictionary mapping parameter names to values
"""
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
if not isinstance(self._query_config, table_query_configs):
raise ValueError(
f"Parameter binding not supported for "
f"{type(self._query_config).__name__}")
_, param_dict = self._get_unique_template_and_params(
self._query_config.where_clause_template, values)
return param_dict
def _get_unique_template_and_params(
self, template: str, values: list[Any]) -> tuple[str, dict[str, Any]]:
"""Generate unique binding parameter names for duplicate templates.
Args:
template: SQL template with potentially duplicate binding parameter names
values: List of parameter values
Returns:
Tuple of (updated_template, param_dict) with unique binding names.
"""
param_names = self._extract_parameter_names(template)
unique_param_names = [
f"{param_name}_{i}" if param_names.count(param_name) > 1 else param_name
for i, param_name in enumerate(param_names)
]
# Update template by replacing each parameter occurrence in order.
updated_template = template
param_positions = []
# Find all parameter positions.
for match in re.finditer(r':(\w+)', template):
param_positions.append((match.start(), match.end(), match.group(1)))
# Replace parameters from right to left to avoid position shifts.
for i in reversed(range(len(param_positions))):
start, end, _ = param_positions[i]
unique_name = unique_param_names[i]
updated_template = (
updated_template[:start] + f':{unique_name}' + updated_template[end:])
# Build parameter dictionary.
param_dict = {
unique_name: val
for unique_name, val in zip(unique_param_names, values)
}
return updated_template, param_dict
def _extract_values_from_request(self, request: beam.Row) -> list[Any]:
"""Extract parameter values from a request based on query configuration.
Args:
request: The beam.Row request to extract values from
Returns:
List of parameter values
Raises:
KeyError: If required fields are missing from the request
"""
try:
if isinstance(self._query_config, TableFunctionQueryConfig):
return [
val for val in self._query_config.where_clause_value_fn(request)
]
elif isinstance(self._query_config, TableFieldsQueryConfig):
request_dict = request._asdict()
return [
request_dict[field]
for field in self._query_config.where_clause_fields
]
else:
raise ValueError("Unsupported query configuration type")
except KeyError as e:
raise KeyError(
"Make sure the values passed in `where_clause_fields` are "
"the keys in the input `beam.Row`." + str(e))
def _extract_parameter_names(self, template: str) -> list[str]:
"""Extract parameter names from a SQL template string.
Args:
template: SQL template string with named parameters (e.g., "id = :id")
Returns:
List of parameter names found in the template (e.g., ["id"])
"""
return re.findall(r':(\w+)', template)
def create_row_key(self, row: beam.Row):
if isinstance(self._query_config, TableFunctionQueryConfig):
return tuple(self._query_config.where_clause_value_fn(row))
if isinstance(self._query_config, TableFieldsQueryConfig):
row_dict = row._asdict()
return (
tuple(
row_dict[where_clause_field]
for where_clause_field in self._query_config.where_clause_fields))
raise ValueError(
"Either where_clause_fields or where_clause_value_fn must be specified")
def get_cache_key(self, request: Union[beam.Row, list[beam.Row]]):
if isinstance(self._query_config, CustomQueryConfig):
raise NotImplementedError(
"Caching is not supported for CustomQueryConfig. "
"Consider using TableFieldsQueryConfig or " +
"TableFunctionQueryConfig instead.")
if isinstance(request, list):
cache_keys = []
for req in request:
req_dict = req._asdict()
try:
if isinstance(self._query_config, TableFunctionQueryConfig):
current_values = self._query_config.where_clause_value_fn(req)
elif isinstance(self._query_config, TableFieldsQueryConfig):
current_values = [
req_dict[field]
for field in self._query_config.where_clause_fields
]
key = ';'.join(map(repr, current_values))
cache_keys.append(key)
except KeyError as e:
raise KeyError(
"Make sure the values passed in `where_clause_fields` are the "
"keys in the input `beam.Row`." + str(e))
return cache_keys
else:
req_dict = request._asdict()
try:
if isinstance(self._query_config, TableFunctionQueryConfig):
current_values = self._query_config.where_clause_value_fn(request)
else: # TableFieldsQueryConfig.
current_values = [
req_dict[field]
for field in self._query_config.where_clause_fields
]
key = ";".join(["%s"] * len(current_values))
cache_key = key % tuple(current_values)
except KeyError as e:
raise KeyError(
"Make sure the values passed in `where_clause_fields` are the "
"keys in the input `beam.Row`." + str(e))
return cache_key
def __exit__(self, _exc_type, _exc_val, _exc_tb):
self._engine.dispose(close=True)
self._engine = None
def batch_elements_kwargs(self) -> Mapping[str, Any]:
"""Returns a kwargs suitable for `beam.BatchElements`."""
return self._batching_kwargs