-
Notifications
You must be signed in to change notification settings - Fork 17
Notebook examples #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edurdevic
wants to merge
8
commits into
master
Choose a base branch
from
notebook-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93c6e14
Added slide deck examples
edurdevic a36098b
Updated scan and search examples
edurdevic 9771273
Updated slide deck examples
08f4eae
Merge branch 'master' into notebook-examples
edurdevic 3b4fa07
Added table freshness
d5abb7d
Added delta protocol version check
68da83a
Updated readme
eb3c322
Updated notebooks based on feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Databricks notebook source | ||
# MAGIC %md | ||
# MAGIC # Check delta protocol version | ||
# MAGIC | ||
# MAGIC This notebook will check the delta read and write protocol versions of multiple tables. | ||
# MAGIC | ||
# MAGIC Feature compatibility between delta lake versions is managed through [read protocol](https://docs.delta.io/latest/versioning.html#read-protocol) and [write protocol](https://docs.delta.io/latest/versioning.html#write-protocol). | ||
# MAGIC | ||
# MAGIC Check out the [feature by protocol version table](https://docs.delta.io/latest/versioning.html#features-by-protocol-version) for more details. | ||
|
||
# COMMAND ---------- | ||
|
||
# %pip install dbl-discoverx | ||
|
||
# COMMAND ---------- | ||
|
||
dbutils.widgets.text("from_tables", "sample_data_discoverx.*.*") | ||
from_tables = dbutils.widgets.get("from_tables") | ||
|
||
|
||
# COMMAND ---------- | ||
|
||
from discoverx import DX | ||
|
||
dx = DX() | ||
|
||
|
||
# COMMAND ---------- | ||
|
||
dx.from_tables(from_tables)\ | ||
.with_sql("SHOW TBLPROPERTIES {full_table_name}")\ | ||
.apply()\ | ||
.filter('key = "delta.minReaderVersion" OR key = "delta.minWriterVersion"')\ | ||
.display() | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Show delta feature compatibility | ||
|
||
# COMMAND ---------- | ||
|
||
from pyspark.sql.functions import col, expr | ||
|
||
result = (dx.from_tables(from_tables)\ | ||
.with_sql("SHOW TBLPROPERTIES {full_table_name}") | ||
.apply() | ||
.filter('key = "delta.minReaderVersion" OR key = "delta.minWriterVersion"') | ||
.withColumn("value", col("value").cast("int")) | ||
.groupBy("table_catalog", "table_schema", "table_name") | ||
.pivot("key", ["delta.minWriterVersion", "delta.minReaderVersion"]) | ||
.min("value") | ||
.withColumnRenamed("delta.minReaderVersion", "minReaderVersion") | ||
.withColumnRenamed("delta.minWriterVersion", "minWriterVersion") | ||
.withColumn("supports_basic_functionality", expr("minWriterVersion >= 2 AND minReaderVersion >= 1")) | ||
.withColumn("supports_check_constraint", expr("minWriterVersion >= 3 AND minReaderVersion >= 1")) | ||
.withColumn("supports_change_data_feed", expr("minWriterVersion >= 4 AND minReaderVersion >= 1")) | ||
.withColumn("supports_generated_columns", expr("minWriterVersion >= 4 AND minReaderVersion >= 1")) | ||
.withColumn("supports_column_mapping", expr("minWriterVersion >= 5 AND minReaderVersion >= 2")) | ||
.withColumn("supports_table_features_read", expr("minWriterVersion >= 7 AND minReaderVersion >= 1")) | ||
.withColumn("supports_table_features_write", expr("minWriterVersion >= 7 AND minReaderVersion >= 3")) | ||
.withColumn("supports_deletion_vectors", expr("minWriterVersion >= 7 AND minReaderVersion >= 3")) | ||
.withColumn("supports_timestamp_without_timezone", expr("minWriterVersion >= 7 AND minReaderVersion >= 3")) | ||
.withColumn("supports_iceberg_compatibilty_v1", expr("minWriterVersion >= 7 AND minReaderVersion >= 2")) | ||
.withColumn("supports_v2_checkpoints", expr("minWriterVersion >= 7 AND minReaderVersion >= 3")) | ||
) | ||
|
||
result.display() | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Update protocol version | ||
# MAGIC | ||
# MAGIC You can update the table protocol read and write versions by uncommenting the following snippet. | ||
# MAGIC | ||
# MAGIC !!! BE CAREFUL !!! | ||
# MAGIC | ||
# MAGIC Upgrading a reader or writer version might impact older DBR version's ability to read or write the tables. Check [this page](https://docs.databricks.com/en/delta/feature-compatibility.html#what-delta-lake-features-require-databricks-runtime-upgrades) for more details. | ||
|
||
# COMMAND ---------- | ||
|
||
# (dx.from_tables(from_tables) | ||
# .with_sql("ALTER TABLE {full_table_name} SET TBLPROPERTIES('delta.minWriterVersion' = '5', 'delta.minReaderVersion' = '2')") | ||
# .apply() | ||
# ) | ||
|
||
# COMMAND ---------- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Databricks notebook source | ||
# MAGIC %pip install dbl-discoverx | ||
edurdevic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# COMMAND ---------- | ||
|
||
dbutils.widgets.text("from_tables", "sample_data_discoverx.*.*") | ||
from_tables = dbutils.widgets.get("from_tables") | ||
|
||
# COMMAND ---------- | ||
|
||
from discoverx import DX | ||
|
||
dx = DX() | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Which are the biggest 10 tables in the "sample_data_discoverx" catalog? | ||
# MAGIC | ||
# MAGIC | ||
|
||
# COMMAND ---------- | ||
|
||
from pyspark.sql.functions import col | ||
|
||
(dx | ||
.from_tables("sample_data_discoverx.*.*") | ||
.with_sql("DESCRIBE DETAIL {full_table_name}") | ||
.apply() | ||
.orderBy(col("sizeInBytes").desc()) | ||
.display() | ||
) | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Which tables have the most daily transactions? | ||
|
||
# COMMAND ---------- | ||
|
||
from pyspark.sql.functions import window | ||
|
||
(dx | ||
.from_tables("sample_data_discoverx.*.*") | ||
.with_sql("DESCRIBE HISTORY {full_table_name}") | ||
.apply() | ||
.groupBy("table_catalog", "table_schema", "table_name", window("timestamp", "1 day")) | ||
.count() | ||
.display() | ||
) | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Which tables have too many small files? | ||
|
||
# COMMAND ---------- | ||
|
||
from pyspark.sql.functions import col, lit | ||
|
||
(dx | ||
.from_tables("sample_data_discoverx.*.*") | ||
.with_sql("DESCRIBE DETAIL {full_table_name}") | ||
.apply() | ||
.withColumn("average_file_size", col("sizeInBytes") / col("numFiles")) | ||
.withColumn("has_many_small_files", | ||
(col("average_file_size") < 10000000) & (col("numFiles") > 100)) | ||
.orderBy("average_file_size") | ||
.display() | ||
) | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Which tables contain email addresses? | ||
|
||
# COMMAND ---------- | ||
|
||
result = (dx | ||
.from_tables("sample_data_discoverx.*.*") | ||
.scan() # Returns a Discovery object | ||
) | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Which tables contain the email address “erni@databricks.com”? | ||
|
||
# COMMAND ---------- | ||
|
||
result.search("erni@databricks.com").display() | ||
|
||
# COMMAND ---------- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Databricks notebook source | ||
# MAGIC %pip install dbl-discoverx | ||
|
||
# COMMAND ---------- | ||
|
||
dbutils.widgets.text("from_tables", "sample_data_discoverx.*.*") | ||
from_tables = dbutils.widgets.get("from_tables") | ||
dbutils.widgets.text("time_span", "1 day") | ||
time_span = dbutils.widgets.get("time_span") | ||
|
||
# COMMAND ---------- | ||
|
||
from discoverx import DX | ||
|
||
dx = DX() | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Number of delta table versions per time period | ||
|
||
# COMMAND ---------- | ||
|
||
from pyspark.sql.functions import window, count | ||
|
||
(dx | ||
.from_tables(from_tables) | ||
.with_sql("DESCRIBE HISTORY {full_table_name}") | ||
.apply() | ||
.groupBy("table_catalog", "table_schema", "table_name", window("timestamp", time_span)) | ||
.agg(count("*").alias("delta_versions_count")) | ||
.display() | ||
) | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %md | ||
# MAGIC ## Number of processed rows | ||
|
||
# COMMAND ---------- | ||
|
||
sql_template = f""" | ||
WITH metrics AS ( | ||
SELECT timestamp, operation, explode(operationMetrics) AS (metric, value) | ||
FROM ( | ||
DESCRIBE HISTORY {{full_table_name}} | ||
) | ||
), | ||
|
||
metrics_window AS ( | ||
SELECT window(timestamp, '{time_span}') AS time_window, metric, sum(value) as total_rows | ||
FROM metrics | ||
WHERE metric IN ( | ||
-- Written | ||
"numCopiedRows", | ||
"numUpdatedRows", | ||
"numOutputRows", | ||
-- Deleted | ||
"numDeletedRows", | ||
"numTargetRowsDeleted" | ||
) | ||
GROUP BY 1, 2 | ||
), | ||
|
||
metrics_pivot AS ( | ||
SELECT * | ||
FROM metrics_window | ||
PIVOT (sum(total_rows) as total_rows | ||
FOR (metric) IN ( | ||
-- Written | ||
"numCopiedRows", | ||
"numUpdatedRows", | ||
"numOutputRows", | ||
|
||
-- Deleted | ||
"numDeletedRows", | ||
"numTargetRowsDeleted" | ||
) | ||
) | ||
) | ||
|
||
SELECT | ||
time_window, | ||
-- Written rows include copied, updated and added rows | ||
(COALESCE(numCopiedRows, 0) + COALESCE(numUpdatedRows, 0) + COALESCE(numOutputRows, 0)) AS totNumWrittenRows, | ||
-- Deleted rows from both delete and merge operations | ||
(COALESCE(numDeletedRows, 0) + COALESCE(numTargetRowsDeleted, 0)) AS totNumDeletedRows | ||
FROM metrics_pivot | ||
""" | ||
|
||
processed_rows = (dx | ||
.from_tables(from_tables) | ||
.with_sql(sql_template) | ||
.apply() | ||
).toPandas() | ||
|
||
# COMMAND ---------- | ||
|
||
display(processed_rows) | ||
|
||
# COMMAND ---------- | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.