Creating indexes from nested structure in DynamoDB - amazon-web-services

I wonder if it's possible to create an index that could look like this
{
"dispenserId": "my-dispenser-123", // primary key
"users": ["user5", "user12"],
"robotId": "my-robot-1",
"enabled": true,
"side": left
}
Based on my DynamoDB documents that look like this
{
"robotId": "my-robot-1", // primary key
"dispensers": {
"left": "left-dispenser-123",
"right": "right-dispenser-123",
"users": ["user5", "user12"]
},
"enabled": true,
"users": ["user1", "user32"]
}
I can't figure out how to point at either dispensers.left or dispensers.right and use that as a key, neither can I figure out how to make a side: left/right attribute based on the path of the dispenser ID.
Can it be achieved with the current structure? If not, what document structure would you guys suggest instead. which allows me to hold the same data?

What you are trying to do (use a map element as a key attribute for an index) is not supported by DynamoDB.
The index partition key and sort key (if present) can be any base table attributes of type string, number, or binary. (Source)
You cannot use (an element of) a map attribute as a a key attribute for an index because the key attribute must be a string, number, or binary attribute from the base table.
Consider using the adjacency list design pattern for your data. It will allow you to easily add both the left and right dispensers to your index.

My new structure looks like this
partition key: robotId
sort key: compoundKey
[
{
"robotId": "robot1",
"enabled": true,
"users": [
"user1",
"user3"
],
"compositeKey": "robot--robot1"
},
{
"robotId": "robot1",
"dispenserId": "dispenser1",
"compositeKey": "dispenser--dispenser1",
"side": "left",
"users": [
"user4",
"user61"
]
}
]
Then I have an index with the dispenserId as partition key, so I can either look the dispensers for a given robot (using the table) or look up details about a dispenser (using the index)

Related

Access element from nlohmann::json?

I want to access element from a json which is the response from one query.
The json structure is :
json = { "result": {
"12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq": [
20964,
347474,
347475
],
"12ashmTiFStQ8RGUpi1BTCinJakVyDKWjRL6SWhnbxbT": [
1992,
1993,
109096
],
}}
I want to get the 1st element(result[0]) key from result object ie 12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq in some variable a and the corresponding array ie [20964, 347474,347475 ] in some varible b.
The problem I am having is that 1st element key value in this case "12CUDzb3oe8RBQ4tYGqsuPsCbsVE4KWfktXRihXf8Ggq" always changes for every query!
Can someone show me the way how can I access it correctly?
json.begin() will give you an iterator pointing to the first element. Then you can access its' key and value using:
auto key = json.begin().key();
auto value = json.begin().value();

Create GSI on an attribute which has the value of Set

So i want to create a simple Dynamodb table called reminders which at the moment has 3 columns :
reminder_id : This is the hash key
reminder_tag: I want to have a global secondary index on this field . But at the same time i want to ensure that the tags attribute should have the datatype of Set . Because there can be multiple tags on a reminder.
reminder_title: I also want to have a global secondary index on this field. This will be a string field.
I checked the documentation : https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/dynamodb.html#valid-dynamodb-types on what are the possible datatypes available in Boto3 .
So i have come up with this script :
import boto3
def create_reminders_table():
"""Just create the reminders table."""
session = boto3.session.Session(profile_name='dynamo_local')
dynamodb = session.resource('dynamodb', endpoint_url="http://localhost:8000")
table = dynamodb.create_table(
TableName='Reminders',
KeySchema=[
{
'AttributeName': 'reminder_id',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'reminder_id',
'AttributeType': 'S'
},
{
'AttributeName': 'reminder_tag',
'AttributeType': 'SS'
},
{
'AttributeName': 'reminder_title',
'AttributeType': 'S'
}
],
GlobalSecondaryIndexes=[
{
'IndexName': 'ReminderTagGsi',
'KeySchema': [
{
'AttributeName': 'reminder_tag',
'KeyType': 'HASH'
}
],
'Projection': {
'ProjectionType': 'INCLUDE',
'NonKeyAttributes': [
'reminder_title'
]
}
},
{
'IndexName': 'ReminderTitleGsi',
'KeySchema': [
{
'AttributeName': 'reminder_title',
'KeyType': 'HASH'
}
],
'Projection': {
'ProjectionType': 'KEYS_ONLY'
}
}
],
BillingMode='PAY_PER_REQUEST'
)
return table
if __name__ == '__main__':
movie_table = create_reminders_table()
print("Table status:", movie_table.table_status)
But when i run this i get the below issue:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: Member must satisfy enum value set: [B, N, S]
I searched and came across this question asked by someone which has the same issue : https://forums.aws.amazon.com/thread.jspa?messageID=613970
Can someone please help me with this since the solution of not providing a datatype either does not work .
Also is it possible to have an index on a an attribute which is of value Set ? I mean i should enable the user to search for reminders with a tag , and for doing that i need to have a set.
Request someone to please help me regarding this.
Is it possible to have an index on an attribute which is of value Set ?
No. As the CreateTable docs say, "the attributes in KeySchema must also be defined in the AttributeDefinitions", to a data type to one of (S)tring, (N)umber or (B)inary."
enable the user to search for reminders with a tag , and for doing that i need to have a set.
A DynamoDB workaround for one-many relations is a composite sort key as in urgent#work. That would only be sensible for a small, fixed number of tags, though.
Your least-bad option is to query by user (and perhaps further narrowing with some sort key), then filtering the results by tag membership outside DynamoDB. (N.B. The IN operator cannot be used in a Query's FilterConditionExpression, so it's of no use to you here).
I want to have a global secondary index on reminder_title
reminder_title is a poor candidate for an index Primary Key. An index's (and table's) Primary Key must ensure per-record uniqueness. A title would likely not. You probably need a combination of 3 elements, user_id, request_id and title, to ensure key uniqueness across records.
Consider a composite Primery Key with, say, user_id for the Partition Key (= HASH) and a compound sort key in a new column (SK) that concatenates title#request_id. You would then search by-user-by-title with:
user_id="Zaphod" AND begins_with(SK, "exercise")
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html
“Each primary key attribute must be a scalar (meaning that it can hold only a single value). The only data types allowed for primary key attributes are string, number, or binary. There are no such restrictions for other, non-key attributes.”

Is there a way to choose a nested field as a partition key in AWS DynamoDB?

I have a JSON document like:
{
"best_answer": {
"answers": {
"a" :"b",
"c" :"d"
},
"question": "random_question"
},
"blurbs": []
}
And I want to create the partition key on the "question" field (nested inside best_answer). How to do this on the AWS Console?
The only way this is possible is to add the "question" entity as a top level attribute on the item, in this case the partition key, in addition to being embedded in the JSON. Whether that is a good partition key remains to be seen. I cannot comment on that without know more about your use case and its access patterns to start with.

dynamodb - scan items by value inside array

I'm doing a table scan. This table has an array as one of its fields, the "apps" field (apps is not a key of any kind). I want to select all rows, whose apps array contains a certain value "MyApp". I tried something of that kind, but my syntax is incorrect:
ComparisonOperator = "#apps CONTAINS :v",
ExpressionAttributeNames = {
'#apps': 'apps'
},
ExpressionAttributeValues = {
":v": "MyApp"
}
Thanks.
The documentation about Condition Expressions clearly states that the appropiate syntax is:
contains(#apps, :v)
The correct request would be:
FilterExpression: "contains(#apps, :v)",
ExpressionAttributeNames: { "#apps": "apps" },
ExpressionAttributeValues: { ":v": "MyApp" }

Create hierarchy from flat list using map/reduce

Suppose I had a Couch instance full of documents like the following:
{"id":"1","parent":null},
{"id":"2","parent":"1"},
{"id":"3","parent":"1"},
{"id":"4","parent":"3"},
{"id":"5","parent":"null"},
{"id":"6","parent":"5"}
Is there a way using MapReduce to build a view that would return my documents in this format:
{
"id":"1",
"children": [
{"id":"2"},
{"id":"3","children":[
{"id":"4"}
]}
]
},
{
"id":"5",
"children": [ {"id":"6"} ]
}
My instinct says "no" because I imagine you'd need one pass for each level of the hierarchy, and items can be nested indefinitely deep.
By using only map function this can not be achieved, yes. But the reduce will have access to the whole list of documents emitted by the map functions: http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Reduce_Functions
To implement that, you will need a robust reduce function, that should also be abled to use "rereduce" efficiently.
In the end, it might be easier to create a view, that will map each document by its parent as key. Example:
function(doc) {
emit(doc.parent, doc._id);
}
This view will allow to query the top level documents with the key "null" and with sub ids like "1", "3" or "5".
A reduce function could added to create a result like this:
null => [1, 5]
1 => [2, 3]
3 => [4]
5 => [6]
The structural tree you whished for is contained therein in a different format and can be created out there.