自学免费网站建设内蒙古建设协会网站
web/
2025/10/7 23:35:10/
文章来源:
自学免费网站建设,内蒙古建设协会网站,织梦 网站无法显示该页面,街区网站建设的意义在上一篇文章中#xff0c;我们继续在DynamoDB数据库上插入数据。 在本教程中#xff0c;我们将对DynamoDB表发出一些基本查询。 主要规则是每个查询都必须使用哈希键。 查询的最简单形式是仅使用哈希键。 我们将在此表上查询Users表。 结果只有一个#xff0c;因此在迭代… 在上一篇文章中我们继续在DynamoDB数据库上插入数据。 在本教程中我们将对DynamoDB表发出一些基本查询。 主要规则是每个查询都必须使用哈希键。 查询的最简单形式是仅使用哈希键。 我们将在此表上查询Users表。 结果只有一个因此在迭代“项目”列表上没有用。 public MapString,AttributeValue getUser(String email) {MapString,String expressionAttributesNames new HashMap();expressionAttributesNames.put(#email,email);MapString,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);ListMapString,AttributeValue attributeValues queryResult.getItems();if(attributeValues.size()0) {return attributeValues.get(0);} else {return null;}} 但是我们可以使用条件发出更复杂的查询。 登录表非常适合作为示例。 我们将发出一个查询以获取到日期之间的登录尝试。 public ListMapString ,AttributeValue queryLoginsBetween(String email, Date from, Date to) {ListMapString,AttributeValue items new ArrayList();MapString,String expressionAttributesNames new HashMap();expressionAttributesNames.put(#email,email);expressionAttributesNames.put(#timestamp,timestamp);MapString,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);MapString,AttributeValue lastKey null;do {QueryResult queryResult amazonDynamoDB.query(queryRequest);ListMapString,AttributeValue results queryResult.getItems();items.addAll(results);lastKey queryResult.getLastEvaluatedKey();} while (lastKey!null);return items;} 请记住DynamoDB提取页面中的数据因此如果有多个页面则必须多次发出同一请求。 因此您必须使用上次评估的密钥来处理下一个请求。 最后但并非最不重要的是对索引的查询是基本操作之一。 对于本地或全局二级索引它是相同的例程。 请记住获取的结果取决于创建表后指定的投影类型。 在我们的情况下投影类型适用于所有字段。 我们将使用“主管”表。 public MapString ,AttributeValue getSupervisor(String company,String factory) {ListMapString,AttributeValue items new ArrayList();MapString,String expressionAttributesNames new HashMap();expressionAttributesNames.put(#company,company);expressionAttributesNames.put(#factory,factory);MapString,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);ListMapString,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
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88751.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!