Was this page helpful?
In this tutorial you’ll build a Media Player to store your songs and build playlists.
Install the Java ScyllaDB Driver.
<dependency>
<groupId>com.scylladb</groupId>
<artifactId>java-driver-core</artifactId>
<version>3.11.2.0</version>
</dependency>
<dependency>
<groupId>com.scylladb</groupId>
<artifactId>java-driver-query-builder</artifactId>
<version>3.11.2.0</version>
</dependency>
<dependency>
<groupId>com.scylladb</groupId>
<artifactId>java-driver-mapper-runtime</artifactId>
<version>3.11.2.0</version>
</dependency>
Get your database credentials from your ScyllaDB Cloud Cluster View in the tab Connect
.
Add your machine’s IP Address to the list of allowed IP addresses in ScyllaDB Cloud. Otherwise, your connection will get refused.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints("your-node-url.scylla.cloud", "your-node-url.clusters.scylla.cloud", "your-node-url.clusters.scylla.cloud")
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
}
}
With the Java driver, you can use the function inside your cluster connection called execute(query)
and build the query you want to execute inside your database/keyspace.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
ResultSet result = session.execute("SELECT * FROM system.clients LIMIT 10");
System.out.println(result);
}
}
A keyspace in ScyllaDB is a collection of tables with attributes which define how data is replicated on nodes.
You don’t need a keyspace on your connection boot, but you’ll need it to create a table.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import static com.datastax.driver.core.schemabuilder.SchemaBuilder.createKeyspace;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
String createKeyspaceQuery = createKeyspace("media_player")
.ifNotExists()
.with()
.replication(ImmutableMap.of("class", "NetworkTopologyStrategy", "replication_factor", 3))
.durableWrites(true)
.getQueryString();
session.execute(createKeyspaceQuery)
}
}
After that you probably will need to re-create your connection poiting which keyspace
you want to use.
A table stores part or all of your app data (depending on how you structure your database schema).
Add the keyspace
as a parameter in the connection object and define a CQL string that creates a table to store your favorite songs.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import static com.datastax.driver.core.schemabuilder.SchemaBuilder.createTable;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
String createTableQuery = createTable("media_player", "songs")
.ifNotExists()
.addPartitionKey("id", DataType.bigint())
.addClusteringColumn("updated_at", DataType.timestamp())
.addColumn("title", DataType.text())
.addColumn("album", DataType.text())
.addColumn("artist", DataType.text())
.addColumn("created_at", DataType.timestamp())
.getQueryString()
session.execute(createTableQuery)
}
}
Now that you have created a keyspace and a table, insert some songs to populate the table.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import java.util.Date;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
PreparedStatement statement = session.prepare(
"INSERT INTO media_player.songs (id, title, artist, album, created_at, updated_at) VALUES (?,?,?,?,?,?)"
);
BoundStatement bound = statement.bind()
.setLong(0, 1)
.setString(1, "Stairway to Heaven")
.setString(2, "Led Zeppelin")
.setString(3, "Led Zeppelin IV")
.setTimestamp(4, new Date())
.setTimestamp(5, new Date());
session.execute(bound);
}
}
Let’s read the songs from the database and print them to the terminal.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import java.util.Date;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
ResultSet songsResult = session.execute("SELECT * FROM media_player.songs PER PARTITION LIMIT 1");
List<Row> songList = results.all();
for (Row row : resultsmemo) {
System.out.println(
String.format(
"ID: %d -> Song: %s -> Artist: %s -> Album: %s -> Created At: %s",
row.getInt("id"),
row.getString("title"),
row.getString("artist"),
row.getString("album"),
row.getTimestamp("created_at").toString()
)
);
}
}
}
The UPDATE
query takes two fields in the WHERE
clause (partition Key
and clustering key
). See the snippet below:
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import java.util.Date;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
PreparedStatement statement = session.prepare(
"UPDATE songs set title = ? where id = ? AND updated_at = ?"
);
BoundStatement bound = statement.bind()
.setLong(0, 1)
.setString(1, "Rock and Roll")
.setTimestamp(2, new Date());
session.execute(bound);
}
}
After the data gets inserted, query all columns and filter by the ID:
scylla@cqlsh:media_player> select * from songs where id = 1;
id | updated_at | album | artist | created_at | title
---+---------------------------------+-------------+--------+---------------------------------+----------------------------
1 | 2023-03-02 22:00:00.000000+0000 | Smithereens | Joji | 2023-03-02 22:00:00.000000+0000 | Glimpse of Us
1 | 2023-03-02 23:10:00.000000+0000 | null | null | null | Glimpse of US - Inutilismo
It only updated the field title
and updated_at
(the clustering key), and since we didn’t input the rest of the data, it will not be replicated as expected.
Last things last! Let’s understand what we can DELETE with this statement. There’s the regular DELETE
statement that focuses on ROWS
and the other one that deletes data only from COLUMNS
. The syntax is very similar.
// Deletes a single row
DELETE FROM songs WHERE id = d754f8d5-e037-4898-af75-44587b9cc424;
// Deletes a cell
DELETE artist FROM songs WHERE id = d754f8d5-e037-4898-af75-44587b9cc424;
If you want to remove a specific column, you also should pass as parameter the Clustering Key
and be very specific in which register you want to delete something.
On the other hand, the “normal delete” just need the Partition Key
to handle it. Just remember: if you use the statement “DELETE FROM table” it will delete ALL the rows that you stored with that ID.
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy;
import java.util.Date;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Cluster cluster = Cluster.builder()
.addContactPoints(
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud",
"your-node-url.7207ca5a8fdc45f2b03f.clusters.scylla.cloud"
)
.withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc("AWS_US_EAST_1").build()) // your local data center
.withAuthProvider(new PlainTextAuthProvider("scylla", "your-awesome-password"))
.build();
Session session = cluster.connect();
PreparedStatement statement = session.prepare(
"DELETE FROM songs where id = ?"
);
BoundStatement bound = statement.bind()
.setLong(0, 1);
session.execute(bound);
}
}
Yay! You now know how get started with ScyllaDB in Java.
There is a simple project with this structure that you can check out here.
If you think something can be improved, please open an issue and let’s make it happen!
Did you like the content? Don’t forget to star the repo and follow us on socials.
Was this page helpful?