Cassandra Support
-
基于 Java 的配置和 XML 命名空间
-
用于提高常见 Cassandra 数据访问操作效率的 helper 类
-
将 CQL 表和 POJO 进行对象映射的 Helper 类
-
将异常转换为 Spring 的数据访问异常层次结构
-
与 Spring 转换服务集成的功能丰富的对象映射
-
可扩展以支持其他元数据格式的基于注解的映射元数据
-
基于 Java 的查询、条件和更新 DSL
-
对命令式和响应式 Repository 接口的自动实现,包括对自定义查询方法的支持
Spring Data for Apache Cassandra 允许在 CQL 和实体级别进行交互,抽象并处理 Cassandra 和 CQL 的低级别细节。它提供了多种方法来访问 Cassandra 数据库,包括 CqlTemplate、CassandraTemplate 和 Repository Abstraction。
参考文档的这一部分解释了 Spring Data for Apache Cassandra 提供的核心功能。Spring Data 对 Apache Cassandra 的支持包含广泛的功能:
This part of the reference documentation explains the core functionality offered by Spring Data for Apache Cassandra. Spring Data support for Apache Cassandra contains a wide range of features:
-
Spring configuration support with Java-based
@Configuration
classes or the XML namespace. -
The
CqlTemplate
,AsyncCqlTemplate
, andReactiveCqlTemplate
helper classes that increases productivity by properly handling common Cassandra data access operations. -
The
CassandraTemplate
,AsyncCassandraTemplate
, andReactiveCassandraTemplate
helper classes that provide object mapping between CQL Tables and POJOs. -
Exception translation into Spring’s portable Data Access Exception Hierarchy.
-
Feature rich object mapping integrated with Spring’s Conversion Service.
-
Annotation-based mapping metadata that is extensible to support other metadata formats.
-
Java-based query, criteria, and update DSLs.
-
Automatic implementation of imperative and reactive
Repository
interfaces including support for custom query methods.
Abstractions
Spring Data for Apache Cassandra 允许在 CQL 和实体级别进行交互。
Spring Data for Apache Cassandra allows interaction on both the CQL and the entity level.
Spring Data for Apache Cassandra 抽象提供的值也许最能通过下表中概述的动作序列体现。该表显示了 Spring 负责哪些动作,而哪些动作是你,应用程序开发人员的责任。
The value provided by the Spring Data for Apache Cassandra abstraction is perhaps best shown by the sequence of actions outlined in the table below. The table shows which actions Spring take care of and which actions are the responsibility of you, the application developer.
Action | Spring | You |
---|---|---|
Define connection parameters. |
X |
|
Open the connection. |
X |
|
Specify the CQL statement. |
X |
|
Declare parameters and provide parameter values |
X |
|
Prepare and run the statement. |
X |
|
Set up the loop to iterate through the results (if any). |
X |
|
Do the work for each iteration. |
X |
|
Process any exception. |
X |
|
Close the Session. |
X |
核心 CQL 支持负责处理可能让 Cassandra 和 CQL 成为此类繁琐 API 的所有低级别详细信息,并且无法与之开发。使用映射实体对象允许生成架构、对象映射和存储库支持。
The core CQL support takes care of all the low-level details that can make Cassandra and CQL such a tedious API with which to develop. Using mapped entity objects allows schema generation, object mapping, and repository support.
Choosing an Approach for Cassandra Database Access
作为 Cassandra 数据库访问的基础,你可以选择几种方法。Spring 对 Apache Cassandra 的支持有不同的版本。在开始使用这些方法之一后,你仍然可以混合匹配,以包含来自不同方法的功能。下列方法效果不错:
You can choose among several approaches to use as a basis for your Cassandra database access. Spring’s support for Apache Cassandra comes in different flavors. Once you start using one of these approaches, you can still mix and match to include a feature from a different approach. The following approaches work well:
-
CqlTemplate
andReactiveCqlTemplate
are the classic Spring CQL approach and the most popular. This is the “lowest-level” approach. Note that components likeCassandraTemplate
useCqlTemplate
under-the-hood. -
CassandraTemplate
wraps aCqlTemplate
to provide query result-to-object mapping and the use ofSELECT
,INSERT
,UPDATE
, andDELETE
methods instead of writing CQL statements. This approach provides better documentation and ease of use. -
ReactiveCassandraTemplate
wraps aReactiveCqlTemplate
to provide query result-to-object mapping and the use ofSELECT
,INSERT
,UPDATE
, andDELETE
methods instead of writing CQL statements. This approach provides better documentation and ease of use. -
Repository Abstraction lets you create repository declarations in your data access layer. The goal of Spring Data’s repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.
对于大多数面向数据任务,你可以使用 [Reactive|Async]CassandraTemplate`或 `Repository`支持,它们都使用丰富的对象映射功能。
[Reactive|Async]CqlTemplate`通常用于增加计数器或执行临时 CRUD 操作。[Reactive|Async]CqlTemplate`还提供回调方法,可以轻松获取底层 API 对象,例如 `com.datastax.oss.driver.api.core.CqlSession
,它可让你直接与 Cassandra 通信。适用于 Apache Cassandra 的 Spring Data 在各个 API 中的对象上使用一致的命名约定,以便与 DataStax Java 驱动程序中的命名约定相对应,以便它们熟悉且你可以在 Spring API 上映射现有知识。
For most data-oriented tasks, you can use the [Reactive|Async]CassandraTemplate
or the Repository
support, both of which use the rich object-mapping functionality. [Reactive|Async]CqlTemplate
is commonly used to increment counters or perform ad-hoc CRUD operations. [Reactive|Async]CqlTemplate
also provides callback methods that make it easy to get low-level API objects, such as com.datastax.oss.driver.api.core.CqlSession
, which lets you communicate directly with Cassandra.
Spring Data for Apache Cassandra uses consistent naming conventions on objects in various APIs to those found in the DataStax Java Driver so that they are familiar and so that you can map your existing knowledge onto the Spring APIs.