I have successfully set up DynamoDB local and everything is functioning as expected after going through their documentation. I have also tested their example code, which worked flawlessly.
The Users table has been created with the name "Users".
Below is the content of UsersCreateTable.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
<script>
AWS.config.update({
region: "us-west-2",
endpoint: 'http://localhost:8000/',
accessKeyId: "fakeMyKeyId",
secretAccessKey: "fakeSecretAccessKey"
});
var dynamodb = new AWS.DynamoDB();
function createUsers() {
var params = {
TableName : "Users",
KeySchema: [
{ AttributeName: "id", KeyType: "HASH"}
],
AttributeDefinitions: [
{ AttributeName: "id", AttributeType: "N" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
document.getElementById('textarea').innerHTML = "Unable to create users table: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
document.getElementById('textarea').innerHTML = "Created users table: " + "\n" + JSON.stringify(data, undefined, 2);
}
});
}
</script>
<title></title>
</head>
<body>
<input id="createTableButton" type="button" value="Create Table" onclick="createUsers();" />
<br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
</body>
</html>
I have already prepared sample JSON data for our Users Table. To load this JSON Users Data into DynamoDB local, I modified their MoviesLoadTable.html to UsersLoadTable.html file.
However, when attempting to load the JSON data, console errors are displayed:
Uncaught SyntaxError: Unexpected token : in JSON at position 497
at JSON.parse (<anonymous>)
at FileReader.r.onload (UsersLoadData.html:31)
r.onload @ UsersLoadData.html:31
FileReader (async)
processFile @ UsersLoadData.html:53
Content of UsersLoadData.html:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
<script type="text/javascript">
AWS.config.update({
region: "us-west-2",
endpoint: 'http://localhost:8000/',
accessKeyId: "fakeMyKeyId",
secretAccessKey: "fakeSecretAccessKey"
});
var docClient = new AWS.DynamoDB.DocumentClient();
function processFile(evt) {
document.getElementById('textarea').innerHTML = "";
document.getElementById('textarea').innerHTML += "Importing users into DynamoDB. Please wait..." + "\n";
var file = evt.target.files[0];
if (file) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
var allUsers = JSON.parse(contents);
allUsers.forEach(function (user) {
document.getElementById('textarea').innerHTML += "Processing user id: " + user.id + "\n";
var params = {
TableName: "Users",
Item: {
"id": user.id,
"info": user.info
}
};
docClient.put(params, function (err, data) {
if (err) {
document.getElementById('textarea').innerHTML += "Unable to add user: " + count + user.id + "\n";
document.getElementById('textarea').innerHTML += "Error JSON: " + JSON.stringify(err) + "\n";
} else {
document.getElementById('textarea').innerHTML += "Loading succeeded(id): " + user.id + "\n";
textarea.scrollTop = textarea.scrollHeight;
}
});
});
};
r.readAsText(file);
} else {
alert("Could not read users data file");
}
}
</script>
<title></title>
</head>
<body>
<input type="file" id="fileinput" accept='application/json' />
<br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
<script>
document.getElementById('fileinput').addEventListener('change', processFile, false);
</script>
</body>
</html>
I've tried troubleshooting the error without much success. Any help provided would be greatly appreciated. Thank you.