<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>data quality Archives - Albert Nogués</title>
	<atom:link href="https://www.albertnogues.com/tag/data-quality/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.albertnogues.com/tag/data-quality/</link>
	<description>Data and Cloud Freelancer</description>
	<lastBuildDate>Fri, 31 May 2024 11:17:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.albertnogues.com/wp-content/uploads/2020/12/cropped-cropped-AlbertLogo2-32x32.png</url>
	<title>data quality Archives - Albert Nogués</title>
	<link>https://www.albertnogues.com/tag/data-quality/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Data Quality Checks with Soda-Core in Databricks</title>
		<link>https://www.albertnogues.com/data-quality-checks-with-soda-core-in-databricks/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=data-quality-checks-with-soda-core-in-databricks</link>
		
		<dc:creator><![CDATA[Albert]]></dc:creator>
		<pubDate>Fri, 31 May 2024 11:17:06 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Databricks]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data quality]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[soda]]></category>
		<category><![CDATA[spark]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://www.albertnogues.com/?p=3439</guid>

					<description><![CDATA[<p>It&#8217;s easy to do data quality checks when working with spark with the soda-core library. The library has support for spark dataframes. I&#8217;ve tested it within a databricks environment and it worked quite easily for me. For the examples of this article i am loading the customers table from the tpch delta tables in the &#8230; </p>
<p>The post <a href="https://www.albertnogues.com/data-quality-checks-with-soda-core-in-databricks/">Data Quality Checks with Soda-Core in Databricks</a> appeared first on <a href="https://www.albertnogues.com">Albert Nogués</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>It&#8217;s easy to do data quality checks when working with spark with the soda-core library. The library has support for spark dataframes. I&#8217;ve tested it within a databricks environment and it worked quite easily for me.</p>



<p>For the examples of this article i am loading the customers table from the tpch delta tables in the databricks-datasets folder.</p>



<p>First of all we need to install the library either scoped to our Databricks notebook or on our cluster. In my case i will install it notebook scoped:</p>



<pre class="wp-block-code"><code>%pip install soda-core-spark-df</code></pre>



<p>Then we create a dataframe from the tpch customers table:</p>



<pre class="wp-block-code"><code>#We create a table and read it into a dataframe
customer_df = spark.read.table("delta.`/databricks-datasets/tpch/delta-001/customer/`")</code></pre>



<p>We create a temporary view for our dataframe so soda can query the data and run the checks:</p>



<pre class="wp-block-code"><code>#We create a TempView
customer_df.createOrReplaceTempView("customer")</code></pre>



<p>And here it comes the whole soda core. We will define the checks using yaml syntax:</p>



<pre class="wp-block-code"><code>from soda.scan import Scan
scan = Scan()
scan.set_scan_definition_name("Databricks Test Notebook")
scan.set_data_source_name("customer")
scan.add_spark_session(spark, data_source_name="customer")
#YAML Format
checks = '''
checks for customer:
  - row_count > 0
  - invalid_percent(c_phone) = 0:
      valid regex: ^&#91;0-9]{2}&#91;-]&#91;0-9]{3}&#91;-]&#91;0-9]{3}&#91;-]&#91;0-9]{4}$
  - duplicate_count(c_phone) = 0:
      name: No duplicate phone numbers
  - invalid_count(c_mktsegment) = 0:
      invalid values: &#91;HOUSEHOLD]
      name: HOUSEHOLD is not allowed as a Market Segment
'''
# you can use add_sodacl_yaml_file(s). Useful if the tests are in a github repo or FS
scan.add_sodacl_yaml_str(checks)
scan.execute()
print(scan.get_logs_text())</code></pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="570" height="322" src="https://www.albertnogues.com/wp-content/uploads/2024/05/Output1.png" alt="" class="wp-image-3440" srcset="https://www.albertnogues.com/wp-content/uploads/2024/05/Output1.png 570w, https://www.albertnogues.com/wp-content/uploads/2024/05/Output1-300x169.png 300w, https://www.albertnogues.com/wp-content/uploads/2024/05/Output1-106x60.png 106w" sizes="(max-width: 570px) 100vw, 570px" /></figure>
</div>


<p>More info: <a href="https://docs.soda.io/soda/quick-start-databricks.html">Add Soda to a Databricks notebook | Soda Documentation</a></p>



<p>List of validations: <a href="https://docs.soda.io/soda-cl/validity-metrics.html">Validity metrics | Soda Documentation</a> and <a href="https://docs.soda.io/soda-cl/metrics-and-checks.html">SodaCL metrics and checks | Soda Documentation</a></p>



<p>We can somewhat enhance it and generate a Spark Dataframe all out of the list of our warnings or error validation checks:</p>



<pre class="wp-block-code"><code>from datetime import datetime
schema_checks = 'datasource STRING, table STRING, rule_name STRING, rule STRING, column STRING, check_status STRING, number_of_errors_in_sample INT, check_time TIMESTAMP'
list_of_checks = &#91;]
for c in scan.get_scan_results()&#91;'checks']:
    list_of_checks = list_of_checks + &#91;&#91;scan.get_scan_results()&#91;'defaultDataSource'], c&#91;'table'], c&#91;'name'], c&#91;'definition'], c&#91;'column'], c&#91;'outcome'], 0 if 'pass'in c&#91;'outcome'] else int(c&#91;'diagnostics']&#91;'blocks']&#91;0]&#91;'totalFailingRows']), datetime.strptime(scan.get_scan_results()&#91;'dataTimestamp'], '%Y-%m-%dT%H:%M:%S%z')]]
list_of_checks_df = spark.createDataFrame(list_of_checks,schema_checks)
display(list_of_checks_df)</code></pre>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="1024" height="403" src="https://www.albertnogues.com/wp-content/uploads/2024/05/DataFrameOutput-1024x403.png" alt="" class="wp-image-3441" style="width:840px;height:auto" srcset="https://www.albertnogues.com/wp-content/uploads/2024/05/DataFrameOutput-1024x403.png 1024w, https://www.albertnogues.com/wp-content/uploads/2024/05/DataFrameOutput-300x118.png 300w, https://www.albertnogues.com/wp-content/uploads/2024/05/DataFrameOutput-768x302.png 768w, https://www.albertnogues.com/wp-content/uploads/2024/05/DataFrameOutput-152x60.png 152w, https://www.albertnogues.com/wp-content/uploads/2024/05/DataFrameOutput.png 1328w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>In the case we have the yaml file in our github repo, we can read it and pass it. Or If we are working with Databricks repos and the file is part of out repo we can load it locally</p>



<p>Accessing a remote file and reading it with requests:</p>



<pre class="wp-block-code"><code>#Trying to use a remote yaml file to enforce rules. We can upload it to a github of our own and use it in opur notebook.
#I've created a public repo so i dont need to authenticate to github, but in a real world scenario we should use private repo + secret scopes
customer_quality_rules = 'https://raw.githubusercontent.com/anogues/soda-core-quality-rules/main/soda-core-quality-rules-customer.yaml'
import requests
scan.add_sodacl_yaml_str(requests.get(customer_quality_rules).text)</code></pre>



<p>Or we can load it locally if we are using databricks repos:</p>



<pre class="wp-block-code"><code>scan.add_sodacl_yaml_file("your_file.yaml")</code></pre>
<p>The post <a href="https://www.albertnogues.com/data-quality-checks-with-soda-core-in-databricks/">Data Quality Checks with Soda-Core in Databricks</a> appeared first on <a href="https://www.albertnogues.com">Albert Nogués</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
