Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查询DynamoDB表中包含特定字符串的表_Java_Spring_Amazon Web Services_Spring Boot_Amazon Dynamodb - Fatal编程技术网

Java 查询DynamoDB表中包含特定字符串的表

Java 查询DynamoDB表中包含特定字符串的表,java,spring,amazon-web-services,spring-boot,amazon-dynamodb,Java,Spring,Amazon Web Services,Spring Boot,Amazon Dynamodb,因此,我有一个scanExpression,它根据传入的查询返回博客列表 例如,当我传入查询字符串“Java”时,它将返回Blog“startingoutwithjava” 但是,如果我传递查询字符串“Java教程”,则不会返回任何内容。所以基本上,我希望博客是包含“Java”或“Tutorial”字样的返回 公共列表搜索博客(字符串查询){ 试一试{ DynamoDBScanExpression scanExpression=新的DynamoDBScanExpression(); scanEx

因此,我有一个scanExpression,它根据传入的查询返回博客列表

例如,当我传入查询字符串“Java”时,它将返回Blog“startingoutwithjava”

但是,如果我传递查询字符串“Java教程”,则不会返回任何内容。所以基本上,我希望博客是包含“Java”或“Tutorial”字样的返回

公共列表搜索博客(字符串查询){
试一试{
DynamoDBScanExpression scanExpression=新的DynamoDBScanExpression();
scanExpression.addFilterCondition(“标题”,新条件()
.withComparisonOperator(ComparisonOperator.CONTAINS)
.WithAttributeValue列表(新的AttributeValue().With(query.toLowerCase()));
返回dynamoDBMapper.scan(BlogDetailsEntity.class,scanExpression);
}捕获(例外情况除外){
log.error(“获取博客失败>”+查询);
}
返回null;
}

要查询DynamoDB数据,您应该了解如何使用AWS SDK for Java V2 API和增强型客户端。您的代码是旧版本1,AmazonSDK团队推荐使用V2

AWS SDK for Java 2.x是对1.x版代码库的主要重写。它构建在Java8+之上,并添加了几个经常请求的特性。其中包括对非阻塞I/O的支持,以及在运行时插入不同HTTP实现的能力。

有关增强型客户端的更多信息,请参见:

使用增强型客户端,您可以使用表达式对象来获得所需的结果集。要查看使用V2和增强型客户端检索数据的不同方法,请参阅此代码示例

如果您不熟悉AWS Java V2 API,请参阅《Java V2开发人员指南》中的此文档主题:

使用dynamodb2api,您可以执行您的用例。给这张桌子

我可以使用以下Java代码在标题中的问题一词处调出记录:

AttributeValue attVal = AttributeValue.builder()
        .s("issue")
        .build();

Map<String, AttributeValue> myMap = new HashMap<>();
           myMap.put(":val1", attVal);

Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#title", "title");

Expression expression = Expression.builder()
         .expressionValues(myMap)
         .expressionNames(myExMap)
         .expression("contains(#title, :val1)")
         .build();

   ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder()
         .filterExpression(expression)
         .limit(15)
         .build();

   // Get items in the Issues table.
   Iterator<Issues> results = table.scan(enhancedRequest).items().iterator();
   while (results.hasNext()) {
            Issues issue = results.next();
            System.out.println("The record description is " + issue.getDescription());
            System.out.println("The record title is " + issue.getTitle());
     }
AttributeValue attVal=AttributeValue.builder()
.s.(“发行”)
.build();
Map myMap=newhashmap();
myMap.put(“:val1”,attVal);
Map myExMap=newhashmap();
myExMap.put(“#title”,“title”);
Expression=Expression.builder()
.expressionValues(myMap)
.expressionNames(myExMap)
.表达式(“包含(#title,:val1)”)
.build();
ScanEnhancedRequest enhancedRequest=ScanEnhancedRequest.builder()
.filterExpression(表达式)
.限额(15)
.build();
//获取问题表中的项目。
迭代器结果=table.scan(enhancedRequest).items().Iterator();
while(results.hasNext()){
问题=结果。下一步();
System.out.println(“记录描述为”+issue.getDescription());
System.out.println(“记录标题为“+issue.getTitle());
}
另外-我注意到您标记了弹簧靴。要了解如何编写使用DynamoDB API V2的Spring BOOT web应用程序,请参阅:


标题是否以小写形式存储?因为您正在将查询字符串转换为小写,而DynamoDB是区分大小写的……是的,它们是作为小写插入的。因此,如果查询是“Java”或“Java”,则会返回“从Java开始”博客。问题是查询只适用于精确的字符串匹配。
AttributeValue attVal = AttributeValue.builder()
        .s("issue")
        .build();

Map<String, AttributeValue> myMap = new HashMap<>();
           myMap.put(":val1", attVal);

Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#title", "title");

Expression expression = Expression.builder()
         .expressionValues(myMap)
         .expressionNames(myExMap)
         .expression("contains(#title, :val1)")
         .build();

   ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder()
         .filterExpression(expression)
         .limit(15)
         .build();

   // Get items in the Issues table.
   Iterator<Issues> results = table.scan(enhancedRequest).items().iterator();
   while (results.hasNext()) {
            Issues issue = results.next();
            System.out.println("The record description is " + issue.getDescription());
            System.out.println("The record title is " + issue.getTitle());
     }