0
我一直試圖通過Scanamo庫使用DynamoDB。我的Scala代碼如下所示:DynamoDB/Scanamo:提供的關鍵元素與模式不匹配
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)
}
我正在上execing的普通網這樣的錯誤:
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
就是這樣。
請你給我一些指導,爲什麼這是失敗的,我能做些什麼來解決它?
感謝您的回覆。我正在使用不正確的註釋。然而,無論註釋是否得到尊重,我仍然遇到同樣的問題。我通過更改案例類中的字段名稱來與DynamoDB表中的字段名相匹配,但它不太理想: 'case class Music(Artist:String,SongTitle:String)' – user3864189