forked from robhogan/dynamodb-geo.js
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
303 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,135 @@ | ||
const ddbGeo = require('dynamodb-geo'); | ||
const AWS = require('aws-sdk'); | ||
const uuid = require('uuid'); | ||
const { | ||
GeoDataManager, | ||
GeoDataManagerConfiguration, | ||
GeoTableUtil, | ||
} = require("dynamodb-geo-v3"); | ||
const { | ||
DynamoDB, | ||
DynamoDBClient, | ||
Endpoint, | ||
waitUntilTableExists, | ||
} = require("@aws-sdk/client-dynamodb"); | ||
const uuid = require("uuid"); | ||
|
||
// Set up AWS | ||
AWS.config.update({ | ||
// Use a local DB for the example. | ||
const ddb = new DynamoDB({ | ||
credentials: { | ||
accessKeyId: YOUR_AWS_KEY_ID, | ||
secretAccessKey: YOUR_AWS_SECRET_ACCESS_KEY, | ||
region: YOUR_AWS_REGION | ||
}, | ||
endpoint: new Endpoint("http://localhost:8000"), | ||
region: YOUR_AWS_REGION, | ||
}); | ||
const ddbClient = new DynamoDBClient({ | ||
credentials: { | ||
accessKeyId: YOUR_AWS_KEY_ID, | ||
secretAccessKey: YOUR_AWS_SECRET_ACCESS_KEY, | ||
}, | ||
endpoint: new Endpoint("http://localhost:8000"), | ||
region: YOUR_AWS_REGION, | ||
}); | ||
|
||
// Use a local DB for the example. | ||
const ddb = new AWS.DynamoDB({ endpoint: new AWS.Endpoint('http://localhost:8000') }); | ||
|
||
// Configuration for a new instance of a GeoDataManager. Each GeoDataManager instance represents a table | ||
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'capitals'); | ||
const config = new GeoDataManagerConfiguration(ddb, "capitals"); | ||
|
||
// Instantiate the table manager | ||
const capitalsManager = new ddbGeo.GeoDataManager(config); | ||
const capitalsManager = new GeoDataManager(config); | ||
|
||
// Use GeoTableUtil to help construct a CreateTableInput. | ||
const createTableInput = ddbGeo.GeoTableUtil.getCreateTableRequest(config); | ||
const createTableInput = GeoTableUtil.getCreateTableRequest(config); | ||
|
||
// Tweak the schema as desired | ||
createTableInput.ProvisionedThroughput.ReadCapacityUnits = 2; | ||
|
||
console.log('Creating table with schema:'); | ||
console.log("Creating table with schema:"); | ||
console.dir(createTableInput, { depth: null }); | ||
|
||
// Create the table | ||
ddb.createTable(createTableInput).promise() | ||
// Wait for it to become ready | ||
.then(function () { return ddb.waitFor('tableExists', { TableName: config.tableName }).promise() }) | ||
// Load sample data in batches | ||
.then(function () { | ||
console.log('Loading sample data from capitals.json'); | ||
const data = require('./capitals.json'); | ||
const putPointInputs = data.map(function (capital) { | ||
return { | ||
RangeKeyValue: { S: uuid.v4() }, // Use this to ensure uniqueness of the hash/range pairs. | ||
GeoPoint: { | ||
latitude: capital.latitude, | ||
longitude: capital.longitude | ||
}, | ||
PutItemInput: { | ||
Item: { | ||
country: { S: capital.country }, | ||
capital: { S: capital.capital } | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const BATCH_SIZE = 25; | ||
const WAIT_BETWEEN_BATCHES_MS = 1000; | ||
var currentBatch = 1; | ||
ddb | ||
.createTable(createTableInput) | ||
// Wait for it to become ready | ||
.then(function () { | ||
return waitUntilTableExists( | ||
{ client: ddbClient, maxWaitTime: 30000 }, | ||
{ TableName: config.tableName } | ||
); | ||
}) | ||
// Load sample data in batches | ||
.then(function () { | ||
console.log("Loading sample data from capitals.json"); | ||
const data = require("./capitals.json"); | ||
const putPointInputs = data.map(function (capital) { | ||
return { | ||
RangeKeyValue: { S: uuid.v4() }, // Use this to ensure uniqueness of the hash/range pairs. | ||
GeoPoint: { | ||
latitude: capital.latitude, | ||
longitude: capital.longitude, | ||
}, | ||
PutItemInput: { | ||
Item: { | ||
country: { S: capital.country }, | ||
capital: { S: capital.capital }, | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
function resumeWriting() { | ||
if (putPointInputs.length === 0) { | ||
return Promise.resolve(); | ||
} | ||
const thisBatch = []; | ||
for (var i = 0, itemToAdd = null; i < BATCH_SIZE && (itemToAdd = putPointInputs.shift()); i++) { | ||
thisBatch.push(itemToAdd); | ||
} | ||
console.log('Writing batch ' + (currentBatch++) + '/' + Math.ceil(data.length / BATCH_SIZE)); | ||
return capitalsManager.batchWritePoints(thisBatch).promise() | ||
.then(function () { | ||
return new Promise(function (resolve) { | ||
setInterval(resolve,WAIT_BETWEEN_BATCHES_MS); | ||
}); | ||
}) | ||
.then(function () { | ||
return resumeWriting() | ||
}); | ||
} | ||
const BATCH_SIZE = 25; | ||
const WAIT_BETWEEN_BATCHES_MS = 1000; | ||
var currentBatch = 1; | ||
|
||
return resumeWriting().catch(function (error) { | ||
console.warn(error); | ||
}); | ||
}) | ||
// Perform a radius query | ||
.then(function () { | ||
console.log('Querying by radius, looking 100km from Cambridge, UK.'); | ||
return capitalsManager.queryRadius({ | ||
RadiusInMeter: 100000, | ||
CenterPoint: { | ||
latitude: 52.225730, | ||
longitude: 0.149593 | ||
} | ||
function resumeWriting() { | ||
if (putPointInputs.length === 0) { | ||
return Promise.resolve(); | ||
} | ||
const thisBatch = []; | ||
for ( | ||
var i = 0, itemToAdd = null; | ||
i < BATCH_SIZE && (itemToAdd = putPointInputs.shift()); | ||
i++ | ||
) { | ||
thisBatch.push(itemToAdd); | ||
} | ||
console.log( | ||
"Writing batch " + | ||
currentBatch++ + | ||
"/" + | ||
Math.ceil(data.length / BATCH_SIZE) | ||
); | ||
return capitalsManager | ||
.batchWritePoints(thisBatch) | ||
.then(function () { | ||
return new Promise(function (resolve) { | ||
setInterval(resolve, WAIT_BETWEEN_BATCHES_MS); | ||
}); | ||
}) | ||
}) | ||
// Print the results, an array of DynamoDB.AttributeMaps | ||
.then(console.log) | ||
// Clean up | ||
.then(function() { return ddb.deleteTable({ TableName: config.tableName }).promise() }) | ||
.catch(console.warn) | ||
.then(function () { | ||
process.exit(0); | ||
}); | ||
.then(function () { | ||
return resumeWriting(); | ||
}); | ||
} | ||
|
||
return resumeWriting().catch(function (error) { | ||
console.warn(error); | ||
}); | ||
}) | ||
// Perform a radius query | ||
.then(function () { | ||
console.log("Querying by radius, looking 100km from Cambridge, UK."); | ||
return capitalsManager.queryRadius({ | ||
RadiusInMeter: 100000, | ||
CenterPoint: { | ||
latitude: 52.22573, | ||
longitude: 0.149593, | ||
}, | ||
}); | ||
}) | ||
// Print the results, an array of DynamoDB.AttributeMaps | ||
.then(console.log) | ||
// Clean up | ||
.then(function () { | ||
return ddb.deleteTable({ TableName: config.tableName }); | ||
}) | ||
.catch(console.warn) | ||
.then(function () { | ||
process.exit(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "dynamodb-geo", | ||
"version": "0.4.0", | ||
"name": "dynamodb-geo-v3", | ||
"version": "1.0.0", | ||
"description": "A javascript port of awslabs/dynamodb-geo, for dynamodb geospatial querying", | ||
"scripts": { | ||
"prepublish": "tsc -d", | ||
|
@@ -10,8 +10,8 @@ | |
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": "rh389/dynamodb-geo.js", | ||
"author": "Rob Hogan <[email protected]>", | ||
"repository": "renet/dynamodb-geo-v3", | ||
"author": "René Schubert <[email protected]>", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"@types/long": ">=3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.