title: AWS SDK Java DynamoDB - Query with Expression Attribute Name - An expression attribute value used in expression is not defined
body: <p>Similar to: <a href="https://stackoverflow.com/questions/63557453/i-cannot-query-my-dynamodb-table-from-aws-lambda-due-to-wrong-filterexpression">I cannot query my dynamodb table from aws lambda due to wrong filterexpression?</a> and <a href="https://stackoverflow.com/questions/39833163/dynamodb-update-error-invalid-updateexpression-an-expression-attribute-value-us">DynamoDB update error Invalid UpdateExpression: An expression attribute value used in expression is not defined</a></p>
  <p>I am trying to code a way to query DynamoDB tables using partial matches on Partition Key / Sort Key in Java.</p>
  <p>The DynamoDB table I am trying to access has a Partition key of &quot;Type&quot; (A restricted key word in DynamoDB, I know, but not my choice) and a Sort key of &quot;Id&quot;. I know the &quot;Type&quot; but not the full Id, so I have researched the Query method using AWS SDK 2.x source code and have implemented as shown below:</p>
  <pre><code>DynamoDBClient dynamoDbClient = DynamoDbClient.builder()
    .region(Region.EU_WEST_1)
    .credentialsProvider(StaticCredentialsProvider.create(awsCredentials))
    .build();
  
  String idKey = &quot;wholeIdKey&quot;;
  String idValue = &quot;partialIdValue&quot;;
  String typeValue = &quot;typeValue&quot;;
  
  Map&lt;String, String&gt; expressionNames = new HashMap&lt;&gt;();
  expressionNames.put(&quot;#t&quot;, &quot;Type&quot;);
  
  QueryRequest request = QueryRequest.builder()
    .tableName(tableName)
    .keyConditionExpression(&quot;begins_with ( &quot; + idKey + &quot;, :&quot; + idValue + &quot; )
       AND #t = :&quot; + typeValue)
    .expressionAttributeNames(expressionNames)
    .build();
  
  QueryResponse response = dynamoDbClient.query(request);
  </code></pre>
  <p>However, when I run this code, I get the following error message:</p>
  <pre><code>Exception in thread &quot;main&quot; software.amazon.awssdk.services.dynamodb.model.DynamoDbException: 
  Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :typeValue
  </code></pre>
  <p>It's as if it's not recognizing the fact that I have told the code use the Expression Attribute Names feature to replace the &quot;#t&quot; with &quot;Type&quot; (Which is a reserved keyword in DynamoDB)</p>
  <p>Can anyone help?</p>
  <p><strong>EDIT: References for code:</strong></p>
  <p><a href="https://docs.aws.amazon.com/code-samples/latest/catalog/javav2-dynamodb-src-main-java-com-example-dynamodb-Query.java.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/code-samples/latest/catalog/javav2-dynamodb-src-main-java-com-example-dynamodb-Query.java.html</a></p>
  <p><a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html</a></p>
  <p><a href="https://www.javadoc.io/static/software.amazon.awssdk/dynamodb/2.7.14/software/amazon/awssdk/services/dynamodb/model/QueryRequest.html#expressionAttributeNames--" rel="nofollow noreferrer">https://www.javadoc.io/static/software.amazon.awssdk/dynamodb/2.7.14/software/amazon/awssdk/services/dynamodb/model/QueryRequest.html#expressionAttributeNames--</a></p>