简体   繁体   中英

error Attribute name is a reserved keyword

I have data in my dynamoDb like

在此处输入图像描述

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: 在此处输入图像描述

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

  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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM