英文:
How to add attribute to DynamoDB CDK table [TypeScript]
问题
我是新手,正在尝试创建一个用于 DynamoDB 表的 CDK。我的表设计如下:
@DynamoDBTable(tableName = "ccd")
public class abcd {
@DynamoDBHashKey(attributeName = "id")
private String id;
@DynamoDBAttribute(attributeName = "ack")
@DynamoDBIndexHashKey(attributeName = "ack", globalSecondaryIndexName = "ack-i")
private String acknowledged;
@DynamoDBAttribute(attributeName = "Go")
private String go;
@DynamoDBAttribute(attributeName = "ac")
private long ac;
@DynamoDBAttribute(attributeName = "amt")
private long amt;
@DynamoDBAttribute(attributeName = "cr")
private int cr;
@DynamoDBAttribute(attributeName = "ttl")
private long ttl;
}
在我的 CDK 中,app.ts 大致如下:
export class MyCdkAppStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// 创建 DynamoDB 表
const table = new dynamodb.Table(this, 'MyTable', {
tableName: 'ccd',
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING,
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
// 为 "ACK" 属性添加全局二级索引
table.addGlobalSecondaryIndex({
indexName: 'ack-i',
partitionKey: {
name: 'ack',
type: dynamodb.AttributeType.STRING,
},
});
}
}
你想要如何添加其他属性到表中,比如 Go、ac、ttl 等?
当我尝试使用 table.addAttributes
或 attributeDefinitions: []
时,在我的 IDE 中出现错误。
英文:
I am new to creating CDK and I am trying to create a CDK for a dynamoDB table.
I have table design like this.
@DynamoDBTable(tableName = "ccd")
public class abcd {
@DynamoDBHashKey(attributeName = "id")
private String id;
@DynamoDBAttribute(attributeName = "ack")
@DynamoDBIndexHashKey(attributeName = "ack", globalSecondaryIndexName = "ack-i")
private String acknowledged;
@DynamoDBAttribute(attributeName = "Go")
private String go;
@DynamoDBAttribute(attributeName = "ac")
private long ac;
@DynamoDBAttribute(attributeName = "amt")
private long amt;
@DynamoDBAttribute(attributeName = "cr")
private int cr;
@DynamoDBAttribute(attributeName = "ttl")
private long ttl;
}
In the CDK my app.ts is something like this
export class MyCdkAppStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create the DynamoDB table
const table = new dynamodb.Table(this, 'MyTable', {
tableName: 'ccd',
partitionKey: {
name: 'id',
type: dynamodb.AttributeType.STRING,
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
// Add global secondary index for "ACK" attribute
table.addGlobalSecondaryIndex({
indexName: 'ack-i',
partitionKey: {
name: 'ack',
type: dynamodb.AttributeType.STRING,
},
});
}
How should I add other attributed to the table ? For example Go, ac, ttl etc.
When I am doing table.addAttributes or attributeDefinitions : []
I am getting error in my IDE.
答案1
得分: 1
你不需要将非关键属性添加到表定义中。由于DynamoDB是无模式的,您只需要定义键,所有其他属性可以在插入或更新数据时按需要进行更改。
英文:
You don't add non-key attributes to your tables definition. As DynamoDB is schemaless, you need only define your keys, all other attributes can be altered and changed as you like on a per-item basis while inserting or updating the data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论