Middle
How is the data processing in a pipeline organized?
sobes.tech AI
Answer from AI
The data processing pipeline process, typical for streams in Node.js, is based on passing data from one processing stage to another in small chunks.
Main stages:
- Source: Reads data from a source (file1, socket2, HTTP request3).
- Transformations: Sequentially applies processing operations to the data. Each stage receives data from the previous one, processes it, and passes it on.
- Destination: Writes the processed data to the final node.
Example of using pipeline in Node.js:
const { createReadStream, createWriteStream } = require('node:fs');
const { Transform, pipeline } = require('node:stream');
const sourceStream = createReadStream('input.txt');
const destinationStream = createWriteStream('output.txt');
// Transformation: add a prefix to each line
const transformStream = new Transform({
transform(chunk, encoding, callback) {
this.push(`Prefix: ${chunk}`);
callback();
}
});
pipeline(
sourceStream, // Source
transformStream, // Transformation
destinationStream, // Destination
(err) => { // Callback on completion
if (err) {
console.error('Pipeline failed.', err);
} else {
console.log('Pipeline succeeded.');
}
}
);
Advantages of pipeline:
- Memory efficiency: Processes data in chunks without loading everything into memory at once.
- Composition: Easily combines multiple processing stages.
- Error handling: Built-in error handling mechanism that propagates through all streams.
- Logging: Allows attaching event handlers for process monitoring.