dlt-utils-lib 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dlt_utils/__init__.py +0 -0
- dlt_utils/dlt_transformation.py +23 -0
- dlt_utils/dlt_utils.py +44 -0
- dlt_utils/main_dlt_utils.py +59 -0
- dlt_utils_lib-0.1.dist-info/METADATA +6 -0
- dlt_utils_lib-0.1.dist-info/RECORD +8 -0
- dlt_utils_lib-0.1.dist-info/WHEEL +5 -0
- dlt_utils_lib-0.1.dist-info/top_level.txt +1 -0
dlt_utils/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
def apply_partitions(df: DataFrame, partitions: dict):
|
|
2
|
+
# apply partitioning to the dataframe
|
|
3
|
+
if partitions:
|
|
4
|
+
for col_name, expression in partitions.items():
|
|
5
|
+
if expression.replace(" ", "") != '':
|
|
6
|
+
df = df.withColumn(col_name, expr(expression))
|
|
7
|
+
return df
|
|
8
|
+
|
|
9
|
+
def update_cdc_timestamp(df: DataFrame) -> DataFrame:
|
|
10
|
+
# if cdc_timestamp is null or time difference is greater than threshold, set it to the max timestamp in the row
|
|
11
|
+
time_diff_threshold = 5 # 5 days
|
|
12
|
+
timestamp_cols = [col.name for col in df.schema.fields if isinstance(col.dataType, (TimestampType, DateType)) and col.name != 'cdc_timestamp']
|
|
13
|
+
if timestamp_cols:
|
|
14
|
+
max_timestamp_per_row = greatest(*[col(col_name) for col_name in timestamp_cols])
|
|
15
|
+
df = df.withColumn(
|
|
16
|
+
'cdc_timestamp',
|
|
17
|
+
when(
|
|
18
|
+
col('cdc_timestamp').isNull() |
|
|
19
|
+
(spark_abs(datediff(max_timestamp_per_row, col('cdc_timestamp'))) > time_diff_threshold),
|
|
20
|
+
max_timestamp_per_row
|
|
21
|
+
).otherwise(col('cdc_timestamp'))
|
|
22
|
+
)
|
|
23
|
+
return df
|
dlt_utils/dlt_utils.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from pyspark.sql.functions import col, expr
|
|
2
|
+
import dlt
|
|
3
|
+
|
|
4
|
+
def create_bronze_table_definition(table_name, files_path, file_format, schema_exclude_columns):
|
|
5
|
+
@dlt.table(
|
|
6
|
+
name=f"bronze_{table_name}",
|
|
7
|
+
comment="This is the bronze table.",
|
|
8
|
+
temporary=False
|
|
9
|
+
)
|
|
10
|
+
def transform_cdc_to_bronze():
|
|
11
|
+
df = spark.read.format(file_format).load(files_path)
|
|
12
|
+
fields = [field for field in df.schema.fields if field.name not in schema_exclude_columns]
|
|
13
|
+
schema_string = ', '.join([f"{field.name} {field.dataType.simpleString()}" for field in fields])
|
|
14
|
+
return spark \
|
|
15
|
+
.readStream \
|
|
16
|
+
.format('cloudFiles') \
|
|
17
|
+
.option("cloudFiles.format", file_format) \
|
|
18
|
+
.option("cloudFiles.schemaHints", schema_string) \
|
|
19
|
+
.load(files_path) \
|
|
20
|
+
.withColumn('cdc_timestamp', col('cdc_timestamp').cast('timestamp'))
|
|
21
|
+
|
|
22
|
+
return transform_cdc_to_bronze
|
|
23
|
+
|
|
24
|
+
def silver_streaming_process(table_name, keys, partition_col, exclude_columns):
|
|
25
|
+
dlt.create_streaming_table(
|
|
26
|
+
name=table_name,
|
|
27
|
+
table_properties={
|
|
28
|
+
"delta.autoOptimize.optimizeWrite": "true",
|
|
29
|
+
"delta.autoOptimize.autoCompact": "true"
|
|
30
|
+
},
|
|
31
|
+
comment="This is the silver table with source in",
|
|
32
|
+
partition_cols=partition_col if partition_col else None
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
dlt.apply_changes(
|
|
36
|
+
target=table_name,
|
|
37
|
+
source=f"bronze_{table_name}",
|
|
38
|
+
keys=keys,
|
|
39
|
+
sequence_by=col("cdc_timestamp"),
|
|
40
|
+
apply_as_deletes=expr("Op = 'D'"),
|
|
41
|
+
apply_as_truncates=expr("Op = 'T'"),
|
|
42
|
+
except_column_list=["Op", "_rescued_data"] + exclude_columns,
|
|
43
|
+
stored_as_scd_type=1
|
|
44
|
+
)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import dlt
|
|
2
|
+
from pyspark.sql.functions import col, expr, greatest, when, datediff, abs as spark_abs
|
|
3
|
+
from dlt_transformations import update_cdc_timestamp, apply_partitions
|
|
4
|
+
from pyspark.sql.types import TimestampType, DateType
|
|
5
|
+
from pyspark.sql import DataFrame
|
|
6
|
+
from pyspark.sql import SparkSession
|
|
7
|
+
|
|
8
|
+
# Assuming SparkSession is already created in your Databricks notebook
|
|
9
|
+
spark = SparkSession.builder.appName("DLT Pipeline").getOrCreate()
|
|
10
|
+
spark.conf.set("pipelines.tableManagedByMultiplePipelinesCheck.enabled", "false")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Define the transformation function dynamically
|
|
14
|
+
def create_bronze_table_definition(table_name: str, files_path: str, file_format: str, partitions: dict, schema_exclude_columns: list):
|
|
15
|
+
@dlt.table(
|
|
16
|
+
name=f"bronze_{table_name}",
|
|
17
|
+
comment="This is the bronze table.",
|
|
18
|
+
temporary=False
|
|
19
|
+
)
|
|
20
|
+
def transform_cdc_to_bronze():
|
|
21
|
+
df = spark.read.format(file_format).load(files_path)
|
|
22
|
+
fields = [field for field in df.schema.fields if field.name not in schema_exclude_columns]
|
|
23
|
+
schema_string = ', '.join([f"{field.name} {field.dataType.simpleString()}" for field in fields])
|
|
24
|
+
return spark \
|
|
25
|
+
.readStream \
|
|
26
|
+
.format('cloudFiles') \
|
|
27
|
+
.option("cloudFiles.format", file_format) \
|
|
28
|
+
.option("cloudFiles.schemaHints", schema_string) \
|
|
29
|
+
.load(files_path) \
|
|
30
|
+
.withColumn('cdc_timestamp', col('cdc_timestamp').cast('timestamp')) \
|
|
31
|
+
.transform(update_cdc_timestamp) \
|
|
32
|
+
.transform(lambda df: apply_partitions(df, partitions))
|
|
33
|
+
|
|
34
|
+
return transform_cdc_to_bronze
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# Create silver streaming data
|
|
39
|
+
def silver_streaming_process(table_name: str, keys: list, partitions: dict, exclude_columns: list):
|
|
40
|
+
dlt.create_streaming_table(
|
|
41
|
+
name=table_name,
|
|
42
|
+
table_properties={
|
|
43
|
+
"delta.autoOptimize.optimizeWrite": "true",
|
|
44
|
+
"delta.autoOptimize.autoCompact": "true"
|
|
45
|
+
},
|
|
46
|
+
comment = "This is the silver table with source in",
|
|
47
|
+
partition_cols=partitions if partitions else None
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
dlt.apply_changes(
|
|
51
|
+
target=table_name,
|
|
52
|
+
source=f"bronze_{table_name}",
|
|
53
|
+
keys=keys,
|
|
54
|
+
sequence_by=col("cdc_timestamp"),
|
|
55
|
+
apply_as_deletes=expr("Op = 'D'"),
|
|
56
|
+
apply_as_truncates=expr("Op = 'T'"),
|
|
57
|
+
except_column_list=["Op", "_rescued_data"] + exclude_columns,
|
|
58
|
+
stored_as_scd_type=1
|
|
59
|
+
)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
dlt_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
dlt_utils/dlt_transformation.py,sha256=PPvNi4pVtP5fM69Zpzm1VvagA0e93B7augVzEB_WeGU,1109
|
|
3
|
+
dlt_utils/dlt_utils.py,sha256=KzZjLgWtACns3kQJI9O-1F030xCvgJPDcT3T6Il0NZg,1691
|
|
4
|
+
dlt_utils/main_dlt_utils.py,sha256=goPB2SsoHx6IeX7BYSn_eGZmyxA4emN-tT6Px-LK1Ao,2408
|
|
5
|
+
dlt_utils_lib-0.1.dist-info/METADATA,sha256=AXnvlFI4UetjhIEmvUbAiUb3KrhOXRYkBOepSDrdaQg,106
|
|
6
|
+
dlt_utils_lib-0.1.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
|
7
|
+
dlt_utils_lib-0.1.dist-info/top_level.txt,sha256=KQIiBkukfDFtKV6NmbNK651_k5Oj0jLk5dpU0ZO1xuQ,10
|
|
8
|
+
dlt_utils_lib-0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dlt_utils
|