如何获取 DynamoDB 项目的排名/位置?

问题描述 投票:0回答:1

我在 dynamodb 中有一个用户排行榜,lbId 上有 GSI,这是排行榜 id。如下图:

   {
  "TableName":"user_leaderboard",
  "AttributeDefinitions":[
    {
      "AttributeName":"id",
      "AttributeType":"S"
    },
    {
      "AttributeName":"lbId",
      "AttributeType":"S"
    },
    {
      "AttributeName": "score",
      "AttributeType": "N"
    }
  ],
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "lb_index",
      "KeySchema": [
        {
          "AttributeName":"lbId",
          "KeyType": "HASH"
        },
        {
          "AttributeName": "score",
          "KeyType": "RANGE"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "ProvisionedThroughput":{
        "ReadCapacityUnits":1,
        "WriteCapacityUnits":1
      }
    }
  ],
  "KeySchema":[
    {
      "AttributeName":"id",
      "KeyType":"HASH"
    },
    {
      "AttributeName":"lbId",
      "KeyType":"RANGE"
    }
  ],
  "ProvisionedThroughput":{
    "ReadCapacityUnits":1,
    "WriteCapacityUnits":1
  }
}

排行榜每个排行榜最多有 50-100 个用户。我想获取特定用户的排名或位置。但我不知道我该怎么做?获取一张表的所有用户,在这种情况下用户数量约为 50-100 并从数组中获取索引,或者是否有更好的方法来做到这一点?我看到很多相同或相似的问题,但没有答案。

amazon-dynamodb nosql
1个回答
0
投票

你的问题不清楚。我会做一些假设。您想要特定排行榜的特定用户分数/排名。假设

id
是您的用户标识符,那么您只需从基表中执行
GetItem
即可。

SELECT * FROM mytable WHERE id = 'x' and lbId = 'Y'

如果您希望给定排行榜的排名用户按顺序排列,那么您只需在索引上使用

Query
即可。

SELECT * FROM mytable.myindex WHERE lbId = 'Y' DESC

© www.soinside.com 2019 - 2024. All rights reserved.