DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java
  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Finally, an ORM That Matches Modern Architectural Patterns!

Trending

  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Why We Still Struggle With Manual Test Execution in 2025
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  1. DZone
  2. Data Engineering
  3. Databases
  4. Introducing ScyllaDB With Java

Introducing ScyllaDB With Java

This post gives you an overview of ScyllaDB and how to connect with Java using Jakarta EE.

By 
Otavio Santana user avatar
Otavio Santana
DZone Core CORE ·
Jul. 01, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
15.9K Views

Join the DZone community and get the full member experience.

Join For Free

From Wikipedia: "Scylla is an open-source distributed NoSQL data store. It was designed to be compatible with Apache Cassandra while achieving significantly higher throughputs and lower latencies. It supports the same protocols as Cassandra (CQL and Thrift) and the same file formats (SSTable), but is a completely rewritten implementation, using the C++17 language replacing Cassandra's Java, and the Seastar asynchronous programming library replacing threads, shared memory, mapped files, and other classic Linux programming techniques."

This post will test the ScyllaDB with Java and give you the first impression of this database, which is compatible with Apache Cassandra.

Installation

The first step in this tutorial is to install the Scylla database. To make this process easier, we will use Docker, which simplifies a lot of the installation process with just this command:

docker run -d --name scylladb-instance -p 9042:9042 scylladb/scylla

Infrastructure Code

In a Maven project, we need to set the dependency project. Eclipse JNoSQL has two layers: one for mapping and the other one for communication. Once ScyllaDB is compatible with Cassandra, this post will use the Cassandra driver as the dependency for communication. Furthermore, it important to set a CDI implementation.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>scylla</artifactId>
    <name>Artemis Demo using Java SE scylla</name>

    <parent>
        <groupId>org.jnosql.artemis</groupId>
        <artifactId>artemis-demo-java-se</artifactId>
        <version>0.0.9</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.jnosql.artemis</groupId>
            <artifactId>cassandra-extension</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>

Show Me the Code

For this sample, we have the Person entity with id, name, and phones as fields:

import org.jnosql.artemis.Column;
import org.jnosql.artemis.Entity;
import org.jnosql.artemis.Id;

import java.util.List;


@Entity("Person")
public class Person {

    @Id("id")
    private long id;

    @Column
    private String name;

    @Column
    private List<String> phones;

}

There is a Repository interface that implements the basic operations in the database. Also, it has the method query, which gives the method that Eclipse JNoSQL will implement to the Java developer:

import org.jnosql.artemis.Repository;

public interface PersonRepository extends Repository<Person, Long> {

}

The next step is to make a connection, an entity manager, available to CDI.

import org.jnosql.diana.cassandra.column.CassandraColumnFamilyManager;
import org.jnosql.diana.cassandra.column.CassandraColumnFamilyManagerFactory;
import org.jnosql.diana.cassandra.column.CassandraConfiguration;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

@ApplicationScoped
public class ScyllaProducer {

    private static final String KEY_SPACE = "developers";

    private CassandraConfiguration cassandraConfiguration;

    private CassandraColumnFamilyManagerFactory managerFactory;

    @PostConstruct
    public void init() {
        cassandraConfiguration = new CassandraConfiguration();
        managerFactory = cassandraConfiguration.get();
    }

    @Produces
    public CassandraColumnFamilyManager getManagerCassandra() {
        return managerFactory.get(KEY_SPACE);
    }

}

The ScyllaProducer class makes a manager entity class available and ready for the application use. Once the code does not define the Settings, it will use the default behavior, so it will read from the diana-cassandra.properties file. This file has the configuration startup, thus the port and host to connect. Scylla as Cassandra is not schemaless, which means we need to create the structure before we use it. The properties file has Cassandra Query Language to create the keyspace and the column family structure.

cassandra.host.1=localhost
cassandra.query.1=CREATE KEYSPACE IF NOT EXISTS developers WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
cassandra.query.2=CREATE COLUMNFAMILY IF NOT EXISTS developers.Person (id bigint PRIMARY KEY, name text, phones list<text>);

The whole configuration process and the code are ready, so let's run the code!


import org.jnosql.artemis.cassandra.column.CassandraTemplate;
import org.jnosql.artemis.column.ColumnTemplate;
import org.jnosql.diana.api.column.ColumnQuery;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.Optional;

import static org.jnosql.diana.api.column.query.ColumnQueryBuilder.select;

public class App {


    public static void main(String[] args) {

        try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
            Person person = Person.builder().withPhones(Arrays.asList("234", "432"))
                    .withName("Ada Lovelace").withId(1).build();
            ColumnTemplate template = container.select(CassandraTemplate.class).get();
            Person saved = template.insert(person);
            System.out.println("Person saved" + saved);

            ColumnQuery query = select().from("Person").where("id").eq(1L).build();

            Optional<Person> result = template.singleResult(query);
            System.out.println("Entity found: " + result);

        }
    }

    private App() {
    }
}


import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.Optional;

import static org.jnosql.artemis.DatabaseQualifier.ofColumn;

public class App2 {

    public static void main(String[] args) {

        try(SeContainer container = SeContainerInitializer.newInstance().initialize()) {
            Person person = Person.builder().withPhones(Arrays.asList("234", "432"))
                    .withName("Ada Lovelace").withId(1).build();
            PersonRepository repository = container.select(PersonRepository.class).select(ofColumn()).get();
            Person saved = repository.save(person);
            System.out.println("Person saved" + saved);

            Optional<Person> result = repository.findById(1L);
            System.out.println("Entity found: " + person);

        }
    }


    private App2() {}
}

We now have the ability to run a particular behavior in a NoSQL database matter! That's why the Eclipse JNoSQL worries about the extensibility of both Cassandra and ScyllaDB and worries about the support to native query the CQL and operation with consistency level. To support it and move resources, there is the CassandraTemplate, which is an extension of ColumnTemplate and allows features from Cassandra as a consequence on ScyllaDB.

import com.datastax.driver.core.ConsistencyLevel;
import org.jnosql.artemis.cassandra.column.CassandraTemplate;
import org.jnosql.diana.api.column.ColumnQuery;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.List;

import static org.jnosql.diana.api.column.query.ColumnQueryBuilder.select;

public class App3 {

    public static void main(String[] args) {

        try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
            Person person = Person.builder().withPhones(Arrays.asList("234", "432"))
                    .withName("Ada Lovelace").withId(1).build();
            CassandraTemplate cassandraTemplate = container.select(CassandraTemplate.class).get();
            Person saved = cassandraTemplate.save(person, ConsistencyLevel.ONE);
            System.out.println("Person saved" + saved);
            List<Person> people = cassandraTemplate.cql("select * from developers.Person where id = 1");
            System.out.println("Entity found: " + people);

        }
    }

    private App3() {
    }
}

This post gives you an overview of ScyllaDB and how to connect with Java using Jakarta EE. ScyllaDB has compatibility with Cassandra that allows us to use the same Cassandra API without issues. 

References

  • Code: https://github.com/JNOSQL/artemis-demo/tree/master/artemis-demo-java-se/scylla

  • Scylla: https://www.scylladb.com/

  • Eclipse JNoSQL: http://www.jnosql.org/

Database Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java
  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Finally, an ORM That Matches Modern Architectural Patterns!

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!