63
loading...
This website collects cookies to deliver better user experience
mkdir node-etl
npm init -y
to be able to install node packages.cd node-etl
npm init -y
node-fetch
.npm install node-fetch
extract.js
file, for handling querying the data.touch extract.js
const fetch = require('node-fetch');
const URL = 'https://jsonplaceholder.typicode.com/albums';
async function getPhotos(albumId) {
return await fetch(`${URL}/${albumId}/photos`).then(response =>
response.json(),
);
}
module.exports = { getPhotos };
getPhotos()
async function will take an albumId
as an argument, will make a GET
request to the fake API and return an array where each object has the following interface:interface Photo {
albumId: number;
id: number;
title: string;
url: string;
thumbnailUrl: string;
}
getPhotos
so we can import it in the index.js
, which will orchestrate the ETL pipeline. Have a look at the How to organize Node article for maintainable Node.js code.index.js
file, which will be the entry point of the application and used to orchestrate the ETL pipeline.touch index.js
orchestrateEtlPipeline()
function, which will coordinate each step in the ETL pipeline: extracting data, transforming it and loading it into its destination.const { getPhotos } = require('./extract');
const orchestrateEtlPipeline = async () => {
try {
const photosAlbum = await getPhotos(1);
console.log(photosAlbum);
// TODO - TRANSFORM
// TODO - LOAD
} catch (error) {
console.error(error);
}
};
orchestrateEtlPipeline();
orchestrateEtlPipeline()
function uses async/await
again in a try/catch
block to handle any errors or promise rejections.Promise.all
we can wait for all extracted data before proceeding. However, this method fires all requests at once, which can overwhelm some sources (think of multiple intensive requests to the DB). Read more about Multiple Promises in Node.js.const { getPhotos } = require('./extract');
const orchestrateEtlPipeline = async () => {
try {
const allPhotos = Promise.all([
getPhotos(1),
getPhotos(2),
getPhotos(3),
]);
const [photos1, photos2, photos3] = await allPhotos;
console.log(photos1[0], photos2[0], photos3[0]); // to log the first photo object of all three albums
// TODO - TRANSFORM
// TODO - LOAD
} catch (error) {
console.error(error);
}
};
orchestrateEtlPipeline();