簡體   English   中英

不使用主鍵查找項目

[英]Finding item without using primary key

我嘗試使用用戶名在dynamodb表中查找用戶(用戶名+密碼),userid(int)是主鍵,因此getitem可以工作:

val user = Try(table.getItem("userid",2233))//this works

使用用戶名查找用戶(以驗證密碼)無效,但是,我已經嘗試過

我有一個伴隨對象:

object Authentication {
  @DynamoDBTable(tableName = "users")
  case class User(username: String, passwordMd5: String)
  ...
}

並嘗試使用以下方法進行scan():

val scanExpression = new DynamoDBScanExpression()
scanExpression.addFilterCondition("username",
  new Condition()
    .withComparisonOperator(ComparisonOperator.EQ)
    .withAttributeValueList(new AttributeValue().withS("some_username")))`

但這給了我以下錯誤:

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Class class Authentication$User$ must be annotated with interface com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable

我需要解決什么?

解決方法(重寫):

val client = new AmazonDynamoDBClient(new  ProfileCredentialsProvider())
client.setRegion(Region.getRegion(Regions.XXX))
val dynamoDB = new DynamoDB(client)
val table = dynamoDB.getTable("users")
val index = table.getIndex("username-index")

val spec = new QuerySpec()
  .withKeyConditionExpression("username = :v_username")
  .withValueMap(new ValueMap()
  .withString(":v_username", username))

val items = index.query(spec)

val iterator = items.iterator()
val user = Try(iterator.next())

抓住的是使用index.query(spec) NOT table.query(spec)

我需要更多有關如何設置表格以回答問題的信息。

  1. 您還有其他非主要指標嗎?
  2. 您是否使用主鍵? 還是主要+范圍?

為了根據不是主鍵的用戶來查找用戶,請執行以下操作:

  1. 您可以添加二級索引
  2. 進行掃描操作並手動過濾出匹配結果
  3. 添加范圍鍵(即其用戶名)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM