Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Defined Type with spring-data-cassandra

I am looking to create models as below, how can I use user defined types in spring-data-cassandra?

{
  email: "[email protected]",
  name: {
  fname: "First",
  lname: "Last"
  }
}
like image 691
skptl Avatar asked Aug 10 '16 00:08

skptl


People also ask

What is user defined type in Cassandra?

User-Defined Types (UDTs) can be used to attach multiple data fields to a column. User-defined types (UDTs) can attach multiple data fields, each named and typed, to a single column. The fields used to create a UDT may be any valid data type, including collections and other existing UDTs.

Can we use JPA with Cassandra?

Use JPA libraries to communicate with Apache Cassandra comparing Achilles, Datastax and Kundera. The last one presents the better processing speeds with lower computational resources consumption. Source code is available on Github with detailed documentation on how to build and run the tests using Docker.

What is Spring data Cassandra?

GitHub - spring-projects/spring-data-cassandra: Provides support to increase developer productivity in Java when using Apache Cassandra. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.


2 Answers

User Defined data type is now supported by Spring Data Cassandra. The latest release 1.5.0.RELEASE uses Cassandra Data stax driver 3.1.3 and hence its working now. Follow the below steps to make it working

How to use UserDefinedType(UDT) feature with Spring Data Cassandra :

  1. We need to use the latest jar of Spring data Cassandra (1.5.0.RELEASE)

    group: 'org.springframework.data', name: 'spring-data-cassandra', version: '1.5.0.RELEASE'

  2. Make sure it uses below versions of the jar :

    datastax.cassandra.driver.version=3.1.3 spring.data.cassandra.version=1.5.0.RELEASE spring.data.commons.version=1.13.0.RELEASE spring.cql.version=1.5.0.RELEASE

  3. Create user defined type in Cassandra : The type name should be same as defined in the POJO class

Address data type

CREATE TYPE address_type ( 
    id text, 
    address_type text, 
    first_name text, 
    phone text 
);
  1. Create column-family with one of the columns as UDT in Cassandra:

Employee table:

CREATE TABLE employee( 
   employee_id uuid, 
   employee_name text, 
   address frozen, 
   primary key (employee_id, employee_name) 
);
  1. In the domain class, define the field with annotation -CassandraType and DataType should be UDT:

    @Table("employee") 
    public class Employee { 
       -- othere fields-- 
       @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type") 
       private Address address; 
    }
    
  2. Create domain class for the user defined type : We need to make sure that column name in the user defined type schema has to be same as field name in the domain class.

    @UserDefinedType("address_type") 
    public class Address { 
        @CassandraType(type = DataType.Name.TEXT) 
        private String id; 
        @CassandraType(type = DataType.Name.TEXT) 
        private String address_type; 
    }
    
  3. In the Cassandra Config, Change this :

    @Bean public CassandraMappingContext mappingContext() throws Exception { 
        BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext(); 
        mappingContext.setUserTypeResolver(new 
        SimpleUserTypeResolver(cluster().getObject(), cassandraKeyspace)); 
        return mappingContext; 
    }
    
  4. User defined type should have the same name across everywhere. for e.g

    @UserDefinedType("address_type")
    @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type")
    CREATE TYPE address_type
    
like image 155
denzal Avatar answered Sep 21 '22 06:09

denzal


Spring Data Cassandra does not currently support Cassandra Database UDTs or Tuple Types in the mapping infrastructure of SD Cassandra. However, we do plan to address both of these things in an upcoming release.

See and follow DATACASS-284 - Add support for TupleType/TupleValue and DATACASS-172 - How to handle CUSTOM, User Defined TYPEs for more details.

Note...

SD Cassandra was recently moved under the Pivotal umbrella of managed/maintained SD projects and both myself and @mp911de are the projects leads now.

We recently presented on SD Cassandra at SpringOne Platform 2016, where we outlined the roadmap (slide 11, 3rd bullet) for the project.

like image 32
John Blum Avatar answered Sep 23 '22 06:09

John Blum