DataSinkMergeCDCOptions
laktory.models.DataSinkMergeCDCOptions
¤
Bases: BaseModel
Options for merging a change data capture (CDC).
They are also used to build the target using apply_changes method when
using Databricks DLT.
Examples:
from laktory import models
df = spark.createDataFrame(
[
{"id": 1, "value": 3.0},
{"id": 2, "value": 2.3},
{"id": 3, "value": 7.7},
]
)
sink = models.FileDataSink(
path="./my_table/",
format="DELTA",
mode="MERGE",
merge_cdc_options={
"scd_type": 1,
"primary_keys": ["id"],
},
)
# sink.write(df)
References
| PARAMETER | DESCRIPTION |
|---|---|
variables
|
Dict of variables to be injected in the model at runtime
TYPE:
|
delete_where
|
Specifies when a CDC event should be treated as a DELETE rather than an upsert.
TYPE:
|
end_at_column_name
|
When using SCD type 2, name of the column storing the end time (or sequencing index) during which a row is active. This attribute is not used when using Databricks DLT which does not allow column rename.
TYPE:
|
exclude_columns
|
A subset of columns to exclude in the target table.
TYPE:
|
ignore_null_updates
|
Allow ingesting updates containing a subset of the target columns. When a CDC event matches an existing row and
ignore_null_updates is
TYPE:
|
include_columns
|
A subset of columns to include in the target table. Use
TYPE:
|
order_by
|
The column name specifying the logical order of CDC events in the source data. Used to handle change events that arrive out of order.
TYPE:
|
primary_keys
|
The column or combination of columns that uniquely identify a row in the source data. This is used to identify which CDC events apply to specific records in the target table.
TYPE:
|
scd_type
|
Whether to store records as SCD type 1 or SCD type 2.
TYPE:
|
start_at_column_name
|
When using SCD type 2, name of the column storing the start time (or sequencing index) during which a row is active. This attribute is not used when using Databricks DLT which does not allow column rename.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
execute |
Merge source into target delta from sink |
inject_vars |
Inject model variables values into a model attributes. |
inject_vars_into_dump |
Inject model variables values into a model dump. |
model_validate_json_file |
Load model from json file object |
model_validate_yaml |
Load model from yaml file object using laktory.yaml.RecursiveLoader. Supports |
push_vars |
Push variable values to all child recursively |
validate_assignment_disabled |
Updating a model attribute inside a model validator when |
execute(source)
¤
Merge source into target delta from sink
| PARAMETER | DESCRIPTION |
|---|---|
source
|
Source DataFrame to merge into target (sink).
TYPE:
|
Source code in laktory/models/datasinks/mergecdcoptions.py
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 | |
inject_vars(inplace=False, vars=None)
¤
Inject model variables values into a model attributes.
| PARAMETER | DESCRIPTION |
|---|---|
inplace
|
If
TYPE:
|
vars
|
A dictionary of variables to be injected in addition to the model internal variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Model instance. |
Examples:
from typing import Union
from laktory import models
class Cluster(models.BaseModel):
name: str = None
size: Union[int, str] = None
c = Cluster(
name="cluster-${vars.my_cluster}",
size="${{ 4 if vars.env == 'prod' else 2 }}",
variables={
"env": "dev",
},
).inject_vars()
print(c)
# > variables={'env': 'dev'} name='cluster-${vars.my_cluster}' size=2
References
Source code in laktory/models/basemodel.py
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 | |
inject_vars_into_dump(dump, inplace=False, vars=None)
¤
Inject model variables values into a model dump.
| PARAMETER | DESCRIPTION |
|---|---|
dump
|
Model dump (or any other general purpose mutable object)
TYPE:
|
inplace
|
If
TYPE:
|
vars
|
A dictionary of variables to be injected in addition to the model internal variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Model dump with injected variables. |
Examples:
from laktory import models
m = models.BaseModel(
variables={
"env": "dev",
},
)
data = {
"name": "cluster-${vars.my_cluster}",
"size": "${{ 4 if vars.env == 'prod' else 2 }}",
}
print(m.inject_vars_into_dump(data))
# > {'name': 'cluster-${vars.my_cluster}', 'size': 2}
References
Source code in laktory/models/basemodel.py
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 | |
model_validate_json_file(fp)
classmethod
¤
Load model from json file object
| PARAMETER | DESCRIPTION |
|---|---|
fp
|
file object structured as a json file
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Model
|
Model instance |
Source code in laktory/models/basemodel.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
model_validate_yaml(fp, vars=None)
classmethod
¤
Load model from yaml file object using laktory.yaml.RecursiveLoader. Supports
reference to external yaml and sql files using !use, !extend and !update tags.
Path to external files can be defined using model or environment variables.
Referenced path should always be relative to the file they are referenced from.
| PARAMETER | DESCRIPTION |
|---|---|
fp
|
file object structured as a yaml file
TYPE:
|
vars
|
Dict of variables available when parsing filepaths references in yaml files
i.e.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Model
|
Model instance |
Examples:
businesses:
apple:
symbol: aapl
address: !use addresses.yaml
<<: !update common.yaml
emails:
- jane.doe@apple.com
- extend! emails.yaml
amazon:
symbol: amzn
address: !use addresses.yaml
<<: update! common.yaml
emails:
- john.doe@amazon.com
- extend! emails.yaml
Source code in laktory/models/basemodel.py
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 | |
push_vars(update_core_resources=False)
¤
Push variable values to all child recursively
Source code in laktory/models/basemodel.py
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 | |
validate_assignment_disabled()
¤
Updating a model attribute inside a model validator when validate_assignment
is True causes an infinite recursion by design and must be turned off
temporarily.
Source code in laktory/models/basemodel.py
350 351 352 353 354 355 356 357 358 359 360 361 362 | |