<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: sampleBy stream in DLT in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/sampleby-stream-in-dlt/m-p/101165#M40569</link>
    <description>&lt;P&gt;You can create a stratified sample of your delta table using the `sampleBy` function in Databricks. However, DLT&amp;nbsp; does not support the `sampleBy` function directly. To work around this, you can create a notebook that uses the `sampleBy` function to create a stratified sample of your delta table and then use DLT to schedule and run the notebook.&lt;/P&gt;
&lt;P&gt;To create a stratified sample of your delta table that grows incrementally, you can use the following approach:&lt;/P&gt;
&lt;P&gt;1. Create a new delta table that will store the stratified sample.&lt;BR /&gt;2. Use the `sampleBy` function to create an initial stratified sample of the source table.&lt;BR /&gt;3. Write the sampled data to the new delta table.&lt;BR /&gt;4. Create a streaming query that reads new data from the source table using Auto Loader and applies the `sampleBy` function to the new data.&lt;BR /&gt;5. Write the sampled data to the new delta table.&lt;/P&gt;
&lt;P&gt;Here's some sample code that demonstrates this approach:&lt;BR /&gt;```python&lt;BR /&gt;# Create a new delta table for the stratified sample&lt;BR /&gt;spark.sql("CREATE TABLE sampled_table USING DELTA LOCATION '/path/to/sample/table'")&lt;/P&gt;
&lt;P&gt;# Create an initial stratified sample of the source table&lt;BR /&gt;sampled_data = spark.read.table("source_table").sampleBy("strata_column", fractions={"strata_value1": 0.1, "strata_value2": 0.2})&lt;/P&gt;
&lt;P&gt;# Write the sampled data to the new delta table&lt;BR /&gt;sampled_data.write.format("delta").save("/path/to/sample/table")&lt;/P&gt;
&lt;P&gt;# Create a streaming query that reads new data from the source table using Auto Loader&lt;BR /&gt;streaming_query = (spark.readStream&lt;BR /&gt;.format("cloudFiles")&lt;BR /&gt;.option("cloudFiles.format", "parquet")&lt;BR /&gt;.option("cloudFiles.schemaLocation", "/path/to/schema/location")&lt;BR /&gt;.load("/path/to/source/table")&lt;BR /&gt;.createOrReplaceTempView("streaming_data"))&lt;/P&gt;
&lt;P&gt;# Apply the sampleBy function to the new data and write it to the new delta table&lt;BR /&gt;spark.sql("""&lt;BR /&gt;SELECT * FROM streaming_data TABLESAMPLE(sampleBy('strata_column', fractions={"strata_value1": 0.1, "strata_value2": 0.2}))&lt;BR /&gt;""").writeStream&lt;BR /&gt;.format("delta")&lt;BR /&gt;.option("checkpointLocation", "/path/to/checkpoint/location")&lt;BR /&gt;.start("/path/to/sample/table")&lt;BR /&gt;```&lt;BR /&gt;In this example, replace `/path/to/source/table` with the path to your source delta table, `/path/to/sample/table` with the path to the new delta table for the stratified sample, and `/path/to/schema/location` with the path to the schema location for Auto Loader. You can also adjust the `fractions` parameter in the `sampleBy` function to specify the desired sampling fractions for each strata value.&lt;/P&gt;
&lt;P&gt;Note that this approach assumes that the new data added to the source table has the same schema as the existing data. If the schema changes, you may need to update the schema location for Auto Loader and/or modify the streaming query to handle the schema changes.&lt;/P&gt;</description>
    <pubDate>Fri, 06 Dec 2024 06:31:30 GMT</pubDate>
    <dc:creator>Sidhant07</dc:creator>
    <dc:date>2024-12-06T06:31:30Z</dc:date>
    <item>
      <title>sampleBy stream in DLT</title>
      <link>https://community.databricks.com/t5/data-engineering/sampleby-stream-in-dlt/m-p/92670#M38499</link>
      <description>&lt;P&gt;I would like to create a sampleBy (stratified version of sample) copy/clone of my delta table.&amp;nbsp; &amp;nbsp;Ideally, I'd like to do this using a DLT.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My source table grows incrementally each month as batch files are added and autoloader picks them up.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally, I would like to add a new layer of sampled as it grows incrementally rather than have to regenerate the whole table (very large source table).&amp;nbsp; &amp;nbsp;But, can't seem to find a way to do this with a stream&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 15:47:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sampleby-stream-in-dlt/m-p/92670#M38499</guid>
      <dc:creator>lprevost</dc:creator>
      <dc:date>2024-10-03T15:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: sampleBy stream in DLT</title>
      <link>https://community.databricks.com/t5/data-engineering/sampleby-stream-in-dlt/m-p/101165#M40569</link>
      <description>&lt;P&gt;You can create a stratified sample of your delta table using the `sampleBy` function in Databricks. However, DLT&amp;nbsp; does not support the `sampleBy` function directly. To work around this, you can create a notebook that uses the `sampleBy` function to create a stratified sample of your delta table and then use DLT to schedule and run the notebook.&lt;/P&gt;
&lt;P&gt;To create a stratified sample of your delta table that grows incrementally, you can use the following approach:&lt;/P&gt;
&lt;P&gt;1. Create a new delta table that will store the stratified sample.&lt;BR /&gt;2. Use the `sampleBy` function to create an initial stratified sample of the source table.&lt;BR /&gt;3. Write the sampled data to the new delta table.&lt;BR /&gt;4. Create a streaming query that reads new data from the source table using Auto Loader and applies the `sampleBy` function to the new data.&lt;BR /&gt;5. Write the sampled data to the new delta table.&lt;/P&gt;
&lt;P&gt;Here's some sample code that demonstrates this approach:&lt;BR /&gt;```python&lt;BR /&gt;# Create a new delta table for the stratified sample&lt;BR /&gt;spark.sql("CREATE TABLE sampled_table USING DELTA LOCATION '/path/to/sample/table'")&lt;/P&gt;
&lt;P&gt;# Create an initial stratified sample of the source table&lt;BR /&gt;sampled_data = spark.read.table("source_table").sampleBy("strata_column", fractions={"strata_value1": 0.1, "strata_value2": 0.2})&lt;/P&gt;
&lt;P&gt;# Write the sampled data to the new delta table&lt;BR /&gt;sampled_data.write.format("delta").save("/path/to/sample/table")&lt;/P&gt;
&lt;P&gt;# Create a streaming query that reads new data from the source table using Auto Loader&lt;BR /&gt;streaming_query = (spark.readStream&lt;BR /&gt;.format("cloudFiles")&lt;BR /&gt;.option("cloudFiles.format", "parquet")&lt;BR /&gt;.option("cloudFiles.schemaLocation", "/path/to/schema/location")&lt;BR /&gt;.load("/path/to/source/table")&lt;BR /&gt;.createOrReplaceTempView("streaming_data"))&lt;/P&gt;
&lt;P&gt;# Apply the sampleBy function to the new data and write it to the new delta table&lt;BR /&gt;spark.sql("""&lt;BR /&gt;SELECT * FROM streaming_data TABLESAMPLE(sampleBy('strata_column', fractions={"strata_value1": 0.1, "strata_value2": 0.2}))&lt;BR /&gt;""").writeStream&lt;BR /&gt;.format("delta")&lt;BR /&gt;.option("checkpointLocation", "/path/to/checkpoint/location")&lt;BR /&gt;.start("/path/to/sample/table")&lt;BR /&gt;```&lt;BR /&gt;In this example, replace `/path/to/source/table` with the path to your source delta table, `/path/to/sample/table` with the path to the new delta table for the stratified sample, and `/path/to/schema/location` with the path to the schema location for Auto Loader. You can also adjust the `fractions` parameter in the `sampleBy` function to specify the desired sampling fractions for each strata value.&lt;/P&gt;
&lt;P&gt;Note that this approach assumes that the new data added to the source table has the same schema as the existing data. If the schema changes, you may need to update the schema location for Auto Loader and/or modify the streaming query to handle the schema changes.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 06:31:30 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sampleby-stream-in-dlt/m-p/101165#M40569</guid>
      <dc:creator>Sidhant07</dc:creator>
      <dc:date>2024-12-06T06:31:30Z</dc:date>
    </item>
  </channel>
</rss>

