Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Use begins_with method on primary key in DynamoDB?

Tags:

I have Table with 2 attribute id(string,primary key), value(string). When I try follwoing KeyConditionExpression it throws Query key condition not supported.

KeyConditionExpression: "begins_with(ID, :tagIDValue)"                          or KeyConditionExpression: "contains(ID, :tagIDValue)" 

From this link I came to know we can use only EQ operations on main key. How can I achieve this

Solution:======================================================

I need to use begins_with or contains to filter So I went with following approach.

Table attributes: PK(partion_key, string), ID(sort key, string), value(string).

Now my primary key is framed based on PK,ID

PK will have constant value for all rows. so KeyConditionExpression will be like.

KeyConditionExpression: "PL = :pk  and begins_with(ID, :tagIDValue)" 

NOTE: But still contains not working with KeyConditionExpression. I think it was removed from KeyConditionExpression

like image 219
Praveenkumar Avatar asked Sep 20 '16 09:09

Praveenkumar


People also ask

What data types can you use for the primary key DynamoDB?

When you create a table or a secondary index, you must specify the names and data types of each primary key attribute (partition key and sort key). Furthermore, each primary key attribute must be defined as type string, number, or binary.

Which API call would you use to Query an item by its primary key?

With the DynamoDB API, you use the PutItem operation to add an item to a table. DynamoDB provides the GetItem action for retrieving an item by its primary key.

Can DynamoDB have 2 primary keys?

DynamoDB supports two types of primary keys: Partition key: A simple primary key, composed of one attribute known as the partition key. Attributes in DynamoDB are similar in many ways to fields or columns in other database systems.

Can we update primary key in DynamoDB?

You cannot update the primary key attributes using UpdateItem. Instead, delete the item and use PutItem to create a new item with new attributes. The UpdateItem operation includes an Action parameter, which defines how to perform the update. You can put, delete, or add attribute values.


2 Answers

You can use begins_with and contains only with a range key after specifying an EQ condition for the primary key.

To use EQ with the primary key you can do

KeyConditionExpression: "ID = :tagIDValue" 
like image 107
Tolbahady Avatar answered Oct 22 '22 00:10

Tolbahady


I disagree with Tolbahady statement regarding begins_with and contains only workable in range key. You can use any comparison operator in any keys using method scan. Scan is a flexible but expensive and inefficient way of conducting dynamodb queries.

Anyways, there is a tool named, AWS NoSQL Workbench. It is little bit like MySQL Workbench. What is the good thing about it, is you can construct your table while checking against your access patterns(possible and most used queries of your application against your table).

like image 23
lukaserat Avatar answered Oct 22 '22 00:10

lukaserat