Multiple Buckets and Spatial View Queries in Spring Data Couchbase – Spring Data Couchbase中的多桶和空间视图查询

最后修改: 2016年 6月 4日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this third tutorial on Spring Data Couchbase, we demonstrate the configuration required to support a Couchbase data model that spans multiple buckets, and we introduce the use of Spatial views for querying multi-dimensional data.

在这篇关于Spring Data Couchbase的第三篇教程中,我们演示了支持跨越多个桶的Couchbase数据模型所需的配置,并介绍了使用空间视图来查询多维数据。

2. Data Model

2.数据模型

In addition to the Person entity from our first tutorial and the Student entity from our second tutorial, we define a Campus entity for this tutorial:

除了我们第一个教程中的Person实体和我们第二个教程中的Student实体之外,我们为这个教程定义一个Campus实体。

@Document
public class Campus {
    @Id
    private String id;

    @Field
    @NotNull
    private String name;

    @Field
    @NotNull
    private Point location;

    // standard getters and setters
}

3. Java Configuration for Multiple Couchbase Buckets

3.多个Couchbase桶的Java配置

In order to use multiple buckets in your project, you will need to use version 2.0.0 or later of the Spring Data Couchbase module, and you will need to use a Java-based configuration, because the XML-based configuration supports only single-bucket scenarios.

为了在你的项目中使用多个桶,你需要使用2.0.0或更高版本的Spring Data Couchbase模块,而且你需要使用基于Java的配置,因为基于XML的配置只支持单桶的情况。

Here is the dependency that we include in our Maven pom.xml file:

以下是我们在Maven pom.xml文件中包含的依赖性。

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-couchbase</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

3.1. Defining the Bucket Bean

3.1.定义Bucket Bean

In our Introduction to Spring Data Couchbase tutorial, we designated “baeldung” as the name of our default Couchbase bucket for use with Spring Data.

在我们的Spring Data Couchbase入门教程中,我们指定“baeldung”作为默认的Couchbase桶的名称,用于Spring Data。

We will store Campus entities in the “baeldung2” bucket.

我们将把Campus实体存储在“baeldung2”桶中。

To make use of a second bucket, we first must define a @Bean for the Bucket itself in our Couchbase configuration class:

为了利用第二个桶,我们首先必须在Couchbase配置类中为@Bean本身定义一个Bucket

@Bean
public Bucket campusBucket() throws Exception {
    return couchbaseCluster().openBucket("baeldung2", "");
}

3.2. Configuring the Template Bean

3.2.配置模板Bean

Next, we define a @Bean for the CouchbaseTemplate to be used with this bucket:

接下来,我们为CouchbaseTemplate定义一个@Bean,以便与这个桶一起使用。

@Bean
public CouchbaseTemplate campusTemplate() throws Exception {
    CouchbaseTemplate template = new CouchbaseTemplate(
      couchbaseClusterInfo(), campusBucket(),
      mappingCouchbaseConverter(), translationService());
    template.setDefaultConsistency(getDefaultConsistency());
    return template;
}

3.3. Mapping the Repositories

3.3.储存库的映射

Finally, we define a custom mapping of Couchbase repository operations so that the Campus entity class will use the new template and bucket, while other entity classes will continue to use the default template and bucket:

最后,我们定义了Couchbase仓库操作的自定义映射,这样Campus实体类将使用新的模板和桶,而其他实体类将继续使用默认模板和桶。

@Override
public void configureRepositoryOperationsMapping(
  RepositoryOperationsMapping baseMapping) {
    try {
        baseMapping.mapEntity(Campus.class, campusTemplate());
    } catch (Exception e) {
        //custom Exception handling
    }
}

4. Querying Spatial or Multi-Dimensional Data

4.查询空间或多维数据

Couchbase provides native support for running bounding box queries against two-dimensional data, such as geographical data, using a special type of view known as a Spatial view.

Couchbase提供了对二维数据(如地理数据)运行边界框查询的本地支持,使用一种被称为Spatial view的特殊类型的视图。

A bounding box query is a range query that uses the southwestern-most [x,y] point of the box as its startRange parameter and the northwestern-most [x,y] point as its endRange parameter.

边界盒查询是一个范围查询,它使用盒子的最西南[x,y]点作为其startRange参数,最西北[x,y]点作为其endRange参数。

Spring Data extends Couchbase’s native bounding box query feature to queries involving circles and polygons using an algorithm that seeks to eliminate false positive matches, and it also provides support for queries involving more than two dimensions.

Spring Data将Couchbase的原生边界框查询功能扩展到了涉及圆形和多边形的查询,使用的算法旨在消除错误的正面匹配,它还为涉及两个以上维度的查询提供了支持。

Spring Data simplifies the creation of multi-dimensional queries through a set of keywords that can be used to define derived queries in Couchbase repositories.

Spring Data通过一组关键字简化了多维查询的创建,这些关键字可以用来定义Couchbase资源库中的衍生查询。

4.1. Supported Data Types

4.1.支持的数据类型

Spring Data Couchbase repository queries support data types from the org.springframework.data.geo package, including Point, Box, Circle, Polygon, and Distance.

Spring Data Couchbase资源库的查询支持来自org.springframework.data.geo包的数据类型,包括Point, Box, Circle, Polygon, Distance

4.2. Derived Query Keywords

4.2.衍生的查询关键词

In addition to the standard Spring Data repository keywords, Couchbase repositories support the following keywords in derived queries involving two dimensions:

除了标准的Spring Data存储库关键字外,Couchbase存储库在涉及两个维度的衍生查询中支持以下关键字。

  • Within, InWithin (takes two Point parameters defining a bounding box)
  • Near, IsNear (takes a Point and Distance as parameters)

And the following keywords may be used for queries involving more than two dimensions:

而以下关键词可用于涉及两个以上维度的查询。

  • Between (for adding a single numerical value to both the startRange and endRange)
  • GreaterThan, GreaterThanEqual, After (for adding a single numerical value to the startRange)
  • LessThan, LessThanEqual, Before (for adding a single numerical value to the endRange)

Here are some examples of derived query methods using these keywords:

下面是一些使用这些关键词的衍生查询方法的例子。

  • findByLocationNear
  • findByLocationWithin
  • findByLocationNearAndPopulationGreaterThan
  • findByLocationWithinAndAreaLessThan
  • findByLocationNearAndTuitionBetween

5. Defining the Repository

5.定义存储库

Repository methods backed by Spatial views must be decorated with the @Dimensional annotation, which specifies the design document name, view name, and number of dimensions used to define the view’s key (default 2 if not otherwise specified).

由空间视图支持的存储库方法必须用@Dimensional注解进行装饰,该注解指定了设计文档名称、视图名称和用于定义视图键的维数(如果没有另外指定,默认为2)。

5.1. The CampusRespository Interface

5.1.CampusRespository接口

Here in our CampusRepository interface, we declare two methods — one that uses traditional Spring Data keywords, backed by a MapReduce view, and one that uses dimensional Spring Data keywords, backed by a Spatial view:

在我们的CampusRepository接口中,我们声明了两个方法–一个使用传统的Spring Data关键字,由MapReduce视图支持,另一个使用Spring Data关键字,由空间视图支持。

public interface CampusRepository extends CrudRepository<Campus, String> {

    @View(designDocument="campus", viewName="byName")
    Set<Campus> findByName(String name);

    @Dimensional(dimensions=2, designDocument="campus_spatial",
      spatialViewName="byLocation")
    Set<Campus> findByLocationNear(Point point, Distance distance);
}

5.2. Spatial Views

5.2.空间视图

Spatial views are written as JavaScript functions, much like MapReduce views. Unlike MapReduce views, which consist of both a map function and a reduce function, Spatial views consist of only a spatial function and may not coexist in the same Couchbase design document as MapReduce views.

空间视图被写成JavaScript函数,与MapReduce视图很相似。与MapReduce视图不同,MapReduce视图由map函数和reduce函数组成,而空间视图只由spatial函数组成,并且不能与MapReduce视图共存于同一个Couchbase设计文档。

For our Campus entities, we will create a design document named “campus_spatial” containing a Spatial view named “byLocation” with the following function:

对于我们的校园实体,我们将创建一个名为“campus_spatial”的设计文档,其中包含一个名为“byLocation”的空间视图,其功能如下。

function (doc) {
  if (doc.location &&
      doc._class == "com.baeldung.spring.data.couchbase.model.Campus") {
    emit([doc.location.x, doc.location.y], null);
  }
}

As this example demonstrates, when you write a Spatial view function, the key used in the emit function call must be an array of two or more values.

正如这个例子所演示的,当你写一个空间视图函数时,在emit函数调用中使用的键必须是一个由两个或多个值组成的数组。

5.3. MapReduce Views

5.3.MapReduce视图

To provide full support for our repository, we must create a design document named “campus” containing two MapReduce views: “all” and “byName”.

为了给我们的资源库提供全面支持,我们必须创建一个名为“campus”的设计文档,其中包含两个MapReduce视图。“all”/em>和“byName”/em>。

Here is the map function for the “all” view:

这里是“所有”视图的地图功能。

function (doc, meta) {
  if(doc._class == "com.baeldung.spring.data.couchbase.model.Campus") {    
    emit(meta.id, null);
  }
}

And here is the map function for the “byName” view:

而这里是“byName”视图的地图功能。

function (doc, meta) {
  if(doc._class == "com.baeldung.spring.data.couchbase.model.Campus" &&
     doc.name) {    
    emit(doc.name, null);
  }
}

6. Conclusion

6.结论

We showed how to configure your Spring Data Couchbase project to support the use of multiple buckets, and we demonstrated how to use the repository abstraction to write spatial view queries against multi-dimensional data.

我们展示了如何配置你的Spring Data Couchbase项目以支持多桶的使用,我们还演示了如何使用存储库的抽象来编写针对多维数据的空间视图查询。

You can view the complete source code for this tutorial in the github project.

你可以在github项目中查看本教程的完整源代码。

To learn more about Spring Data Couchbase, visit the official Spring Data Couchbase project site.

要了解有关Spring Data Couchbase的更多信息,请访问官方的Spring Data Couchbase>项目网站。