sentenza
sentenza

Reputation: 1730

How to create two or more GSI in Local DynamoDB using Scanamo

I would like to create two or more Global Secondary Indexes (GSI) using Scanamo v1.0.0-M23 and in particular

case class UnLocation(
    id: Option[UUID],
    countryCode: String,
    unCode: String,
    name: String,
    asciiName: String,
    region: Option[String],
    function: Option[String],
    iata: Option[String],
    source: String
)

    LocalDynamoDB.createTableWithIndex(
      client,
      repo.unLocationTableName,
      secondaryIndexName = repo.gsiName, // Global Secondary Index
      List("unCode" -> S), // Primary index attributes
      List("iata"   -> S) // Secondary index attributes
      //   ^^^^^^^^^^^^ How can I define a series of Global Secondary Indexes??
    )

I would like to create GSIs for iata, id and for name (hash) and region (sort). Looking at createTableWithIndex() in details I don't get how to create a set of indexes:

// org.scanamo.LocalDynamoDB

  def createTableWithIndex(
    client: DynamoDbAsyncClient,
    tableName: String,
    secondaryIndexName: String,
    primaryIndexAttributes: List[(String, ScalarAttributeType)],
    secondaryIndexAttributes: List[(String, ScalarAttributeType)]
  )

If I try to pass more than 2 values as secondaryIndexAttributes: List[(String, ScalarAttributeType)]:

    LocalDynamoDB.createTableWithIndex(
      client,
      repo.unLocationTableName,
      secondaryIndexName = repo.gsiName, // Global Secondary Index
      List("unCode" -> S), // Primary index attributes
      List("iata"   -> S, "id" -> S, "name" -> S) // Secondary index attributes
    )

Then I get:

software.amazon.awssdk.services.dynamodb.model.DynamoDbException: 
Key Schema too big.  
Key Schema must at most consist of the hash and range key of a table (Service: DynamoDb, Status Code: 400

Because I can only provide a single GSI having hash/range, but I need several GSI in order to perform query operations against DynamoDB table.

How can I solve this?

Dynamo-Local

Started using docker-compose:

  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal

Creating the table using AWS CLI

aws dynamodb create-table \
--region=eu-west-1 \
--endpoint-url http://localhost:8000 \
--table-name UnLocationTest \
--attribute-definitions \
    AttributeName=unCode,AttributeType=S \
    AttributeName=id,AttributeType=S \
    AttributeName=countryCode,AttributeType=S \
    AttributeName=name,AttributeType=S \
    AttributeName=iata,AttributeType=S \
--key-schema \
    AttributeName=unCode,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10 \
--global-secondary-indexes \
        "[
            {
                \"IndexName\": \"IataIndex\",
                \"KeySchema\": [{\"AttributeName\":\"iata\",\"KeyType\":\"HASH\"}],
                \"Projection\":{\"ProjectionType\":\"ALL\"},
                \"ProvisionedThroughput\": {
                    \"ReadCapacityUnits\": 10,
                    \"WriteCapacityUnits\": 5
                }
            },
            {
                \"IndexName\": \"IdIndex\",
                \"KeySchema\": [{\"AttributeName\":\"id\",\"KeyType\":\"HASH\"}],
                \"Projection\":{\"ProjectionType\":\"ALL\"},
                \"ProvisionedThroughput\": {
                    \"ReadCapacityUnits\": 10,
                    \"WriteCapacityUnits\": 5
                }
            },
            {
                \"IndexName\": \"NameIndex\",
                \"KeySchema\": [{\"AttributeName\":\"countryCode\",\"KeyType\":\"HASH\"},
                                {\"AttributeName\":\"name\",\"KeyType\":\"RANGE\"}],
                \"Projection\":{\"ProjectionType\":\"ALL\"},
                \"ProvisionedThroughput\": {
                    \"ReadCapacityUnits\": 10,
                    \"WriteCapacityUnits\": 5
                }
            }
        ]"

The result (via aws cli)

{
  "TableDescription": {
    "AttributeDefinitions": [
      {
        "AttributeName": "unCode",
        "AttributeType": "S"
      },
      {
        "AttributeName": "id",
        "AttributeType": "S"
      },
      {
        "AttributeName": "countryCode",
        "AttributeType": "S"
      },
      {
        "AttributeName": "name",
        "AttributeType": "S"
      },
      {
        "AttributeName": "iata",
        "AttributeType": "S"
      }
    ],
    "TableName": "UnLocationTest",
    "KeySchema": [
      {
        "AttributeName": "unCode",
        "KeyType": "HASH"
      }
    ],
    "TableStatus": "ACTIVE",
    "CreationDateTime": "2023-04-20T16:50:33.705000+02:00",
    "ProvisionedThroughput": {
      "LastIncreaseDateTime": "1970-01-01T01:00:00+01:00",
      "LastDecreaseDateTime": "1970-01-01T01:00:00+01:00",
      "NumberOfDecreasesToday": 0,
      "ReadCapacityUnits": 10,
      "WriteCapacityUnits": 10
    }
  },
  "TableSizeBytes": 0,
  "ItemCount": 0,
  "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/UnLocationTest",
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "NameIndex",
      "KeySchema": [
        {
          "AttributeName": "countryCode",
          "KeyType": "HASH"
        },
        {
          "AttributeName": "name",
          "KeyType": "RANGE"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "IndexStatus": "ACTIVE",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 10,
        "WriteCapacityUnits": 5
      },
      "IndexSizeBytes": 0,
      "ItemCount": 0,
      "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/UnLocationTest/index/NameIndex"
    },
    {
      "IndexName": "IdIndex",
      "KeySchema": [
        {
          "AttributeName": "id",
          "KeyType": "HASH"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "IndexStatus": "ACTIVE",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 10,
        "WriteCapacityUnits": 5
      },
      "IndexSizeBytes": 0,
      "ItemCount": 0,
      "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/UnLocationTest/index/IdIndex"
    },
    {
      "IndexName": "IataIndex",
      "KeySchema": [
        {
          "AttributeName": "iata",
          "KeyType": "HASH"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "IndexStatus": "ACTIVE",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 10,
        "WriteCapacityUnits": 5
      },
      "IndexSizeBytes": 0,
      "ItemCount": 0,
      "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/UnLocationTest/index/IataIndex"
    }
  ]
}

Upvotes: 1

Views: 225

Answers (0)

Related Questions