dynamodb java
在上一篇文章中,我们继续在DynamoDB数据库上插入数据。
在本教程中,我们将对DynamoDB表发出一些基本查询。 主要规则是每个查询都必须使用哈希键。
查询的最简单形式是仅使用哈希键。 我们将在此表上查询Users表。 结果只有一个,因此在迭代“项目”列表上没有用。
public Map<String,AttributeValue> getUser(String email) {Map<String,String> expressionAttributesNames = new HashMap<>();expressionAttributesNames.put("#email","email");Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));QueryRequest queryRequest = new QueryRequest().withTableName(TABLE_NAME).withKeyConditionExpression("#email = :emailValue").withExpressionAttributeNames(expressionAttributesNames).withExpressionAttributeValues(expressionAttributeValues);QueryResult queryResult = amazonDynamoDB.query(queryRequest);List<Map<String,AttributeValue>> attributeValues = queryResult.getItems();if(attributeValues.size()>0) {return attributeValues.get(0);} else {return null;}}
但是,我们可以使用条件发出更复杂的查询。
登录表非常适合作为示例。 我们将发出一个查询,以获取到日期之间的登录尝试。
public List<Map<String ,AttributeValue>> queryLoginsBetween(String email, Date from, Date to) {List<Map<String,AttributeValue>> items = new ArrayList<>();Map<String,String> expressionAttributesNames = new HashMap<>();expressionAttributesNames.put("#email","email");expressionAttributesNames.put("#timestamp","timestamp");Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();expressionAttributeValues.put(":emailValue",new AttributeValue().withS(email));expressionAttributeValues.put(":from",new AttributeValue().withN(Long.toString(from.getTime())));expressionAttributeValues.put(":to",new AttributeValue().withN(Long.toString(to.getTime())));QueryRequest queryRequest = new QueryRequest().withTableName(TABLE_NAME).withKeyConditionExpression("#email = :emailValue and #timestamp BETWEEN :from AND :to ").withExpressionAttributeNames(expressionAttributesNames).withExpressionAttributeValues(expressionAttributeValues);Map<String,AttributeValue> lastKey = null;do {QueryResult queryResult = amazonDynamoDB.query(queryRequest);List<Map<String,AttributeValue>> results = queryResult.getItems();items.addAll(results);lastKey = queryResult.getLastEvaluatedKey();} while (lastKey!=null);return items;}
请记住,DynamoDB提取页面中的数据,因此如果有多个页面,则必须多次发出同一请求。 因此,您必须使用上次评估的密钥来处理下一个请求。
最后但并非最不重要的是,对索引的查询是基本操作之一。 对于本地或全局二级索引,它是相同的例程。
请记住,获取的结果取决于创建表后指定的投影类型。 在我们的情况下,投影类型适用于所有字段。
我们将使用“主管”表。
public Map<String ,AttributeValue> getSupervisor(String company,String factory) {List<Map<String,AttributeValue>> items = new ArrayList<>();Map<String,String> expressionAttributesNames = new HashMap<>();expressionAttributesNames.put("#company","company");expressionAttributesNames.put("#factory","factory");Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();expressionAttributeValues.put(":company",new AttributeValue().withS(company));expressionAttributeValues.put(":factory",new AttributeValue().withS(factory));QueryRequest queryRequest = new QueryRequest().withTableName(TABLE_NAME).withKeyConditionExpression("#company = :company and #factory = :factory ").withIndexName("FactoryIndex").withExpressionAttributeNames(expressionAttributesNames).withExpressionAttributeValues(expressionAttributeValues);QueryResult queryResult = amazonDynamoDB.query(queryRequest);List<Map<String,AttributeValue>> attributeValues = queryResult.getItems();if(attributeValues.size()>0) {return attributeValues.get(0);} else {return null;}}
您可以在github上找到带有单元测试的完整源代码。
翻译自: https://www.javacodegeeks.com/2016/07/__trashed-4.html
dynamodb java