我一直试图通过Scanamo库使用DynamoDB。我的scala代码如下所示:
package my.package
import com.amazonaws.ClientConfiguration
import com.amazonaws.regions.{Region, Regions}
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.gu.scanamo._
import com.gu.scanamo.syntax._
import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.services.dynamodbv2.datamodeling._
object MusicService {
def main(args: Array[String]): Unit = {
val musicService = new MusicService
musicService.getAlbums()
}
}
class MusicService {
def getAlbums() {
val awsCreds = new BasicAWSCredentials("my","creds")
val client = AmazonDynamoDBClient
.builder()
.withRegion(Regions.EU_WEST_2)
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
case class Music(@DynamoDBIndexRangeKey(attributeName = "Artist")
artist: String, @DynamoDBIndexHashKey(attributeName = "SongTitle") songTitle: String);
val table = Table[Music]("Music")
val putOp = table.putAll(Set(
Music("The Killers", "Sam's Town"),
Music("The Killers", "Spaceman")
))
Scanamo.exec(client)(putOp)
}
我在执行putOp时遇到此错误:
Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 0KAFH90JO39COO143LC5H6RPPNVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1638)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1303)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1055)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2186)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2162)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeBatchWriteItem(AmazonDynamoDBClient.java:575)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.batchWriteItem(AmazonDynamoDBClient.java:551)
at com.gu.scanamo.ops.ScanamoInterpreters$$anon$1.apply(ScanamoInterpreters.scala:51)
at com.gu.scanamo.ops.ScanamoInterpreters$$anon$1.apply(ScanamoInterpreters.scala:30)
at cats.free.Free.$anonfun$foldMap$1(Free.scala:126)
at cats.package$$anon$1.tailRecM(package.scala:41)
at cats.free.Free.foldMap(Free.scala:124)
at cats.free.Free.$anonfun$foldMap$1(Free.scala:127)
at cats.package$$anon$1.tailRecM(package.scala:41)
at cats.free.Free.foldMap(Free.scala:124)
at com.gu.scanamo.Scanamo$.exec(Scanamo.scala:17)
at my.package.MusicService.getAlbums(MusicService.scala:39)
at my.package.MusicService$.main(MusicService.scala:14)
at my.package.MusicService.main(MusicService.scala)
我在DynamoDB上的表结构非常简单,看起来像这样:
Table name: Music
Partition key: Artist
Sort key: SongTitle
那就是全部。
请你能给我一些指导,说明为什么会失败以及我能做些什么来解决它?
答案 0 :(得分:0)
首先,您需要交换@DynamoDBIndexHashKey
和@DynamoDBIndexRangeKey
(因为@DynamoDBIndexHashKey
应该用于哈希键 - 艺术家和@DynamoDBIndexRangeKey
用于排序键 - songTitle)。
您还提到Artist是分区键,SongTitle是Sort键。那你为什么要使用@DynamoDBIndexHashKey
和@DynamoDBIndexRangeKey
呢?我想你需要@DynamoDBHashKey
和@DynamoDBRangeKey
代替(如果艺术家和歌曲标题不是索引)。