Handling large CSV files in your Xano function stacks can be a memory-intensive task, leading to potential failures or performance issues. Thankfully, Xano has introduced a new feature called CSV Stream, which allows you to process CSV files in chunks, improving memory efficiency and enabling seamless handling of large datasets. In this guide, we'll walk you through the process of using CSV Stream in your function stacks.
Before we dive into CSV Stream, let's revisit the traditional approach to processing CSVs in Xano function stacks:
getFileResourceData step to retrieve the raw data from the file resource.getFileResourceData step includes metadata, create a new variable to isolate the raw file data.While this approach works, it has a significant drawback: you need to hold the entire CSV data in memory multiple times, leading to high memory usage. As the size of the CSV file increases, the function stack becomes more prone to failures, particularly on lower-tier plans or instances with high concurrent usage.
CSV Stream is designed to solve the memory inefficiencies associated with processing large CSV files. Here's how you can implement it in your function stacks:
CSVStream function. Provide a variable name and the file resource value as input.forEach loop, providing rows of your CSV in chunks for efficient processing.forEach loop, you can add records to your table or perform any other desired operations on the CSV data.CSV Stream is specifically designed to work with the forEach loop, similar to the stream return type introduced for the Query All Records functionality. This approach ensures that you don't need to hold the entire CSV data in memory at once, significantly reducing memory usage and enabling efficient processing of large datasets.
To illustrate the efficiency gains of CSV Stream, let's compare the two approaches using a CSV file with over 200,000 records on a Starter Plan instance.
While using CSV Stream, you won't be able to view the entire contents of the CSV in the function stack, as the goal is to reduce memory usage. However, you can inspect individual items within the forEach loop using a stop and debug statement.
After running the CSV Stream function stack, you can navigate to your database and verify that all records (216,930 in this example) have been successfully added to the table.
CSV Stream is a game-changer for processing large CSV files on Xano. By enabling chunk-based processing, it significantly reduces memory usage and allows you to handle datasets of any size efficiently, even on lower-tier plans or instances with high concurrent usage. Implementing CSV Stream in your function stacks is straightforward and can be achieved with just a few steps. Give it a try and experience the power of efficient CSV processing on Xano!
If you have any questions or need further assistance, feel free to leave a comment below, reach out to Xano Support, or engage with the Xano community.
Join 100,000+ people already building with Xano.
Start today and scale to millions.
Start building for free