yuyu kungkung
yuyu kungkung

Reputation: 137

error Attribute name is a reserved keyword

I have data in my dynamoDb like

enter image description here

my database use priceId as PrimaryKey and symbol as sortKey and other is attributes

i have try use this code

Table table = dynamoDB.getTable(tableName);
        System.out.println("runFirstTime For Search Data");
        String Symbols = "EURUSD";
        String time = "2020-06-10 06:08:07";
        try{
            Item item = table.getItem("symbol", Symbols, "Time", time, "symbol, Price, Time", null);
            System.out.println("Displaying retrieved items...");
            String price = item.getString("Price");
            System.out.println(price);
            System.out.println(item.toJSONPretty());
        }catch (Exception e) {
             System.err.println("Cannot retrieve items.");
             System.err.println(e.getMessage());
          }

but i got an error like

Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: Time (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 5QP69C4BA99JP3LKTKIL8NHB7RVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)

i have updated my database structure become that is symbol as primaryKey and priceId is sortKey and others is attributes: enter image description here

when i use this code

Table table = dynamoDB.getTable(tableName);
        System.out.println("runFirstTime For Search Data");
        String Symbols = "EURUSD";
        String time = "2020-06-10 09:12:07";
        try{
            //GetItemSpec spec = new GetItemSpec().withPrimaryKey("symbol", Symbols).
            Item item = table.getItem("symbol", Symbols, "savetime", time, "symbol, Price, savetime", null);
            System.out.println("Displaying retrieved items...");
            String price = item.getString("Price");
            System.out.println(price);
            System.out.println(item.toJSONPretty());
        }catch (Exception e) {
             System.err.println("Cannot retrieve items.");
             System.err.println(e.getMessage());
          }

i got an error like : The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: KFCMB4GVB2QC7PAQD6AN5CE5HRVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)

my question is : 1. why that is got an error like that ? 2. if i want get all of the price how to code or setup my database using one primaryKey called symbol and attributes called savetime? 3. any example how to retrieve data from dynamoDb

Upvotes: 1

Views: 5981

Answers (2)

fex
fex

Reputation: 307

Regarding Q1: You should use ExpressionAttributeNames, which is the correct tool to prevent name clashes with reserved keywords.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-ExpressionAttributeNames

I do not know the Java API, so here just the command line tool (and using localstack):

awslocal dynamodb query --table-name YourTableName --index-name yourIndexName \
    --key-condition-expression '#time= :time' \
    --expression-attribute-values '{":time":{"S":"2015-02-18T20:27:36.165Z"}}' \
    --expression-attribute-names  '{"#time":"time"}' \
    --return-consumed-capacity TOTAL

Even though this answer might not be perfect, it will definitely give you enough pointers how to solve Q1 without migrating your database!

Upvotes: 2

Ivan Skurdija
Ivan Skurdija

Reputation: 11

  1. You have used reserved word Time as an attribute name, try to define a different name for this attribute.

You can see list of the reserved names here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

  1. To get all prices you can use Scan and in ProjectionExpression set Price.

Check java documentation here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html

Upvotes: 1

Related Questions