Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB create index on map or list type

I'm trying to add an index to an attribute inside of a map object in DynamoDB and can't seem to find a way to do so. Is this something that is supported or are indexes really only allowed on scalar values? The documentation around this seems to be quite sparse. I'm hoping that the indexing functionality is similar to MongoDB but so far the approaches I've taken of referencing the attribute to index using dot syntax has not been successful. Any help or additional info that can be provided is appreciated.

like image 206
BryceH Avatar asked May 15 '15 14:05

BryceH


People also ask

What are the two types of indexing in DynamoDB?

DynamoDB supports two different kinds of indexes: Global secondary indexes – The primary key of the index can be any two attributes from its table. Local secondary indexes – The partition key of the index must be the same as the partition key of its table. However, the sort key can be any other attribute.

What is difference between LSI and GSI in DynamoDB?

LSI - allows you to perform a query on a single Hash-Key while using multiple different attributes to "filter" or restrict the query. GSI - allows you to perform queries on multiple Hash-Keys in a table, but costs extra in throughput, as a result.

What is map data type in DynamoDB?

A map is similar to a JSON object. There are no restrictions on the data types that can be stored in a map element, and the elements in a map do not have to be of the same type. Maps are ideal for storing JSON documents in DynamoDB.

Should GSI be unique?

In a DynamoDB table, each key value must be unique. However, the key values in a global secondary index do not need to be unique.


2 Answers

Indexes can be built only on top-level JSON attributes. In addition, range keys must be scalar values in DynamoDB (one of String, Number, Binary, or Boolean).

From http://aws.amazon.com/dynamodb/faqs/:

Q: Is querying JSON data in DynamoDB any different?

No. You can create a Global Secondary Index or Local Secondary Index on any top-level JSON element. For example, suppose you stored a JSON document that contained the following information about a person: First Name, Last Name, Zip Code, and a list of all of their friends. First Name, Last Name and Zip code would be top-level JSON elements. You could create an index to let you query based on First Name, Last Name, or Zip Code. The list of friends is not a top-level element, therefore you cannot index the list of friends. For more information on Global Secondary Indexing and its query capabilities, see the Secondary Indexes section in this FAQ.

Q: What data types can be indexed?

All scalar data types (Number, String, Binary, and Boolean) can be used for the range key element of the local secondary index key. Set, list, and map types cannot be indexed.

like image 183
Ben Schwartz Avatar answered Sep 22 '22 21:09

Ben Schwartz


I have tried doing hash(str(object)) while I store the object separately. This hash gives me an integer(Number) and I am able to use a secondary index on it. Below is a sample in python, it is important to use a hash function which generates the same hash key every time for the value. So I am using sha1.

# Generate a small integer hash: import hashlib def hash_8_digits(source):     return int(hashlib.sha1(source.encode()).hexdigest(), 16) % (10 ** 8) 

The idea is to keep the entire object small while still the entity intact. i.e. rather than serializing and storing the object as string and changing whole way the object is used I am storing a smaller hash value along with the actual list or map.

like image 21
user 923227 Avatar answered Sep 22 '22 21:09

user 923227