A stream is an abstract interface for working with streaming data in Node.js. Streams have gained the reputation that it is hard to work with and harder to understand. However, it is a highly underrated but very powerful concept in Node.js. This article will help in understanding of streams, how to work with them and where to use this module.
The official documentation of Node.js defines stream as an abstract interface for working with streaming data. It is one of the fundamental concept in Node.js and is very powerful when working with large amounts of data, e.g., reading a very large file size. Streams are memory efficient as there is no need to load large amount of data in memory instead read chunks of data piece by piece and process the contents. Also, since one doesn’t have to wait for all the data to load first, it is time efficient too.
For a complete introduction of Stream module and its related apis, read the official documentation.
Streams: A use case
For the purpose of showing the powerful capability of streams, let us do the following operations.
- Read data from a large file.
- Filter the data.
- Transform the input data.
- Write the transformed data into a file.
I have downloaded the Loan Data for Dummy Bank file for this article. I converted this file into json format with each line representing a json object. Let us read the data from the file, filter out all those who have ‘RENT’ as value for ‘home_ownership’ column. Add a new field ‘risk’ with value as low for loan_amount <= 5000 and high otherwise and write into a new file.
First lets create file read and write stream
Create a filter data stream and filter condition to filter out data having ‘home_ownership’ as RENT.
Similarly, create a transform data stream to add field ‘risk’ with value as ‘high’ for ‘loan_amount’ > 5000 else value as ‘low’.
I have also used ‘split’ module to create read chunks line by line. Using ‘pipeline’ module method of Nodejs pipe all the streams. This method pipes streams forwarding errors and properly cleaning up and provide a callback when the pipeline is complete. Also read about back pressure optimization in streams.
In this example, we can see that by using Nodejs streams, we can manipulate large sets of data very efficiently both in terms of memory and time. I reiterate, streams are a highly underrated but very powerful concept in Node.js. Hopefully, this article will help in understanding of streams, how to work with them and where to use this module.
Note:- Complete code is checked into github