我尝试使用用户名在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)
...
}
尝试使用以下方法扫描():
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)
答案 0 :(得分:1)
我需要更多关于如何设置表格以回答问题的信息。
为了根据不是主键的密钥查找用户: