We use Node.js to work with large files and raw data sets.
Recently, I was faced with a task that interested me - it was necessary to extract certain data from the huge volume of unstructured files of the US Federal Election Commission. I did not work too much with raw data, so I decided to take the challenge and take on this task. As a tool for solving it, I chose Node.js.
Skillbox recommends: The Frontend Developer Profession online course .
We remind you: for all readers of “Habr” - a discount of 10,000 rubles when registering for any Skillbox course using the “Habr” promo code.
The task was described in four points:
- The program should calculate the total number of lines in the file.
- Every eighth column contains a person’s name. You need to load this data and create an array with all the names contained in the file. It is necessary to display the 432th and 43,243rd name.
- Each fifth column contains the date of donation by volunteers. Count how many total donations are made each month, and print the total result.
- Every eighth column contains a person’s name. Create an array by selecting only the first name, without the last name. Find out which name is most often found and how many times?
(The original task can be viewed here at this link .) The
file you need to work with is a regular .txt with a capacity of 2.55 GB. There is also a folder that contains parts of the main file (you can debug the program on them without having to analyze the entire huge array).
Two possible solutions on Node.js
The familiar one is fs.readFile (). It allows you to read the entire file, putting it into memory, and then use Node.
An alternative is fs.createReadStream (), a function that passes data similar to how it is organized in other languages - for example, in Python or Java.
The solution I chose
Since I needed to calculate the total number of lines and parse data to parse names and dates, I decided to stop on the second option. Here I could use the rl.on ('line', ...) function to get the necessary data from the lines.
Node.js CreateReadStream () & ReadFile () Code
Below is the code that I wrote using Node.js and the fs.createReadStream () function.
Initially, I needed to set everything up, realizing that importing data requires Node.js functions such as fs (file system), readline and stream. Next, I was able to create instream and outstream along with readLine.createInterface (). The resulting code made it possible to parse the file line by line, taking the necessary data.
In addition, I added some variables and comments to work with specific data. These are lineCount, dupeNames and arrays of names, donation and firstNames.
In the rl.on ('close', ...) function, I performed all the transformations of the data added to the arrays with the information received in console.log.
lineCount and names are needed to determine the 432th and 43,243rd names, no conversions are required here. But the identification of the most common name in the array and the determination of the number of donations are more complicated tasks.
In order to identify the most common name, I had to create an object of value pairs for each name (key) and the number of references to Object.entries (). (value) and then convert it all into an array of arrays using the ES6 function. After that, the task of sorting names and identifying the most duplicate was no longer difficult.
With donations, I did roughly the same trick: I created an object of value pairs and a logDateElements () function, which allowed me, using ES6 interpolation, to display keys and values for each month. Then I created new Map (), converting the dateDonations object to a metamarray, and looped through each array using logDateElements (). (It didn’t turn out as easy as it seemed at the beginning.)
But it worked, I was able to read a relatively small file of 400 MB in size, highlighting the necessary information.
After that I tried fs.createReadStream () - I implemented the task on fs.readFile () in order to see the difference. Here is the code:
You can see the whole solution here .
Results of work with Node.js
It turned out that, although everything worked, but this solution tried to transfer the entire contents of the file to memory, which was impossible with a capacity of 2.55 GB. Node can work simultaneously with 1.5 GB in memory, no more.
Therefore, none of my decisions came up. It took a new one that could work even with such voluminous files.
As it turned out, it was necessary to use the popular NPM module EventStream.
Having studied the documentation, I was able to understand what needs to be done. Here is the third version of the program code.
The documentation for the module indicated that the data stream should be divided into separate elements using the \ n character at the end of each line of the txt file.
Basically, the only thing I had to change was the names response. I didn’t manage to put 130 million names into the array - again I ran out of memory error. I solved the problem by calculating the 432th and 43,243rd names and adding them to my own array. A little not what was asked in the conditions, but who said that you can not be creative?
Round 2. We try the program in work
Yes, all the same file with a volume of 2.55 GB, we cross our fingers and follow the result.
As it turned out, just Node.js is not suitable for solving such problems, its capabilities are somewhat limited. But expanding them using modules, you can work with such large files.