This is a script made with a collation of internet examples.
The goal here is to load a DynamoDB table with records from a SQLServer table.
For this example I'm using a local installation of DynamoDB.
// SQL Server configuration
// DynamoDB configuration
// Connect to SQLServer
// For each row create the result array[]{year,title}
// When all rows are read, save them to DynamoDB
// Start processing
The goal here is to load a DynamoDB table with records from a SQLServer table.
For this example I'm using a local installation of DynamoDB.
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var fs = require('fs');
var AWS = require("aws-sdk");
console.time("dbsave");
var config = {
userName: 'username',
password: 'password',
server: 'serverIP',
options: {
//instanceName: 'MSSQLSERVER', -- didnt have to use this option
port: 1433,
database: 'dbname'
}
}
// DynamoDB configuration
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
docClient = new AWS.DynamoDB.DocumentClient();
connection = new Connection(config);
connection.on('connect', function (err) {
if (err) return console.error(err); // <-
console.log("Connected");
executeStatement();
});
function executeStatement() {
var options = { keepNulls: true };
var result = [];
// Read rows from SQLServer table
request = new Request("select * from Movies", function (err, rowCount, rows) {
if (err) return console.error(err)
else console.log("request rowcount: " + rowCount); // <-
//console.log(rows);
});
request.on("row", function (columns) {
var item = {};
columns.forEach(function (column) {
item[column.metadata.colName] = column.value;
});
result.push(item);
});
request.on('requestCompleted', function (rowCount, more) {
console.log("Importing movies into DynamoDB. Please wait.");
result.forEach(function (movie) {
var params = {
TableName: "Movies",
Item: {
"year": movie.year,
"title": movie.title
}
};
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add movie", movie.title);
// } else {
// console.log("PutItem succeeded:", movie.title);
}
});
});
console.log('PutItem Ended');
end();
});
// Start processing
connection.execSql(request);
};
end = function(err, saved) {
//var i;
//console.log(( err || !saved )?"Error":"Saved");
//if(--i === 1){
console.timeEnd("dbsave");
//}
};
Comments
Post a Comment