Skip to content

Query by Inner class key - enhanced client #2618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
reply2srij opened this issue Jul 22, 2021 · 2 comments
Closed

Query by Inner class key - enhanced client #2618

reply2srij opened this issue Jul 22, 2021 · 2 comments
Labels
closed-for-staleness dynamodb-enhanced guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.

Comments

@reply2srij
Copy link

reply2srij commented Jul 22, 2021

My project has a structure similar to below. How do I query by InnerClassPrimaryKey? Tried to use filterExpression but that is not working.

AWS Java SDK version used: AWS SDK 2.16.99
JDK version used: Open JDK 11
Operating System and version: Windows 10

@DynamoDbBean
public class OuterClass {	
	private String outerClassPrimaryKey;	
	private List<InnerClass> innerClassList;

	@DynamoDbPartitionKey
	public String getOuterClassPrimaryKey() { return outerClassPrimaryKey; }
	public void setOuterClassPrimaryKey(String outerClassPrimaryKey) { this.outerClassPrimaryKey = outerClassPrimaryKey; }
	public List<InnerClass> getInnerClassList() { return innerClassList; }
	public void setInnerClassList(List<InnerClass> innerClassList) { this.innerClassList = innerClassList; }
}

@DynamoDbBean
public class InnerClass {
	private String innerClassPrimaryKey;
	private String innerClassAttribute1;
	private String innerClassAttribute2;

	@DynamoDbPartitionKey
	public String getInnerClassPrimaryKey() { return innerClassPrimaryKey; }
	public void setInnerClassPrimaryKey(String innerClassPrimaryKey) { this.innerClassPrimaryKey = innerClassPrimaryKey; }
	public String getInnerClassAttribute1() { return innerClassAttribute1; }
	public void setInnerClassAttribute1(String innerClassAttribute1) { this.innerClassAttribute1 = innerClassAttribute1; }
	public String getInnerClassAttribute2() { return innerClassAttribute2; }
	public void setInnerClassAttribute2(String innerClassAttribute2) { this.innerClassAttribute2 = innerClassAttribute2; }
}

public class InnerOuterDynamoDbService {
	private DynamoDbClient ddb;
	private DynamoDbEnhancedClient eddb;
	private TableSchema<OuterClass> outerClassSchema;
	private DynamoDbTable<OuterClass> outerClassTable;

	public InnerOuterDynamoDbService(DynamoDbClient ddb) {
		this.ddb = ddb;
		this.eddb = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
		this.outerClassSchema = TableSchema.fromClass(OuterClass.class);
		this.outerClassTable = eddb.table(OuterClass.class.getSimpleName(), outerClassSchema);
	}
	public OuterClass getOuterClass(String key) {
		return outerClassTable.getItem(Key.builder().partitionValue(key).build());
	}
	public OuterClass getOuterClassByInnerPk(String outerClassPrimaryKey, String innerClassPrimaryKey) {
		AttributeValue attribute = AttributeValue.builder().s(innerClassPrimaryKey).build();

		Expression expression = Expression.builder().expression("innerClassPrimaryKey = :value")
				.putExpressionValue(":value", attribute).build();

		Key partitionKey = Key.builder().partitionValue(outerClassPrimaryKey).build();
		QueryConditional queryConditional = QueryConditional.keyEqualTo(partitionKey);

		PageIterable<OuterClass> results = outerClassTable.query(
				QueryEnhancedRequest.builder().queryConditional(queryConditional).filterExpression(expression).build());
		return results.items().stream().collect(Collectors.toList()).get(0);
	}

	public void putOuterClass(OuterClass item) { outerClassTable.putItem(item); }

	public void createOuterTable() {
		outerClassTable.createTable();
		outerClassTable.tableSchema().tableMetadata().toString();
	}
}
@reply2srij reply2srij added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Jul 22, 2021
@reply2srij reply2srij changed the title Query by Inner class key Query by Inner class key - enhanced client Jul 22, 2021
@debora-ito
Copy link
Member

Hi @reply2srij I'm sorry for the delay in reply.

A Query condition expression needs to include the table primary key, you cannot provide the inner class primary key in this case.

I think using the inner class in the filter expression should work, not using a List though but using a Map. As a map you can access the nested attributes by name, take a look at the DynamoDB Developer guide: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html

@debora-ito debora-ito added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. dynamodb-enhanced and removed needs-triage This issue or PR still needs to be triaged. labels Jul 29, 2021
@github-actions
Copy link

github-actions bot commented Aug 6, 2021

It looks like this issue hasn’t been active in longer than a week. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please add a comment to prevent automatic closure, or if the issue is already closed please feel free to reopen it.

@github-actions github-actions bot added closing-soon This issue will close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will close in 4 days unless further comments are made. labels Aug 6, 2021
aws-sdk-java-automation pushed a commit that referenced this issue Jul 13, 2023
…9c76d5feb

Pull request: release <- staging/92de2758-d899-4226-b4bb-d889c76d5feb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness dynamodb-enhanced guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.
Projects
None yet
Development

No branches or pull requests

2 participants