DynamoDB / Scanamo : The provided key element does not match the schema

1.1k Views Asked by At

I've been trying to use DynamoDB through the Scanamo library. My scala code looks like this:

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)

}

I am getting this error on execing the 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)

My table structure on DynamoDB is incredibly simple and looks like this:

Table name: Music
Partition key: Artist
Sort key: SongTitle

That's all there is.

Please can you give me some guidance why this is failing and what I can do to fix it?

1

There are 1 best solutions below

1
Vasyl Sarzhynskyi On

First of all you need to swap @DynamoDBIndexHashKey and @DynamoDBIndexRangeKey (as @DynamoDBIndexHashKey should be for hash key - artist and @DynamoDBIndexRangeKey for sort key - songTitle).

Also you mentioned that Artist is a Partition key and SongTitle is a Sort key. So why you use @DynamoDBIndexHashKey and @DynamoDBIndexRangeKey? I guess you need @DynamoDBHashKey and @DynamoDBRangeKey instead (in case artist and songTitle are not index).