【发布时间】:2020-08-08 19:37:51
【问题描述】:
我正在使用 Serverless 和 DynamoDB,并且对它比较陌生。我的应用程序有一个名为 Trips 的表。表格的参数是{id, route, cost, sell, type, date, LR, asset }和一堆其他不相关的单据编号,其中id是由uuid生成的。
根据this guide for ordering data in a date range(见最后表中的第5行)https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-modeling-nosql-B.html我添加了一个名为createdAt的参数,它是一个从1到20的随机整数,由前端发送和用HASH key 和SORT key 作为date 创建了一个GSI。
我同时运行了 20 个 promise,以保持 createdAt 从 1 到 20。我只是将所有结果合并到一个对象中,就像这样。
await Promise.all(promises).then(function (values) {
console.log(values);
values.map((value) => {
value.map((v) => {
tripdata.push(v);
});
});
});
这是回复
[
[
{
Cost: 12128,
date: '2020-04-01',
RouteShortCode: 'Hazira-Manjusar-Sudeep',
Selling: 11000,
Type: 'Export'
},
{
Cost: 12581,
date: '2020-04-24',
RouteShortCode: 'Hazira-Nandesari-Kevin',
Selling: 10000,
Type: 'Export'
}
],
[
{
Cost: 12691,
date: '2020-04-09',
RouteShortCode: 'Hazira-Nandesari-Kevin',
Selling: 10000,
Type: 'Export'
}
],
[
{
Cost: 11536,
date: '2020-04-09',
RouteShortCode: 'Hazira-Nandesari-Omega',
Selling: 29000,
Type: 'Import'
},
{
Cost: 8973.5,
date: '2020-04-18',
RouteShortCode: 'Hazira-Manjusar-Sudeep',
Selling: 11000,
Type: 'Export'
}
],
[
{
Cost: 11665,
date: '2020-04-20',
RouteShortCode: 'Hazira-Nandesari-Kevin',
Selling: 10000,
Type: 'Export'
}
],
]
tripsTable 的 Serverless.yml
tripTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
[
{ "AttributeName": "id", "AttributeType": "S" },
{ "AttributeName": "date", "AttributeType": "S" },
{ "AttributeName": "createdAt", "AttributeType": "N" },
{ "AttributeName": "Asset", "AttributeType": "S" },
]
# { "AttributeName": "Route", "AttributeType": "S" },
KeySchema:
[
{ "AttributeName": "date", "KeyType": "HASH" },
{ "AttributeName": "id", "KeyType": "RANGE" },
]
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
StreamSpecification:
StreamViewType: "NEW_AND_OLD_IMAGES"
TableName: ${self:provider.environment.TRIPS}
GlobalSecondaryIndexes:
- IndexName: DateVSTrips
KeySchema:
- AttributeName: createdAt
KeyType: HASH
- AttributeName: date
KeyType: RANGE
Projection:
ProjectionType: "ALL"
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
LocalSecondaryIndexes:
- IndexName: TripsVSRoutes
KeySchema:
- AttributeName: date
KeyType: HASH
- AttributeName: Asset
KeyType: RANGE
Projection:
ProjectionType: ALL
但问题是 2020/04/01 和 2020/04/24 上的条目具有相同的 createdAt 参数,因此在组合所有数组结果后,最终结果不按日期排序。
我需要重新排序这个列表还是我在这里遗漏了什么?如果我需要重新排序,它会不会变得更加低效?
【问题讨论】:
-
是否有效,或者您有问题?
-
@RichardRublev 否,最终结果未按日期排序,这会产生问题,因为我需要该数据用于图形。我已添加回复,请检查
-
向我们展示您的 serverelss.yml。
-
@RichardRublev 添加
-
您要按
date对结果进行排序吗?还是createdAt?请告诉我们query或scan。
标签: amazon-web-services database-design nosql amazon-dynamodb serverless