[英]How to write data to local DynamoDB?
According to DynamoDB docs, you always need to specify --endpoint-url http://localhost:8000
option if you want to interact with your local (downloaded) version of DynamoDB.根据 DynamoDB 文档,如果您想与本地(下载的)DynamoDB 版本进行交互,则始终需要指定
--endpoint-url http://localhost:8000
选项。 It works fine for me via aws cli
, but, following their tutorial for Node.js developers, I've faced a strange issue: I have been interacting with data in DynamoDB web version instead of my local one, even if configured connection options to database :它通过
aws cli
对我来说很好用,但是,按照他们为 Node.js 开发人员提供的教程,我遇到了一个奇怪的问题:我一直在与 DynamoDB Web 版本中的数据而不是本地版本中的数据进行交互,即使将连接选项配置为数据库:
const serviceConfigOptions: ServiceConfigurationOptions = {
region: "eu-north-1",
endpoint: "http://localhost:8000",
};
I described endpoint, but I have no results interacting with data in my local version and I have these in the web.我描述了端点,但我在本地版本中没有与数据交互的结果,我在网络上有这些。 All code is here:
所有代码都在这里:
import fs from "fs";
import AWS, { AWSError } from "aws-sdk";
import { config as dotenvConfig } from "dotenv";
import {
CreateTableInput,
DocumentClient,
PutItemInput,
PutItemOutput,
} from "aws-sdk/clients/dynamodb";
import { ServiceConfigurationOptions } from "aws-sdk/lib/service";
dotenvConfig();
const serviceConfigOptions: ServiceConfigurationOptions = {
region: "eu-north-1",
endpoint: "http://localhost:8000",
};
const dynamoDB: AWS.DynamoDB = new AWS.DynamoDB();
const params: CreateTableInput = {
TableName: "Movies",
KeySchema: [
{
AttributeName: "year",
KeyType: "HASH",
},
{
AttributeName: "title",
KeyType: "RANGE",
},
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" },
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10,
},
};
dynamoDB.createTable(params, (err, data): void => {
if (err) {
console.log("Unable to create table.");
console.error(err);
return;
}
console.log("Table created");
console.log(data);
});
const docClient: DocumentClient = new AWS.DynamoDB.DocumentClient();
console.log("Imporing data to DynamoDB");
fs.readFile(
"moviedata.json",
"utf-8",
(err: NodeJS.ErrnoException | null, data: string): void => {
if (err) {
console.log("Unable to read a file");
console.error(err);
return;
}
const allMovies: Array<any> = JSON.parse(data);
allMovies.forEach(({ year, title, info }): void => {
const params: PutItemInput = {
TableName: "Movies",
Item: {
year,
title,
info,
},
};
docClient.put(params, (err: AWSError, data: PutItemOutput) => {
if (err) {
console.error(
"Unable to add movie",
title,
". Error JSON:",
JSON.stringify(err, null, 2),
);
return;
}
console.log("PutItem succeeded:", title);
});
});
},
);
UPDATE: Refactored code into promises.更新:将代码重构为承诺。
import fs from "fs";
import { promisify } from "util";
import AWS, { AWSError } from "aws-sdk";
import { config as dotenvConfig } from "dotenv";
import {
CreateTableInput,
DocumentClient,
PutItemInput,
PutItemOutput,
} from "aws-sdk/clients/dynamodb";
import { ServiceConfigurationOptions } from "aws-sdk/lib/service";
dotenvConfig();
const serviceConfigOptions: ServiceConfigurationOptions = {
region: "eu-north-1",
endpoint: "http://localhost:8000",
};
const dynamoDB: AWS.DynamoDB = new AWS.DynamoDB();
const params: CreateTableInput = {
TableName: "Movies",
KeySchema: [
{
AttributeName: "year",
KeyType: "HASH",
},
{
AttributeName: "title",
KeyType: "RANGE",
},
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" },
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10,
},
};
const createDBTable: (
params: AWS.DynamoDB.CreateTableInput,
) => Promise<AWS.DynamoDB.CreateTableOutput> = promisify(
dynamoDB.createTable,
).bind(dynamoDB);
const readFile = promisify(fs.readFile);
createDBTable(params)
.then(data => {
console.log("Table created");
console.log(data);
})
.catch(err => {
console.log("Unable to create table.");
throw new Error(err);
})
.then(() => {
console.log("Imporing data to DynamoDB");
return readFile("moviedata.json", "utf-8");
})
.then((data: string) => {
const allMovies: Array<any> = JSON.parse(data);
const docClient: DocumentClient = new AWS.DynamoDB.DocumentClient();
allMovies.forEach(({ year, title, info }): void => {
const params: PutItemInput = {
TableName: "Movies",
Item: {
year,
title,
info,
},
};
docClient.put(params, (err: AWSError, data: PutItemOutput) => {
if (err) {
console.error(
"Unable to add movie",
title,
". Error JSON:",
JSON.stringify(err, null, 2),
);
return;
}
console.log("PutItem succeeded:", title);
});
});
})
.catch(err => {
console.error(err);
});
Now I'm getting Unable to create table. Error: ResourceInUseException: Table already exists: Movies
现在我
Unable to create table. Error: ResourceInUseException: Table already exists: Movies
Unable to create table. Error: ResourceInUseException: Table already exists: Movies
. Unable to create table. Error: ResourceInUseException: Table already exists: Movies
。 However, in my local db only one Music
table.但是,在我的本地数据库中只有一张
Music
表。
$ aws dynamodb list-tables --endpoint-url http://localhost:8000
Out:出去:
{
"TableNames": [
"Music"
]
}
PS AWS_SDK_LOAD_CONFIG=1
PS
AWS_SDK_LOAD_CONFIG=1
I found the mistake I made.我发现了我犯的错误。 I've configured params
我已经配置了参数
const serviceConfigOptions: ServiceConfigurationOptions = {
region: "eu-north-1",
endpoint: "http://localhost:8000",
};
but I forgot to pass it to const dynamoDB: AWS.DynamoDB = new AWS.DynamoDB();
但我忘了将它传递给
const dynamoDB: AWS.DynamoDB = new AWS.DynamoDB();
as an argument.作为论据。
Must be必须是
const serviceConfigOptions: ServiceConfigurationOptions = {
region: "eu-north-1",
endpoint: "http://localhost:8000",
};
const dynamoDB: AWS.DynamoDB = new AWS.DynamoDB(serviceConfigOptions);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.