2015-09-07 38 views
0

我嘗試使用的用戶名在dynamodb表中查找用戶(用戶名+密碼),用戶ID(INT)是主鍵等等的GetItem工作:尋找項目,而無需使用主鍵

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

查找用戶(以驗證密碼)使用的用戶名但不工作,我已經試過這

我有一個同伴對象:

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

並使用此嘗試掃描():

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)table.query(SPEC)

回答

1

我需要一點點關於如何的更多信息你的桌子被設置爲回答問題。

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

爲了查找基於密鑰的用戶,是不是他們的主鍵:

  1. 您可以添加一個二級指數
  2. 執行掃描操作,並手動篩選出匹配的結果
  3. 添加一系列鍵(即自己的用戶名)
+0

我使用,這是對用戶名的索引嘗試,那工作,謝謝。 – FelixHJ