0

I have an attribute ObjectIdentifier in DynamoDb having values of the form

{"Id":"testId","Version":"2020-09-03t16:29:51"}

I need to create a partition key for a GSI using Id+Version. Is it possible to create and use a key like that since DynamoDb mentions that only strings, binary, and number types can be used as partition keys?

2 Answers 2

0

You can put any data you want in the partition key, within the limits of DynamoDB. It sounds like you want something that automatically is set the Id+Version, which isn't possible, however, what you can do is create a field that will be your partition key and set the value to those to fields. Your data might look something like this (I'm using a pipe to separate the values).

{
  "pk": "testId|2020-09-03t16:29:51",
  "ObjectIdentifier": {
    "Id": "testId",
    "Version": "2020-09-03t16:29:51"
  }
}

In this example pk would be the partition key. The value isn't calculated for you, you have to do that yourself.

6
  • Even if I form the partition key pk, how would I set it as a partition key while creating the index? Would something like ObjectIdentifier.pk work?
    – user9460641
    Commented Mar 19, 2021 at 16:08
  • I think I misunderstood your data a bit. I'm going to modify my answer. Commented Mar 19, 2021 at 16:14
  • @zinngg Just to be clear, are you talking about doing this in a base table or when creating an GSI? Commented Mar 19, 2021 at 16:54
  • I'm not suggesting one or the other. I don't know what your needs access patterns are. I'm simply giving you a way to have a partition key that is the combination of two values. If that needs to be the table key, then make pk the partition key on the table. If you need a GSI that has that as the partition key then make pk the partition key on the table. Commented Mar 19, 2021 at 16:59
  • @kirk I am talking about when creating a GSI. Have updated the question for clarity.
    – user9460641
    Commented Mar 19, 2021 at 17:12
0

If I understand your question correctly, it sounds like you have an existing table with the following:

  • some partition key
  • an attribute named ObjectIdentifier which is of type Map
  • possibly some other attributes.

You are asking if it is possible to create a GSI on this table where ObjectIdentifier (of type Map) will be the partition key of the new index.

No, this is not possible.

As you mentioned, a partition key can only be of type String, Binary, or Number. It is possible to create a GSI of type String with attribute name ObjectIdentifier; however, items that were already in the table prior to creating the GSI will not be inserted into the GSI, since they do not have an attribute named ObjectIdentifier of type String. See below for a test/example.

Table view:

Table View

GSI view after creating GSI on String ObjectIdentifier

GSI View