forked from dagster-io/dagster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_manager.py
More file actions
452 lines (371 loc) · 18.1 KB
/
Copy pathio_manager.py
File metadata and controls
452 lines (371 loc) · 18.1 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
from abc import abstractmethod
from collections.abc import Generator, Sequence
from contextlib import contextmanager
from enum import Enum
from typing import Optional, Union, cast
from dagster import IOManagerDefinition, OutputContext, io_manager
from dagster._config.pythonic_config import ConfigurableIOManagerFactory
from dagster._core.definitions.partitions.utils import TimeWindow
from dagster._core.storage.db_io_manager import (
DbClient,
DbIOManager,
DbTypeHandler,
TablePartitionDimension,
TableSlice,
)
from dagster._core.storage.io_manager import dagster_maintained_io_manager
from google.api_core.exceptions import NotFound
from google.cloud import bigquery
from pydantic import Field
from dagster_gcp.bigquery.utils import setup_gcp_creds
BIGQUERY_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
class BigQueryWriteMode(str, Enum):
TRUNCATE = "truncate"
APPEND = "append"
REPLACE = "replace"
def build_bigquery_io_manager(
type_handlers: Sequence[DbTypeHandler], default_load_type: Optional[type] = None
) -> IOManagerDefinition:
"""Builds an I/O manager definition that reads inputs from and writes outputs to BigQuery.
Args:
type_handlers (Sequence[DbTypeHandler]): Each handler defines how to translate between
slices of BigQuery tables and an in-memory type - e.g. a Pandas DataFrame.
If only one DbTypeHandler is provided, it will be used as the default_load_type.
default_load_type (Type): When an input has no type annotation, load it as this type.
Returns:
IOManagerDefinition
Examples:
.. code-block:: python
from dagster_gcp import build_bigquery_io_manager
from dagster_bigquery_pandas import BigQueryPandasTypeHandler
from dagster import Definitions
@asset(
key_prefix=["my_prefix"],
metadata={"schema": "my_dataset"} # will be used as the dataset in BigQuery
)
def my_table() -> pd.DataFrame: # the name of the asset will be the table name
...
@asset(
key_prefix=["my_dataset"] # my_dataset will be used as the dataset in BigQuery
)
def my_second_table() -> pd.DataFrame: # the name of the asset will be the table name
...
bigquery_io_manager = build_bigquery_io_manager([BigQueryPandasTypeHandler()])
Definitions(
assets=[my_table, my_second_table],
resources={
"io_manager": bigquery_io_manager.configured({
"project" : {"env": "GCP_PROJECT"}
})
}
)
You can set a default dataset to store the assets using the ``dataset`` configuration value of the BigQuery I/O
Manager. This dataset will be used if no other dataset is specified directly on an asset or op.
.. code-block:: python
Definitions(
assets=[my_table],
resources={
"io_manager": bigquery_io_manager.configured({
"project" : {"env": "GCP_PROJECT"}
"dataset": "my_dataset"
})
}
)
On individual assets, you an also specify the dataset where they should be stored using metadata or
by adding a ``key_prefix`` to the asset key. If both ``key_prefix`` and metadata are defined, the metadata will
take precedence.
.. code-block:: python
@asset(
key_prefix=["my_dataset"] # will be used as the dataset in BigQuery
)
def my_table() -> pd.DataFrame:
...
@asset(
# note that the key needs to be "schema"
metadata={"schema": "my_dataset"} # will be used as the dataset in BigQuery
)
def my_other_table() -> pd.DataFrame:
...
For ops, the dataset can be specified by including a "schema" entry in output metadata.
.. code-block:: python
@op(
out={"my_table": Out(metadata={"schema": "my_schema"})}
)
def make_my_table() -> pd.DataFrame:
...
If none of these is provided, the dataset will default to "public".
To only use specific columns of a table as input to a downstream op or asset, add the metadata ``columns`` to the
:py:class:`~dagster.In` or :py:class:`~dagster.AssetIn`.
.. code-block:: python
@asset(
ins={"my_table": AssetIn("my_table", metadata={"columns": ["a"]})}
)
def my_table_a(my_table: pd.DataFrame) -> pd.DataFrame:
# my_table will just contain the data from column "a"
...
If you cannot upload a file to your Dagster deployment, or otherwise cannot
`authenticate with GCP <https://cloud.google.com/docs/authentication/provide-credentials-adc>`_
via a standard method, you can provide a service account key as the ``gcp_credentials`` configuration.
Dagster willstore this key in a temporary file and set ``GOOGLE_APPLICATION_CREDENTIALS`` to point to the file.
After the run completes, the file will be deleted, and ``GOOGLE_APPLICATION_CREDENTIALS`` will be
unset. The key must be base64 encoded to avoid issues with newlines in the keys. You can retrieve
the base64 encoded with this shell command: ``cat $GOOGLE_APPLICATION_CREDENTIALS | base64``
"""
@dagster_maintained_io_manager
@io_manager(config_schema=BigQueryIOManager.to_config_schema()) # pyright: ignore[reportArgumentType]
def bigquery_io_manager(init_context):
"""I/O Manager for storing outputs in a BigQuery database.
Assets will be stored in the dataset and table name specified by their AssetKey.
Subsequent materializations of an asset will overwrite previous materializations of that asset.
Op outputs will be stored in the dataset specified by output metadata (defaults to public) in a
table of the name of the output.
Note that the BigQuery config is mapped to the DB IO manager table hierarchy as follows:
BigQuery DB IO
* project -> database
* dataset -> schema
* table -> table
"""
mgr = DbIOManager(
type_handlers=type_handlers,
db_client=BigQueryClient(write_mode=init_context.resource_config.get("write_mode")),
io_manager_name="BigQueryIOManager",
database=init_context.resource_config["project"],
schema=init_context.resource_config.get("dataset"),
default_load_type=default_load_type,
)
if init_context.resource_config.get("gcp_credentials"):
with setup_gcp_creds(init_context.resource_config.get("gcp_credentials")):
yield mgr
else:
yield mgr
return bigquery_io_manager
class BigQueryIOManager(ConfigurableIOManagerFactory):
"""Base class for an I/O manager definition that reads inputs from and writes outputs to BigQuery.
Examples:
.. code-block:: python
from dagster_gcp import BigQueryIOManager
from dagster_bigquery_pandas import BigQueryPandasTypeHandler
from dagster import Definitions, EnvVar
class MyBigQueryIOManager(BigQueryIOManager):
@staticmethod
def type_handlers() -> Sequence[DbTypeHandler]:
return [BigQueryPandasTypeHandler()]
@asset(
key_prefix=["my_dataset"] # my_dataset will be used as the dataset in BigQuery
)
def my_table() -> pd.DataFrame: # the name of the asset will be the table name
...
defs = Definitions(
assets=[my_table],
resources={
"io_manager": MyBigQueryIOManager(project=EnvVar("GCP_PROJECT"))
}
)
You can set a default dataset to store the assets using the ``dataset`` configuration value of the BigQuery I/O
Manager. This dataset will be used if no other dataset is specified directly on an asset or op.
.. code-block:: python
defs = Definitions(
assets=[my_table],
resources={
"io_manager": MyBigQueryIOManager(project=EnvVar("GCP_PROJECT"), dataset="my_dataset")
}
)
On individual assets, you an also specify the dataset where they should be stored using metadata or
by adding a ``key_prefix`` to the asset key. If both ``key_prefix`` and metadata are defined, the metadata will
take precedence.
.. code-block:: python
@asset(
key_prefix=["my_dataset"] # will be used as the dataset in BigQuery
)
def my_table() -> pd.DataFrame:
...
@asset(
# note that the key needs to be "schema"
metadata={"schema": "my_dataset"} # will be used as the dataset in BigQuery
)
def my_other_table() -> pd.DataFrame:
...
For ops, the dataset can be specified by including a "schema" entry in output metadata.
.. code-block:: python
@op(
out={"my_table": Out(metadata={"schema": "my_schema"})}
)
def make_my_table() -> pd.DataFrame:
...
If none of these is provided, the dataset will default to "public".
To only use specific columns of a table as input to a downstream op or asset, add the metadata ``columns`` to the
:py:class:`~dagster.In` or :py:class:`~dagster.AssetIn`.
.. code-block:: python
@asset(
ins={"my_table": AssetIn("my_table", metadata={"columns": ["a"]})}
)
def my_table_a(my_table: pd.DataFrame) -> pd.DataFrame:
# my_table will just contain the data from column "a"
...
If you cannot upload a file to your Dagster deployment, or otherwise cannot
`authenticate with GCP <https://cloud.google.com/docs/authentication/provide-credentials-adc>`_
via a standard method, you can provide a service account key as the ``gcp_credentials`` configuration.
Dagster will store this key in a temporary file and set ``GOOGLE_APPLICATION_CREDENTIALS`` to point to the file.
After the run completes, the file will be deleted, and ``GOOGLE_APPLICATION_CREDENTIALS`` will be
unset. The key must be base64 encoded to avoid issues with newlines in the keys. You can retrieve
the base64 encoded with this shell command: ``cat $GOOGLE_APPLICATION_CREDENTIALS | base64``
To change the write mode (default is "truncate"), you can set the ``write_mode`` configuration.
Supported modes: "truncate", "replace", "append".
.. code-block:: python
defs = Definitions(
assets=[my_table],
resources={
"io_manager": BigQueryIOManager(
project=EnvVar("GCP_PROJECT"),
write_mode="replace"
)
}
)
"""
project: str = Field(description="The GCP project to use.")
dataset: Optional[str] = Field(
default=None,
description=(
"Name of the BigQuery dataset to use. If not provided, the last prefix before"
" the asset name will be used."
),
)
location: Optional[str] = Field(
default=None,
description=(
"The GCP location. Note: When using PySpark DataFrames, the default"
" location of the project will be used. A custom location can be specified in"
" your SparkSession configuration."
),
)
gcp_credentials: Optional[str] = Field(
default=None,
description=(
"GCP authentication credentials. If provided, a temporary file will be created"
" with the credentials and ``GOOGLE_APPLICATION_CREDENTIALS`` will be set to the"
" temporary file. To avoid issues with newlines in the keys, you must base64"
" encode the key. You can retrieve the base64 encoded key with this shell"
" command: ``cat $GOOGLE_AUTH_CREDENTIALS | base64``"
),
)
temporary_gcs_bucket: Optional[str] = Field(
default=None,
description=(
"When using PySpark DataFrames, optionally specify a temporary GCS bucket to"
" store data. If not provided, data will be directly written to BigQuery."
),
)
timeout: Optional[float] = Field(
default=None,
description=(
"When using Pandas DataFrames, optionally specify a timeout for the BigQuery"
" queries (loading and reading from tables)."
),
)
write_mode: BigQueryWriteMode = Field(
default=BigQueryWriteMode.TRUNCATE,
description="Write mode to use for non-partitioned table cleanup: truncate, append, or replace.",
)
@staticmethod
@abstractmethod
def type_handlers() -> Sequence[DbTypeHandler]: ...
@staticmethod
def default_load_type() -> Optional[type]:
return None
def create_io_manager(self, context) -> Generator:
mgr = DbIOManager(
db_client=BigQueryClient(write_mode=self.write_mode),
io_manager_name="BigQueryIOManager",
database=self.project,
schema=self.dataset,
type_handlers=self.type_handlers(),
default_load_type=self.default_load_type(),
)
if self.gcp_credentials:
with setup_gcp_creds(self.gcp_credentials):
yield mgr
else:
yield mgr
class BigQueryClient(DbClient):
def __init__(self, write_mode: Optional[Union[BigQueryWriteMode, str]] = BigQueryWriteMode.TRUNCATE):
# Coerce string inputs (from raw config) to the enum, fallback to TRUNCATE on invalid values
if write_mode is None:
write_mode = BigQueryWriteMode.TRUNCATE
if isinstance(write_mode, str):
try:
write_mode = BigQueryWriteMode(write_mode)
except ValueError:
write_mode = BigQueryWriteMode.TRUNCATE
self.write_mode = write_mode
def delete_table_slice(self, context: OutputContext, table_slice: TableSlice, connection) -> None:
try:
# If partitioned, keep existing behavior (delete matching partitions)
if table_slice.partition_dimensions:
connection.query(_get_cleanup_statement(table_slice)).result()
return
# Non-partitioned tables: behavior depends on configured write_mode
if self.write_mode == BigQueryWriteMode.TRUNCATE:
connection.query(
f"TRUNCATE TABLE `{table_slice.database}.{table_slice.schema}.{table_slice.table}`"
).result()
elif self.write_mode == BigQueryWriteMode.APPEND:
# Do nothing; preserve existing data and append
return
elif self.write_mode == BigQueryWriteMode.REPLACE:
connection.query(
f"DROP TABLE IF EXISTS `{table_slice.database}.{table_slice.schema}.{table_slice.table}`"
).result()
except NotFound:
# table doesn't exist yet, so ignore the error
pass
@staticmethod
def get_select_statement(table_slice: TableSlice) -> str:
col_str = ", ".join(table_slice.columns) if table_slice.columns else "*"
if table_slice.partition_dimensions:
query = (
f"SELECT {col_str} FROM"
f" `{table_slice.database}.{table_slice.schema}.{table_slice.table}` WHERE\n"
)
return query + _partition_where_clause(table_slice.partition_dimensions)
else:
return f"""SELECT {col_str} FROM `{table_slice.database}.{table_slice.schema}.{table_slice.table}`"""
@staticmethod
def ensure_schema_exists(context: OutputContext, table_slice: TableSlice, connection) -> None:
connection.query(f"CREATE SCHEMA IF NOT EXISTS {table_slice.schema}").result()
@staticmethod
@contextmanager
def connect(context, _): # pyright: ignore[reportIncompatibleMethodOverride]
conn = bigquery.Client(
project=context.resource_config.get("project"),
location=context.resource_config.get("location"),
)
yield conn
def _get_cleanup_statement(table_slice: TableSlice) -> str:
"""Returns a SQL statement that deletes data in the given table to make way for the output data
being written.
"""
if table_slice.partition_dimensions:
query = (
f"DELETE FROM `{table_slice.database}.{table_slice.schema}.{table_slice.table}` WHERE\n"
)
return query + _partition_where_clause(table_slice.partition_dimensions)
else:
return f"TRUNCATE TABLE `{table_slice.database}.{table_slice.schema}.{table_slice.table}`"
def _partition_where_clause(partition_dimensions: Sequence[TablePartitionDimension]) -> str:
return " AND\n".join(
(
_time_window_where_clause(partition_dimension)
if isinstance(partition_dimension.partitions, TimeWindow)
else _static_where_clause(partition_dimension)
)
for partition_dimension in partition_dimensions
)
def _time_window_where_clause(table_partition: TablePartitionDimension) -> str:
partition = cast("TimeWindow", table_partition.partitions)
start_dt, end_dt = partition
start_dt_str = start_dt.strftime(BIGQUERY_DATETIME_FORMAT)
end_dt_str = end_dt.strftime(BIGQUERY_DATETIME_FORMAT)
return f"""{table_partition.partition_expr} >= '{start_dt_str}' AND {table_partition.partition_expr} < '{end_dt_str}'"""
def _static_where_clause(table_partition: TablePartitionDimension) -> str:
partitions = ", ".join(f"'{partition}'" for partition in table_partition.partitions)
return f"""{table_partition.partition_expr} in ({partitions})"""