Object mappers in Cassandra drivers
The C#, Java, Node.js, and Python drivers each provide an object mapper. These mappers are tools for generating, executing, and consuming the results of queries.
Mappers are available through mapper APIs in the drivers.
Model classes and data objects
The following concepts are common across all mapper APIs for Cassandra drivers:
-
Model class: A class that represents an Apache Cassandra, DSE, HCD, or Astra DB table. These classes have member variables that map to columns in that table.
-
Data object: An instance of a model class.
Use driver object mappers
By design, the mapper APIs don’t implement all CQL features. Additionally, they differ for each language because each language requires a different set of patterns. To verify whether the mapper APIs support your application, see the documentation for your driver and its mapper API.
C/C++ driver
The C/C++ driver doesn’t provide an object mapper.
C# driver object mapper and LINQ components
In the C# driver object mapper, model classes are normal classes with setters and getters, also known as plain old CLR objects (POCOs).
By default, the mapper automatically discovers the mapping from object member to table column name.
You can also use the Define
method to explicitly configure the mapping.
Because model classes are POCOs, all queries must be mediated by the mapper object.
Mappers wrap Session
instances and provide methods, such as First
and Fetch
, that execute statements and define the types into which the mapper reads data.
Similarly, mappers provide methods, such as Insert
and Update
, that take in a data object and use it to generate a write statement.
All of these methods are generic, and they can take a parameterized query and arguments or a data object.
By default, writing null values deletes or unsets the corresponding value in the database.
Writing null values creates tombstones that can impact query performance and the overall health of the database.
Writing nulls can be controlled on a per-operation basis by setting insertNulls
when using the https://docs.datastax.com/en/drivers/csharp/latest/api/Cassandra.Mapping.Mapper.html#Cassandra_Mapping_Mapper_Insert10_System_Boolean_Cassandra_Mapping_CqlQueryOptions[Insert
] or https://docs.datastax.com/en/drivers/csharp/latest/api/Cassandra.Mapping.Mapper.html#Cassandra_Mapping_Mapper_InsertAsync10_Cassandra_Mapping_CqlQueryOptions[InsertAsync
] methods.
The C# driver also includes a C# LINQ API with support for mapping.
Both the mapper and LINQ components support batch statements, as explained in Batch statements with the mapper and Batch statements with LINQ.
GoCQL driver
The GoCQL driver doesn’t provide an object mapper.
Java driver 4.x series object mapper
The Java driver 4.x series mapper relies on annotations to configure mapped entities and queries that use compile-time annotation processing. For information about the annotation processor, see the Mapper integration documentation.
Model classes are created by annotating classes with @Entity
.
The mapper automatically discovers the object member to table column name mapping using the default NamingConvention
.
The default mapping can be changed by specifying a different naming convention with the @NamingStrategy
annotation, or by providing your own implementation of NameConverter
, but not both.
In addition, you can override entity and column name mappings individually with the @CqlName
annotation.
Once your Entities
are designed, you must create Data Access Object (DAO) interfaces annotated with @Dao
.
A DAO defines all needed query methods, each marked with the appropriate query annotation for performing your CRUD operations.
Executing queries requires an instance of a DAO, which you can get from the Mapper interface.
For each DAO, your mapper interface should define a DaoFactory
method.
An instance of a mapper can be obtained from the auto-generated Mapper Builder.
Build an instance of Mapper
from the mapper builder, invoke the desired DaoFactory
method to get an instance of a DAO, and then, from the DAO, invoke your desired query method to execute the query.
By default, writing null values doesn’t delete or unset the corresponding value in the database.
Writing null values creates tombstones that can impact query performance and the overall health of the database.
You can change this behavior by specifying a different NullSavingStrategy
in the @DefaultNullSavingStrategy
annotation at the DAO level, query method level, or both if you want a mix of null saving strategies in the same DAO.
The preceding links are for Java driver versions 4.17 and earlier. For documentation for versions 4.18 and later, see the 4.x tree in the Apache Cassandra Java driver GitHub repository.
Java driver 3.x series object mapper (maintenance)
The 3.x series of the Java driver is in maintenance mode. For more information, see Java driver for Apache Cassandra®. When upgrading to the 4.x series, object mapper functionality is significantly different:
For more information, see the preceding section on the Java driver 4.x series object mapper. |
The Java driver 3.x series object mapper relies on annotated classes that are processed at runtime using reflection. Model classes are created by annotating classes with @Table. By default, the mapper automatically discovers the mapping from object member to table column name. You can also explicitly configure the mapping using the @Column annotation or use custom codecs.
To execute queries with the Java 3.x series mapper, create a data object that defines the query and pass it using CRUD operations on the mapper. For read queries, the data object arguments are used as filters on the table columns. If custom queries are needed, the mapper extends functionality through Accessors. You can also use manual mapping to map regular ResultSets to data objects. For write requests, the data object values are used as values in the insert query.
By default, writing null values deletes or unsets the corresponding value in the database.
Writing null values creates tombstones that can impact query performance and the overall health of the database.
Writing nulls can be controlled by default or on a per-operation basis by setting saveNullFields
in the mapper options.
The preceding links are for Java driver versions 3.11 and earlier. For documentation for later versions of the Java driver 3.x series, see the 3.x tree in the Apache Cassandra Java driver GitHub repository.
Node.js driver object mapper
See the following:
PHP driver
The DataStax PHP driver doesn’t provide an object mapper.
Python driver object mapper (cqlengine)
For the Python driver object mapper, model classes are created by sub-classing cassandra.cqlengine.model.Model
.
By default, the mapper automatically discovers the mapping of object attributes created from cassandra.cqlengine.columns.Column
to table column names.
You can also explicitly define this mapping using the db_field kwarg to Column subclass initializers.
It is safest to create the tables for model objects outside the scope of the mapper, though the Python driver does allow for making schema changes with the object mapper. DataStax doesn’t recommend creating tables within the mapper because this can result in concurrent schema modifications.
The Python driver mapper provides class methods for reading and writing data objects. In addition, queries can be executed by directly calling methods on the model class. The mapper’s read query methods return collections of data objects that have instance methods for CRUD operations.
Passing None corresponds to a DELETE operation on the value in the corresponding row. This creates tombstones that can impact query performance and the overall health of the database.
Mapper connections are maintained in a connection registry that can be used to access the sessions that connect to the database.
Ruby driver
The DataStax Ruby driver doesn’t provide an object mapper.