Amazon Dynamodb ConditionalCheckFailedException 抛出 java InaccessibleObjectException

Amazon Dynamodb ConditionalCheckFailedException throws java InaccessibleObjectException

我正在根据某些条件使用 updateItem。如果满足条件,它工作正常,但如果条件失败,它会抛出 ConditionalCheckFailedException 和 java InaccessibleObjectException。

为什么会抛出 InaccessibleObjectException?另外,如何处理?

更新:在 ValidationException 的情况下也会发生错误

updateItemSpec = new UpdateItemSpec()
           .withConditionExpression()

table.update(updateItemSpec).getItem()

您正在为 Amazon DynamoDB 使用旧的 Java API。要更新 table,请考虑放弃 V1 并使用增强客户端——它是 Java V2 的 AWS SDK 的一部分。更多信息在这里:

Mapping items in DynamoDB tables

这是使用增强客户端更新 table 的代码。

public class EnhancedModifyItem {

    public static void main(String[] args) {


        String usage = "Usage:\n" +
                "    UpdateItem <key> <email> \n\n" +
                "Where:\n" +
                "    key - the name of the key in the table (id120).\n" +
                "    email - the value of the modified email column.\n" ;

       if (args.length != 2) {
            System.out.println(usage);
            System.exit(1);
       }

        String key = args[0];
        String email = args[1];
        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .build();

        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        String updatedValue = modifyItem(enhancedClient,key,email);
        System.out.println("The updated name value is "+updatedValue);
        ddb.close();
    }


    public static String modifyItem(DynamoDbEnhancedClient enhancedClient, String keyVal, String email) {
        try {
            //Create a DynamoDbTable object
            DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));

            //Create a KEY object
            Key key = Key.builder()
                    .partitionValue(keyVal)
                    .build();

            // Get the item by using the key and update the email value.
            Customer customerRec = mappedTable.getItem(r->r.key(key));
            customerRec.setEmail(email);
            mappedTable.updateItem(customerRec);
            return customerRec.getEmail();

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }
}

您可以找到所有 V2 DynamoDB examples here.