aws dynamo db search - amazon-web-services

I have the following keys.txt
{
"test": {"BOOL": true}
}
I run the following command
aws dynamodb get-item --table-name marvel-users-prod --key file://keys.json
but its not returning the items that match the key/value. What am I doing wrong?
I get the error
A client error (ValidationException) occurred when calling the GetItem operation: The provided key element does not match the schema
I tried using the GUI but the scan stopped.

Get-item can be used to get the data by primary key. The DynamoDB hash key can't be BOOL type.
The get-item operation returns a set of attributes for the item with
the given primary key. If there is no matching item, get-item does not
return any data.
Create Table Attribute Types:-
"AttributeType": "S"|"N"|"B"
Also, I assume that the boolean attribute that you would like to filter is one of the attributes in the Dynamodb table. You may need to scan the table if you don't include the hash key in the filter criteria.
Scan command:-
"interested" - is my BOOL attribute name
:a - Attribute value placeholder. The value is present in the JSON file
aws dynamodb scan --table-name autotable --filter-expression "interested = :a" --expression-attribute-values file://scan-movies.json
Scan Movies JSON file:-
{
":a": {"BOOL" : true}
}

Related

Key element does not match the schema boto3 UpdateItem

I created a table on DynamoDB with partition key Robot (Number) ans sort key Status (string). I have written this code to update the Status of two items in a given condition through this code
import boto3
from boto3.dynamodb.conditions import Key
TABLE_NAME = "RobotStatus"
# Creating the DynamoDB Client
dynamodb_client = boto3.client('dynamodb', region_name="eu-central-1")
# Creating the DynamoDB Table Resource
dynamodb=boto3.resource('dynamodb', region_name="eu-central-1")
table=dynamodb.Table(TABLE_NAME)
# Use the DynamoDB client query method to get robot1's status
response=dynamodb_client.query(
TableName=TABLE_NAME,
KeyConditionExpression='Robot = :Robot',
ExpressionAttributeValues={
':Robot': {'N': "1"}
}
)
if response['Items'][0]['Status']['S'] == "disconnected":
response=table.update_item(
Key={'Robot': "2"},
UpdateExpression = "set #st = :s",
ExpressionAttributeValues={":s" : "disconnecting"},
ExpressionAttributeNames={"#st":"Status"},
ReturnValues="UPDATED_NEW"
)
response=table.update_item(
Key={'Robot': "3"},
UpdateExpression="set #st = :s",
ExpressionAttributeValues = {":s" : "disconnecting"},
ExpressionAttributeNames={"#st":"Status"},
ReturnValues="UPDATED_NEW"
)
but once I run the script this error pops up
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema
I have also tried to add the sort key inside the Key field of UpdateItem but nothing changes, do you know what could be the problem?

AWS DynamoDB Attribute Names containing Spaces

I have an AWS DynamoDB table that is populated via a Lambda script triggered by a web form.
The table ends up with Attribute Names like "Full Name" and "Phone Number".
From the aws CLI in PowerShell I can run:
aws dynamodb scan --table-name cc_career --filter-expression 'ID = :t' --expression-attribute-values '{\":t\":{\"N\":\"12\"}}'
and it will return expected values (Attribute Name = ID, value = 12).
But if I want to filter on the attribute "Full Name", for example:
aws dynamodb scan --table-name cc_career --filter-expression 'Full Name = :t' --expression-attribute-values '{\":t\":{\"S\":\"Sherman Horton\"}}'
I get
An error occurred (ValidationException) when calling the Scan operation: Invalid FilterExpression: Syntax error; token: "Name", near: "Full Name ="
How does one properly escape or specify an Attribute Name that contains a space?
I read up on using "expression attribute names" from the docs. But even this example:
aws dynamodb scan --table-name cc_career --return-consumed-capacity "TOTAL" --projection-expression "#fn,#dt" --expression-attribute-names '{\"#fn\":\"Email\",\"#dt\":\"Full Name\"}'
will execute without error BUT not return the "Full Name" data.
I did a pretty thorough 'net search on this topic but found nothing. Surely it's a common use case!
You are right about using an expression attribute names
If an attribute name begins with a number, contains a space or contains a reserved word, you must use an expression attribute name to replace that attribute's name in the expression.
aws dynamodb scan --table-name cc_career --return-consumed-capacity "TOTAL" --projection-expression "#fn,#dt" --expression-attribute-names '{\"#fn\":\"Email\",\"#dt\":\"Full Name\"}'
The problem here is that you are missing filter expression https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Scan.html#Scan.FilterExpression
Considering the table design with ID as PK and Full Name and Phone Number using below command I was able to scan Data based on Full Name
aws dynamodb scan --table-name stack_overflow --filter-expression "#fn = :t" --expression-attribute-values '{":t":{"S":"Jatin Mehrotra"}}' --expression-attribute-name '{"#fn":"Full Name"}
My results after running above command
{
"Items": [
{
"ID": {
"N": "1"
},
"Full Name": {
"S": "Jatin Mehrotra"
},
"Phone Number": {
"S": "123456789"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}

AWS Step Function get all Range keys with a single primary key in DynamoDB

I'm building AWS Step Function state machines. My goal is to read all Items from a DynamoDB table with a specific value for the Hash key (username) and any (wildcard) Sort keys (order_id).
Basically something that would be done from SQL with:
SELECT username,order_id FROM UserOrdersTable
WHERE username = 'daffyduck'
I'm using Step Functions to get configurations for AWS Glue jobs from a DynamoDB table and then spawn a Glue job via step function's Map for each dynamodb item (with parameters read from the database).
"Read Items from DynamoDB": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {
"TableName": "UserOrdersTable",
"Key": {
"username": {
"S": "daffyduck"
},
"order_id": {
"S": "*"
}
}
},
"ResultPath": "$",
"Next": "Invoke Glue jobs"
}
But I can't bring the state machine to read all order_id's for the user daffyduck in the step function task above. No output is displayed using the code above, apart from http stats.
Is there a wildcard for order_id ? Is there another way of getting all order_ids? The query customization seems to be rather limited inside step functions:
https://docs.amazonaws.cn/amazondynamodb/latest/APIReference/API_GetItem.html#API_GetItem_RequestSyntax
Basically I'm trying to accomplish what can be done from the command line like so:
$ aws dynamodb query \
--table-name UserOrdersTable \
--key-condition-expression "Username = :username" \
--expression-attribute-values '{
":username": { "S": "daffyduck" }
}'
Any ideas? Thanks
I don't think that is possible with Step functions Dynamodb Service yet.
currently supports get, put, delete & update Item, not query or scan.
For GetItem we need to pass entire KEY (Partition + Range Key)
For the primary key, you must provide all of the attributes. For
example, with a simple primary key, you only need to provide a value
for the partition key. For a composite primary key, you must provide
values for both the partition key and the sort key.
We need to write a Lambda function to query Dynamo and return a map and invoke the lambda function from step.

Batch write to DynamoDB

I am trying to write data using batch_writer into DynamoDB using Lambda function. I am using "A1" as the partition key for my DynamoDB and when I try to pass the following Json input it works well.
{
"A1":"001",
"A2":{
"B1":"100",
"B2":"200",
"B3":"300"
}
}
When I try to send the following request I get an error.
{
"A1":{
"B1":"100",
"B2":"200",
"B3":"300"
}
}
Error -
"errorMessage": "An error occurred (ValidationException) when calling the BatchWriteItem operation: The provided key element does not match the schema"
Is it possible to write the data into DynamoDB using lambda function for this data and what should I change in my code to do that?
My code -
def lambda_handler(event, context):
with table.batch_writer() as batch:
batch.put_item(event)
return {"code":200, "message":"Data added success"}
It's hard to say without seeing the table definition, but my bet is that "A1" is the primary key of type string. If you try setting it to a map, it will fail.

how to return items in a dynamodb on aws-cli

So, I have a DynamoDB table Users and I want to return all the contents of this table. Or maybe even some.
I tried
aws dynamodb query --table-name Users
and it says I have to specify key-condition or key-condition-expression, so I added the following:
aws dynamodb query --table-name Users --key-condition-expression Username = "test"
and it returns an error message " Unknown options: test ".
If you want to dump the whole table, just use
aws dynamodb scan --table-name Users
Try this format:
aws dynamodb get-item --table-name Users --key '{"Username": {"S": "test"}}'
Since the question is about using the query operation, here it goes.
As the AWS cli documentation explains, you should separate the attribute values from the condition, by using the --expression-attribute-values parameter:
aws dynamodb query --table-name Users
--key-condition-expression "Username = :v1"
--expression-attribute-values "{ \":v1\" : { \"S\" : \"test\" } }"
Additionally, you may combine more attributes in the filter (in my case I have a Datetime sort key I want to filter by):
aws dynamodb query
--table-name Users
--key-condition-expression "Username = :v1 AND #Datetime BETWEEN :v2 AND :v3"
--expression-attribute-names "{ \"#Datetime\": \"Datetime\" }"
--expression-attribute-values "{ \":v1\" : { \"S\" : \"test\" }, \":v2\" : { \"S\" : \"2019-06-06\" }, \":v3\" : { \"S\" : \"2019-06-07\" } }"
Here the #Datetime mapping is done through the --expression-attribute-names parameter, because Datetime is a reserved keyword, so I cannot use it inside the key condition expression.
As per my understanding you are not passing "key"(hash or hash/range) properly
create a file containing your keys:
test.json
{
"userName": {"S": "abc"},
"anyRangeKey": {"S": "xyz"} //optional
}
Run
aws dynamodb get-item --table-name users --key file://test.json
refer:http://docs.aws.amazon.com/cli/latest/reference/dynamodb/get-item.html
Hope that helps
aws dynamodb get-item --table-name ProductCatalog --key "{""Id"":{""N"":""205""}}" --no-verify-ssl