示例如下
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DynamoDBQueryExample {
public static void main(String[] args) {
Region region = Region.US_EAST_1;
DynamoDbClient client = DynamoDbClient.builder().region(region).build();
String tableName = "YourTableName";
String attributeName = "YourAttributeName";
List<String> searchValues = List.of("Value1", "Value2", "Value3");
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
for (int i = 0; i < searchValues.size(); i++) {
expressionAttributeValues.put(":value" + i, AttributeValue.builder().s(searchValues.get(i)).build());
}
String filterExpression = "#attrName IN (" + String.join(",", expressionAttributeValues.keySet()) + ")";
ScanResponse response = client.scan(ScanRequest.builder()
.tableName(tableName)
.filterExpression(filterExpression)
.expressionAttributeNames(Map.of("#attrName", attributeName))
.expressionAttributeValues(expressionAttributeValues)
.build());
System.out.println("Matching items:");
for (Map<String, AttributeValue> item : response.items()) {
System.out.println(item);
}
}
}