Transcript

Use with ChatGPT / Claude Auto-scroll A

1. Autoloader and Incremental Loading Basics

Introduces Autoloader in Databricks and explains why incremental data loading is important. The speaker defines the ideal characteristics of incremental pipelines and motivates the need for a better solution.

0:05 - 1:02
Hello friends welcome to rajas data Engineering in this video I'm going to talk about autoloader in data breakes autoloader is one of the advanced future provided by data breaks even though this is Advanced concept this is mostly usedin most of the data engineering data braks related projects so I'm going to give idea of what is autoloader when to use that how to use that autoloader is mainly used for incremental data loading so in order tounderstand autoloader first we have to understand what is incremental data load so let me start from the basics of incremental data load so incremental data load is one of the most commonly used data loading pattern in data engineeringETL pipelines so as per this concept we have to process only the adding new or modified data to the existing data set without reloading the entire data set which which means whenever we are processing whenever we are ingesting
data from multiple Source systems we have to process only the modified or newly added data we should not process the entire data so this uh incremental data loading pattern this is one of the complex and most challenging inmost of the data engineering projects so there are multiple approaches in order to handle this incremental data load but each and every uh pattern is having certain problem so coming to autoloader this is going to resolve those problemsso that is what we are going to discuss in this video so you understood what is incremental data load let me explain what is the ideal incremental data load or what are the characteristics of incremental data load soas for the uh thump rule of incremental data loading we have to process only the newly added or modified data files we should not process the entire data or we should not process non-modified data right that is one
of the important quality of this incremental data load and the next one we have to process the data as soon as it has been deducted or when we receive new file immediately it should be processed that is thesecond characteristics of incremental data load and coming to coming to the third one you you should not miss any new files we should not pro process uh the previously processed to file that is what we have seen inthe first point same at the same time we should not miss any new files that is also one of the important characteristics and it should provide good performance even though we are having multiple uh different patterns to handleincremental data load this is one of the place where we are having a challenges uh we we have to design a incremental data load Pipeline with good performance even though we are going to have huge number of files
under large number of directories still there should not be any performance bottleneck and the last quality is it should be repeatable pattern which means we should be able to schedule then it should be able to process uh thesame thing uh in a repeated uh manner so these are the ideal characteristics of incremental data load even though we are having many uh incremental data pipelines in the world now you know we are having millions or billionsof pipelines running across the world to handle incremental data load but each and every pipeline they are suffering from some of the issue you they are getting deviated from these ideal characteristics so in order to avoid that weare going to introduce autoloader so that we will come to know later now before getting into autoloader again I want to highlight few common patterns which is used to handle incremental data loading right so coming to the first

2. Traditional Incremental Loading Approaches

Covers common methods like watermarking and checkpointing, along with their drawbacks. The section ends by positioning Autoloader as the more efficient alternative and outlining its supported storage systems and file formats.

button this is one of the traditional and most popular uh method which is Watermark method so as for this method what we are going to do is we will um when we in data from The Source system weare going to capture the maximum time stamp or one of the incremental ID column what is the maximum ID we are going to capture that value and we are going to store it in one of the control tablewhich is called Watermark table then as per our um ETL pipeline logic when uh the pipeline is getting triggered next time it will first go to the control table and it will get the value of Maximum time stampor maximum ID then it will go back to the source system then it will compare whatever the new records either modified or added uh greater than this particular Watermark time stamp or ID it is going to pull only
those value so this is the watermark method and this is very common and traditional approach and it is implemented in most of the uh traditional relational databases and data warehousing but we have certain problem with this method oncewe have created this uh solution then we will uh create triggers based on particular interval which is a schedule based or we can go with even based trigger right but coming to the downside of this approach even thoughthere is no changes to the source system there is no modified or new data but still at the scheduled interval the program will start executing it will go back to the source system and it will just compare withthe watermark time stamp and it will check if there is any new or modified record when it it come it comes to know that there is no changes in the source system then it won't execute anything then the
process will be terminated so the conclusion is even though there is no changes but still our program is getting executed that is unnecessary because each each execution that is associated with particular cost uh because of computing platform infrastructureuh you know for these reasons you know it is uh uh it is costing something so even though there is no necessity to execute but still uh it is getting executed at the regular interval so this is oneof the the downside of this particular pattern right so let us go to the next pattern which is checkpointing this is most commonly used in spark structured streaming so as per spark structured streaming it is uh not goingto check the source system when there is no change okay that issue is getting resolved but um the thing is it is going to collect uh the offset value for the processed data and it is going to store
those offset value under the checkpoint location with the help of this checkpoint location spark structure structured streaming can compare the source and Target easily and it can process only the newly modified or uh added data fine but theproblem with this pattern is whenever we are having huge number of files uh millions or billions of files across thousands or millions of folders then it is going to recognize uh performance bottle neck it is not going touh it's not going to be efficient in order to process handle huge amount of data files so that that is a problem with this pattern checkpointing location and apart from that we can go with our own approaches likewe can use assure logic apps aure functions we can go with our own approaches also different you know patterns we can design but each and every pattern it is coming up with some shortcoming there is some problem with
each and every pattern so I'm not going to dive into different patterns here I have covered just two patterns which are very commonly used in today's data engineering projects right now we have seen now there are multiple patternsto handle the incremental data load but each and every pattern it is coming up with its own disadvantages so what is the best solution so best solution is we can go with autoloader so autoloader that is uh givingthe feature of processing the incremental data continuously efficiently automatically as soon as new files arrive so this is the concept of autoloader so at high level you understood what is autoloader why we are using using autoloader is oneof the future provided by data breaks in order to handle incremental data load right now let us try to understand more about autoloader so autoloader we we understood the purpose it is um uh used to process incremental data
efficiently and automatically and there are certain uh other limitations like this is currently uh supporting only cloud storage systems like assure data Lake storage in assure or Amazon P S3 pcket or Google Cloud Storage dbfs now these arethe different cloud storage system we are having in the market currently it is supporting only cloud storage system and um coming to file format autoloader is supporting only certain list of file formats those are Json CSV Park AOorc text and binary file these are the file formats currently supported by autoloader right we understood what is autoloader and U you know for which sour systems for which file formats we can go with autoloader right now let

3. Autoloader Architecture and File Detection Modes

Explains Autoloader’s internal components, including metadata storage, Spark Structured Streaming, and cloud-native file discovery. It then compares directory listing and file notification modes, using a folder-hierarchy example to show why Autoloader scales better.

us try to understand the internal components of autoloader basically autoloader is having three major internal components the first one is internal database so whenever autoloader is processing ingesting data from different Source systems it is uh storing the metadataof processed files within internal database which is rocks TB this is one of the major component and second component is in order to process the incremental data it is using spark structured streaming concept so coming to Smart structuredstreaming it is processing the incremental data but it is having certain problem with a file listing whenever it has to handle huge amount of data then it is having certain performance botel neck that is actually eliminated with thehelp of this first component and three third component but uh coming to processing processing engine it's quite similar to Smart structured streaming and the third component is autoloader is using Advanced and optimized Cloud native components in order to
identify new files as soon as they arrive so with the help of this processed data and also newly available data know with with the help of these components autoloader is uh performing better than spark structured streaming right theseare the components of autoloader we understood now coming to processing you know when autoloader is processing the data this is actually having two parts one is cloud files data reader this is nothing but identifying the new files andhow it is reading from The Source system and the second one is cloud notification services this is optional coming to Cloud notification Services now there are two internal components one is there are two internal options one is uhdirectory listing that is a default one if you don't specify any option then it is going to act as a de directory listing but apart from that one more option which is file notification method even we can choose
that explicitly but in order to choose that we have to Grant certain access that I will cover in the next slide so coming to processing in autoloader it is having two major parts right coming to file detection modeas I mentioned in the previous slide you know we are having two different file detection mode one is directory listing mode another one is file notification mode coming to directory listing mode that is a default one we don'tneed to specify anything explicitly but coming to file notification mode for that we have to specify explicitly and coming to directory listing mode that is more suitable in order to process the data in batch mode which means knowwe are getting list of files you know for every 1 hour or every six hours every one day every week something like that we can go with directory listing mode but if you're are getting real-time data in a
huge number of files uh per uh second or per minute then it's better to go with file notification mode right I will dive into details for these two modes so that you can get better understanding first we canunderstand directory listing mode so coming to directory listing mode as I mentioned this is the default mode we don't need to specify that um explicitly in our programming and coming to permission setup know there is no specific permissionneeded in order to use directory listing mode except access to storage account access to storage account this is not only for this autoloader even we have to uh create data we have to read uh certain file from anystorage then we need uh we need uh access to that particular storage account it is coming in the form of Mount point or we can directly access with certain key so even in order to create Mount point we
are having different approaches like we can use a service principle or access key or assure active directory now there are various approach so with uh with the help of those approaches we need to Grant access to the storageaccount that is the only thing apart from that we don't need to uh configure any specific permission right and uh this directory listing that is more optimized and efficient directory listing program compared to spark option even spark structuredstreaming that is also uh comparing the list of files under directory and it is processing but this is completely different from spark structure streaming so I'm going to give some example then you will understand the power of autoloadercompared to spark structured uh spark structured structured streaming right in order to give that example in order to get better understanding you can uh know you can try to understand this example let's say you are processing data from
multiple Source systems and you Areo data Lake storage that is divided as per this hierarchy uh at top level we are having Source systems and within that we are having year within that we are having month within thatwe are having Day within that we are having hour so in real time we might have multiple Source systems but in this example I'm just considering only one source system and One S system let us assume we arehaving multiple years 2021 2022 2023 let us assume only one year I'm having currently 2022 and 2022 that is having multiple uh monthly folders internally so there will be 12 folders per year folder and each month that isdivided into multiple subfolders for each day for example for the month of Jan I will have 31 subfolders for the month of February I will have 28 or 29 subfolders fers coming to March again it will have 31
subfolders so overall we are going to have 365 unique folders under this layer under this level and coming to each day again it is sub subdivided into 24 subfolders one for each hour so when we go to thisparticular level the number of folders would be above we are having 365 and for each folder we are having 24 subfolders which means 365 * by 24 we are going to have 8,7 61 folders per one year justfor Simplicity I'm going to understand I'm going to assume I'm having 10 files under each folder so we are having 8,761 folder 10 files per F per folder which means overall I'm having
87,134 this should be multiplied by two more and in case I'm going to add um multiple Source systems then we even we have to multiply uh those many number of times then we can understand how many number offiles we are going to have and just in this example I have considered only 10 files on an average but in real time we can have thousand or millions of files under each folder then we can understand thecomplexity of this uh storage The Source system right now coming to this uh structure in order to process in order to process this uh files know from the root fold from the you know the lowest folders you knowto process these files let us assume we are going to use Spar structure streaming in uh one use case and we are going to use autoloader in another use case then we are going to compare so as per
spark structured streaming and it is having the concept of one AP call per folder in order to process uh the newly or newly added or modified files so it will uh it will send one AP call per folderso in this case we are having 8,761 folders which means in order to process uh 8,761 folders it will uh send these many number of API calls and in order to process
87,134 API calls but coming to autol loer this is following different powerful algorithm as per this one it is going to consider the entire folder uh in flatten structure so it is basically flattening the entire structure and asper ADLs future it is supporting 5,000 files for single APA call which means in order to process
87,134
that is a limit and in order to process
87,134 AP hits then we can easily compare here just 18 that is uh compared with 8,761 now we can understand the power of autoloader how powerful it is now how quickly it can finish the same data injection comparedto SP structured streaming I hope you understood the power of autoloader in directory listing mode right now let me uh go to the next um uh file detection mode which is file notification mode so uh coming to filenotification mode now this is uh this is mostly suitable whenever we are processing millions of files under huge number of directories and also the data is coming in real time you know for each and every minute or everysecond we are receiving uh many files so for those scenarios file file notification mode is more powerful than directory listing mode for file notification mode we have to Grant set access like for assure data L Storage account we
have to Grant contributor access same for storage que and also for uh even subscription okay uh we need to Grant the contributor access for these Services once it is done then autoloader will automatically set up the notification anduh Q services for us then it will start using that we don't need to manually set up anything but in the program you know in order to uh Grant access to Q storage we have to create a serviceprinciple and we have to give certain configuration details through our code so I will explain in the demo but before that we have to understand you know what are the different um parameters we need to pass for filedetection file notification mode so we have to keep uh these information Sandy subscription subscription ID Resource Group tenant ID client ID and client secret client ID and client secret that is nothing but service principal credentials right I hope
you understood uh file notification mode as well now finally I can summarize uh the architecture of autoloader so for whatever we have discussed I'm going to summarize in this diagram so we are having aure data Lake storage sowe have to process the data into one of the metallion architecture in our Delta Lake uh so for that we can go with the spark structured streaming but that is not powerful that's the reason we want to gowith autoloader so coming to autoloader there are two different options either we can use uh even grit and Azure CU this is nothing but file notification mode so for that these services will be created by autoloader itself automaticallybut we have to configure certain things and we need to Grant certain access that's it so for this method uh autoloader will consume the metadata from the ADLs and it will just store the processed metadata within the rock
TB and with the help of this rock TB and the source system smart structured streaming will be able to process the data efficiently into our Medallion architecture this is one approach the second approach is this is the defaultone option two in this option we don't need to explicitly mention uh the type by default it will be using directory listing so in the directory listing basically it is going to list down all the you know filesunder the different folders and it is going to maintain the processed data within the rock TB and with the help of this rock TB and directory listing spark structured streaming will be able to um pull the unprocessed ormodified newly added data efficiently from The Source system and that can be processed and pushed into metalian architecture within the Delta Lake this is the summary architecture of autoloader I hope you understood now let me get into demo

4. Databricks Demo and Configuration Options

Walks through practical Databricks usage, including storage access setup, basic Autoloader syntax, schema handling, and notification-based configuration. It finishes with a tour of important options, wildcard paths, and writing the ingested data to the target medallion layer.

in data breaks I have logged into my data breaks environment so in this notebook I'm going to explain different features of autoloader how to use that practically so as I mentioned uh previously the first step is we haveto Grant access to the storage account to autoloader so for that we can use spark configuration settings so here what I'm doing is I'm retrieving my assur storage Account Details along with access key from the keyal even wecan hardcode that directly but it's recommended to use uh keyal because uh keeping the sensitive information directly in the notebook is not recommended I have already posted couple of videos in my Channel about assure keyal integration ation incase you don't understand how to integrate assure keyword with datab bricks you can watch those videos right so I I am retrieving the values then using Spar configuration uh command just I'm uh uh configure my assure data L
Storage Account Details once this is done then autoloader will be able to injust data from The Source system right this is one of the preer site once this is done in the next step I'm going to show thebasic configuration now the basic way of using autoloader so for that I'm going to use the syntax Spock do readstream then within the format usually we used to give the file format like CSV Json but when you havedecided to use autoloader then we have to use uh the format which is cloud files whenever we are using cloud files it means it is going to use autoloader internally right this is how we can invoke autoloader andone of the one of the mandatory parameter is option is we have to specify which file format we are going to process in our ASU data Lake storage we might have CSV file we might have Json file or
Park file so what kind of file format we have to process in this inje so for that we have to specify option within this option we have to tell cloud files start format that is gson in case youhave to process a CSV file in your project you can give CSV right this is one of the very simple example how to use autoloader and apart from that we are having so many futures so many options availablein autoloader processing that I'm going to explain in the subsequent steps so the next step I'm going to tell schema as well so this is similar to any data frame reader so in while reading data frame while creatingdata frame we used to specify this option we used to define the schema explicitly with the help of struct type and struct field then we used to mention that that is recommended for good performance in case we are
going with infer schema then un neily data breaks engine will scan the data files once in order to derive the schema but that is very costly operation in regular uh spark processing but coming to autoloader even that isnot very costly because autoloader efficiently samples the data to infer the schema so even we can go with infer schema option for that we have to go with option cloud files infer schema type True by default it isfalse but if you want to go with infer scheme option then we can give this option and along with this option we should give schema location as well once we have created this infer schema where we have tostore that information for that we can give location of our choice it could be ADLs or dbfs right these are the different options we can exp explicitly Define the schema and use or we can ask the autoloaded engine
to infer the schema right apart from that we can specify schema hints as well so in this example whenever I'm telling autoloader to infer the column type let's say I'm having one column employee ID which is having allthe numerical values but uh data bricks autoloader might infer that as integer let us assume that column should be big integer instead of regular integer then what I can do is only for those particular columns I can givehints as well so here I'm giving schema hints then employee ID column it should be big integer so even though you are inferring as a integer don't use integer it should be big integer so this is one ofthe other option so so similarly you know here I have covered few options you know with the help of this option command we can specify so there are so many different options available we can use any option as
per our use case our requirement this is um so far I have talked about directory listing mode only because explicitly I didn't mention that notification service now in the next example I'm going to show you how to usethis notification so for that file notification mode we have to use the syntax we have to give option Cloud files.use notification Pro Pro by default this is false so it will use directory listing mode so whenever we have to use file list file uh notification mode then we have to use this syntax and as I mentioned earlierwhenever we have to use um um file notification mode we have to Grant access in assure portal um for assure data L Storage account assure q and uh even git once that is done and also we have createduh service principle then even we can pass these parameters along with autol loader so for that in my next example what I have done is I'm creating one variable here cloud files conf we can give any variable in
this example I have given this variable within that I'm defining all the parameters along with its value with the help of key value PA it's a kind of python dictionary it is having key value par so here oneside I'm having all the parameters and other side I'm keeping all the values so here I have you know given all the parameters then while reading the data what I can do is just I can pause all thesevalues in one go here I have defined the options in the above variable now I am using that variable so this variable is key value pair that's the reason we need to give star star in case it's arraywe can give only single star but it's a key value pair that's the reason we need to give star star and uh earlier I have given only option so far but here it's a multiple option that's the reason
here we have to give options not only option we have to give yes also right this is how we can use file notification mode as well I hope you understood and apart from that here I have summarized differentum options autoloader options I'm not going to dive into details but quickly I can walk through first one is file format okay which type of file we have to use for our injection that we can specify and thenext one is allow override in case there is some changes to to the existing record let's say in our existing value for one of the Val one of the employee the salary is 5,000 but in the source it's6,000 there is some change then we have to overwrite the existing record card so this is a default property but in case you don't want you can change as well and coming to schema location in case we are
going with infer schema then uh where we have to store that schema information so for that we can Define schema location the next property is schema evaluation mode and today's Big Data World schema evaluation that is one ofthe important concept autoloader is efficiently handling schema evaluation so basically with this property we can uh play around this schema evaluation B basically it is giving different uh far different modes schema evaluation modes to handle schema evaluation soI have given different schema evaluation mode as well below by default it is going to use add new columns so let's say in our Target table we are having five columns in the source we are getting six columnso one new column we are getting then what happens our streaming process streaming pipeline will fail then the new column will be added after that the streaming uh process will be restarted this is the default mode and coming
to rescue in the rescue mode it is not going to U it is not going to add the new column to the Target but instead of that it is going to create one column which is called underscore rescuedata within that it is going to document what are the new columns we have received and at the same time it is not going to fail the uh streaming process that is rescue and the next one fail onnew columns let's say we are processing some sensitive data we are not expecting any new column to arrive but in case something is coming uh that is unexpected then it should fail so for that those scenarios we cango with fail on new columns and uh the streaming pipeline will be failed it will not restart automatically we have to fix the schema issue then only it will restart and coming to the last one none just totally
ignore whenever we are getting some uh new schema which means it is turning of schema evaluation future that's it these are the different options available for schema evaluation mode and uh coming to other um uh other uh optionsschema hint I have already covered in case for a particular column you want to specify particular data type then we can go with that and infer column column types in case you don't want to go with user definedschema then we can go with this one and include existing files which means whenever we are setting up this uh entire autoloader process for first time and you are pointing to some data folder but there are already filesare available so the previously available file we have to consider or not that property is handled with the help of this option and the next one is Max pite per trigger so whenever we are processing uh the incremental
data basically it is triggered maybe it could be once or it could be micro batch or it could be continuous but you know for each trigger how much data it can fetch at a time that property is definedwith the help of this option and the next one is maximum file age this is one of the important parameter in order to handle the duplicate files so let's say I'm going to uh set 1 hour is themaximum age for all the files So within 1 hour I'm going to get to two different data files with same name then it will be considered as duplicate but after 1 hour let's say last one hour I gotone file and uh after completing uh next hour again I'm getting the same file then it will be treated as a new file so in order to define the duplicate file this property is very important and um coming
to Resource tack this is kind of uh uh you know this is helping to ident if y the right resources this is not very commonly used and uh this next one is very important property validate option so basicallywe have defined different options in the above step so we have to validate those options or not for example in your code for one of the options you might have given wrong value if you have given wrong valuethat will be ignored and it will be Pro you know it will be proceeded but uh you have given certain option it's very important option it should have received the right value that value should be considered so youwant to validate then you can give this option which means whatever the options we have specified in our autoload injection everything will be validated in case certain value is incorrect then it is going to report so in that
aspect this is one of the important parameter and um we are having back fill interval you know for what duration we have to consider the back filling that is defined with the help of this parameter and the lastone is partition column this is actually using Hive style partition so whenever we are ingesting the data from The Source system in case the source system is partitioned based on the folder now usually this is following this stylecountry equal to uh UK or city equal to certain city so basically you know the hi style partition in case uh the source system is following High St partition then that can be uh used for autoloader injection sothese are the different uh commonly used uh options in autoloader I hope you understood and and apart from that while processing data through autoloader we can consider wild card options also Wild Card option that is nothing new to
you because this is very common in most of the programming languages uh question mark which means ma matching any single character star which means matches zero or more number of characters so there are various options I have givenso you can have a quick look on that this is uh nothing new this is similar to any programming uh languages this wild card option this is uh commonly available with most of the programming languages right so howto use that so while um reading the data using autoloader first we have to give a format cloud files then we can Define different options then we can Define scheme as well then here we can use the WildCard path so for example under root folder I want to consider all the subfolders that's the reason star star means any so we can use any wild card option while reading the data from The Source system and finally
once we have ined the data from The Source system we have to write the data into our Target system in The Medallion architecture we have to write the data within bronze silver or cold layer so for that wecan use right stream uh command so DF do right stream then trigger there are multiple options for trigger either we can use once or we can go for for each batch now there are different options we can useand finally we have to use Save and our Target location in The Medallion architecture this is how uh we can use autoloader in our real-time projects I hope you understood the concepts of autoloader how it is working internallyhow to use in our project practically I hope you understood hope you enjoyed this video if you like the content of this video please like and comment in the Channel Please Subscribe this Channel and don't forget to click
on the Bell button to get latest videos on data breaks thank
you
Seek backward 5s ←
Longer seek (10s) Shift ←
Play Space Seek forward 5s →
Longer seek (10s) Shift →
0:00 Auto-scroll A Copy link at current time S

121. Databricks | Pyspark| AutoLoader: Incremental Data Load