You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS SDK Java版DynamoDB查询报错:表达式中使用的属性值未定义(涉及保留关键字处理)

Fixing DynamoDB Query Error: "Invalid KeyConditionExpression: An expression attribute value used in expression is not defined"

Let's break down what's going wrong here and fix your code step by step:

Key Issues in Your Current Code

  1. Missing Expression Attribute Values: Your error specifically calls out :typeValue not being defined — that's because you're using placeholders like :typeValue and :partialIdValue in your keyConditionExpression, but never mapping those placeholders to actual values via the expressionAttributeValues parameter.
  2. Incorrect Expression Syntax: You're directly inserting Java variable names (like idKey, idValue) into the expression string, which creates invalid, unrecognized placeholders for DynamoDB.
  3. Wrong Order in Key Condition: DynamoDB requires that the partition key condition comes first in the keyConditionExpression, followed by the sort key condition (you had them reversed).

Corrected Code

DynamoDBClient dynamoDbClient = DynamoDbClient.builder()
    .region(Region.EU_WEST_1)
    .credentialsProvider(StaticCredentialsProvider.create(awsCredentials))
    .build();

String typeValue = "typeValue";
String partialIdValue = "partialIdValue"; // Renamed for clarity
String tableName = "your-table-name";

// Handle reserved keyword "Type" with Expression Attribute Names
Map<String, String> expressionNames = new HashMap<>();
expressionNames.put("#t", "Type");

// Map placeholders to actual values with Expression Attribute Values
Map<String, AttributeValue> expressionValues = new HashMap<>();
expressionValues.put(":typeVal", AttributeValue.builder().s(typeValue).build());
expressionValues.put(":idPartial", AttributeValue.builder().s(partialIdValue).build());

QueryRequest request = QueryRequest.builder()
    .tableName(tableName)
    // Partition key condition first, then sort key begins_with condition
    .keyConditionExpression("#t = :typeVal AND begins_with(Id, :idPartial)")
    .expressionAttributeNames(expressionNames)
    .expressionAttributeValues(expressionValues) // Don't forget this critical parameter!
    .build();

QueryResponse response = dynamoDbClient.query(request);

What Changed & Why

  • Added expressionAttributeValues: This maps the intentional placeholders (:typeVal, :idPartial) to real values that DynamoDB can interpret. Each value is wrapped in an AttributeValue matching the data type of your DynamoDB attributes (string type with .s() here).
  • Fixed keyConditionExpression:
    • Put the partition key condition (#t = :typeVal) first — this is a non-negotiable requirement for DynamoDB Query operations.
    • Used clear, explicit placeholders instead of inserting Java variables directly. The sort key Id isn't a reserved keyword, so we can reference it directly in the expression.
  • Renamed variables for clarity: partialIdValue makes it obvious this is the partial string we're matching against the sort key.

Additional Notes

  • Always separate expression placeholders from actual values: this avoids syntax errors, protects against injection risks, and keeps your code maintainable.
  • For Query operations, the keyConditionExpression must include an equality match on the partition key. You can only add sort key conditions using supported operators like begins_with, between, =, <, >, etc.
  • Double-check that your attribute names (like Id) match exactly with the case used in your DynamoDB table — DynamoDB is case-sensitive for attribute names.

内容的提问来源于stack exchange,提问作者Raisus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:37:48