Amazon dynamodb 对于保存git提交的DynamoDB表,最好的数据模型是什么?
我正在为DynamoDB寻找最合适的表结构 将按存储库保存(git)提交的表 系统中有两个同样流行的请求:Amazon dynamodb 对于保存git提交的DynamoDB表,最好的数据模型是什么?,amazon-dynamodb,Amazon Dynamodb,我正在为DynamoDB寻找最合适的表结构 将按存储库保存(git)提交的表 系统中有两个同样流行的请求: 通过唯一(存储库+sha)[如果不唯一,则拒绝] 通过存储库+sha 获取存储库按升序/降序提交的第一批n 我已经尝试过用一个KeySchema创建一个表,该表具有hash:repository,range:sha 第二个索引是hash:repository,range:createdAt,projection:ALL 这样做的优点是能够以原子方式拒绝提交 如果它与另一个提交(通过Con
唯一(存储库+sha)
[如果不唯一,则拒绝]存储库+sha
n
我已经尝试过用一个KeySchema创建一个表,该表具有
hash:repository,range:sha
第二个索引是hash:repository,range:createdAt,projection:ALL
这样做的优点是能够以原子方式拒绝提交
如果它与另一个提交(通过ConditionExpression)具有相同的repository+sha。
它也有需要2倍大小的缺点(因为我需要能够项目)
第三次查询的所有键)
是否有一种方法可以支持上述三个查询,而不需要2x大小的要求
下面是代码,以澄清: 异步函数createCommitsTable(){ wait dyn.createTable({ 通过供应, 表名:提交, 属性定义:[ {AttributeName:'repository',AttributeType:'S'}, {AttributeName:'sha',AttributeType:'S'}, {AttributeName:'createdAt',AttributeType:'N'}, ], KeySchema:[ {AttributeName:'repository',KeyType:'HASH'}, {AttributeName:'sha',KeyType:'RANGE'}, ], LocalSecondary索引:[{ IndexName:“CreatedAtIndex”, 投影:{ProjectionType:'ALL'}, KeySchema:[ {AttributeName:'repository',KeyType:'HASH'}, {AttributeName:'createdAt',KeyType:'RANGE'}, ] }] }) } 将提交放入表中: 异步函数putCommits(){ 等待dc.put({ 表名:提交, 项目:{ 存储库:“linux”, sha:'6a13feb9c82803e2b815eca72fa7a9f5561d7861', createdAt:Date.now() }, 条件表达式: '属性不存在(存储库)和属性不存在(sha)' }) } 查询表中的单个提交和多个提交(按
createdAt
排序):
异步函数queryCommits(){
console.log('[single commit]:\n',等待dc.get({
表名:提交,
关键:{
存储库:“linux”,
sha:'6a13feb9c82803e2b815eca72fa7a9f5561d7861'
}
}));
console.log(“[commits by repo]:\n”,等待dc.query({
表名:提交,
IndexName:“CreatedAtIndex”,
KeyConditionExpression:'存储库=:存储库',
表达式属性值:{
“:存储库”:“linux”
}
}));
}
尝试使用createdAt和sha的组合键,如下所示:
async function createCommitsTable() {
await dyn.createTable({
ProvisionedThroughput,
TableName: COMMITS,
AttributeDefinitions: [
{AttributeName: 'repository', AttributeType: 'S'},
{AttributeName: 'createdAt-sha', AttributeType: 'S'},
],
KeySchema: [
{AttributeName: 'repository', KeyType: 'HASH'},
{AttributeName: 'createdAt-sha', KeyType: 'RANGE'},
]
})
}
async function putCommits() {
await dc.put({
TableName: COMMITS,
Item: {
repository: 'linux',
createdAt-sha: Date.now() + '-6a13feb9c82803e2b815eca72fa7a9f5561d7861'
},
ConditionExpression:
'attribute_not_exists(repository) and NOT contains("createdAt-sha", sha)'
})
}
如果您使用可排序的时间戳字符串,该字符串将允许您使用createdAt sha连接作为排序键,并在写入时使用contains作为条件,如下所示:
async function createCommitsTable() {
await dyn.createTable({
ProvisionedThroughput,
TableName: COMMITS,
AttributeDefinitions: [
{AttributeName: 'repository', AttributeType: 'S'},
{AttributeName: 'createdAt-sha', AttributeType: 'S'},
],
KeySchema: [
{AttributeName: 'repository', KeyType: 'HASH'},
{AttributeName: 'createdAt-sha', KeyType: 'RANGE'},
]
})
}
async function putCommits() {
await dc.put({
TableName: COMMITS,
Item: {
repository: 'linux',
createdAt-sha: Date.now() + '-6a13feb9c82803e2b815eca72fa7a9f5561d7861'
},
ConditionExpression:
'attribute_not_exists(repository) and NOT contains("createdAt-sha", sha)'
})
}
然后,您可以在表中查询按日期排序的提交。不确定所有这些的格式是否正确,但这应该是总体思路。这正是我要找的。我没有想到使用
contains
来强制唯一性。非常感谢。