Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

JNoSQL/Artemis/Cassandra

Cassandra Extension

The goal of an extension goes beyond the Artemis API-core, and with Cassandra Extensions it does not look different. Cassandra Extension has classes such as CassandraColumnRepository and CassandraCrudRepositoryAsync whose have support to Cassandra particular behavior as CQL and write and read with consistency level.


Maven Dependency

To use the Cassandra extension to Artemis:

 
  <dependency>
    <groupId>org.jnosql.artemis</groupId>
    <artifactId>cassandra-extension</artifactId>
    <version>version</version>
  </dependency>

E.g:

 
  <dependency>
    <groupId>org.jnosql.artemis</groupId>
    <artifactId>cassandra-extension</artifactId>
    <version>0.0.1</version>
  </dependency>

Cassandra Template

CassandraTemplateAsync and CassandraTemplate are specialized classes from ColumnTemplate and ColumnTemplateAsync respectively, so they have all methods plus the Cassandra methods whose has in Cassandra driver such as Cassandra query language, consistency level and also UDT. To active, this resource just needs to produce a CassandraColumnFamilyManager in a CDI context.

  @Produces
    public CassandraColumnFamilyManager getManager() {
	//code
        return managerFactory.get(KEY_SPACE);
    }


Done, the Artemis will take and manage the bean to you.


@Inject
private CassandraTemplate template;

public void sample() {
            Person saved = template.save(PERSON, ConsistencyLevel.ONE);
            System.out.println("Person saved" + saved);

            ColumnQuery query = ColumnQuery.of("Person");
            query.and(ColumnCondition.eq(Column.of("id", 1L)));

            List<Person> people = template.cql("select * from developers.Person where id = 1");
            System.out.println("Entity found: " + people);
}


The similar thing happens with the CassandraTemplateAsync just produces the CassandraColumnFamilyManagerAsync.

  @Produces
    public CassandraColumnFamilyManagerAsync getManagerAsync() {
	//code
        return managerFactory.get(KEY_SPACE);
    }


Converters

To save data Cassandra Extension has two converters:

  • LocalDateConverter to save the time Cassandra type.
  • TimestampConverter to keep the timestamp Cassandra type.


@Entity
public class History {


    @Column
    private String name;

    @Column
    @Convert(value = LocalDateConverter.class)
    private LocalDate localDate;

    @Column
    @Convert(value = TimestampConverter.class)
    private LocalDateTime localDateTime;

    //getter and setter
}


Cassandra repository

Beyond the column repository, Cassandra Extension also has the Repository specialization to both sync and async works. These specializations allow querying with consistency level and also it has the CQL annotation that can use Cassandra query language.


Sample Code
    public interface PersonRepository extends CassandraRepository<Person> {

        Person findByName(String name, ConsistencyLevel level);

        void deleteByName(String name, ConsistencyLevel level);

        @CQL("select * from Person")
        List<Person> findAll();

        @CQL("select * from Person where name = ?")
        List<Person> findByName(String name);

        @CQL("select * from Person where age = :age")
        List<Person> findByAge(@Param("age") Integer age);
    }


    public interface PersonRepository extends CassandraRepository<Person> {

        Person findByName(String name, ConsistencyLevel level);

        void deleteByName(String name, ConsistencyLevel level);

        @CQL("select * from Person")
        List<Person> findAll();

        @CQL("select * from Person where name = ?")
        List<Person> findByName(String name);
    }


   public interface PersonAsyncRepository extends CassandraRepositoryAsync<Person> {

        Person findByName(String name);

        Person findByName(String name, ConsistencyLevel level, Consumer<List<Person>> callBack);

        void deleteByName(String name, ConsistencyLevel level, Consumer<Void> callBack);

        @CQL("select * from Person where name= ?")
        void queryName(String name, Consumer<List<Person>> callBack);
    }


UDT

Artemis also supports user defined type, the UDT, The Cassandra Extension has the @UDT annotation that "teach" to Artemis to convert to UDT column, from Cassandra Driver.


@Entity
public class Movie {

    @Column
    private String name;

    @Column
    private Integer age;

    @Column
    @UDT("director")
    private Director director;

    //getter and setter

}


Links

Back to the top