BAEL-3043: Datastax Java Driver for Apache Cassandra (#7290)
This commit is contained in:
parent
02c8af2044
commit
c06471c727
|
@ -4,7 +4,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-cassandra</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>java-cassandra</name>
|
||||
<name>java-cassandra</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -29,6 +29,19 @@
|
|||
<version>${cassandra-unit.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- DataStax Cassandra -->
|
||||
<dependency>
|
||||
<groupId>com.datastax.oss</groupId>
|
||||
<artifactId>java-driver-core</artifactId>
|
||||
<version>${datastax-cassandra.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-handler</artifactId>
|
||||
<version>${io-netty.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -39,6 +52,8 @@
|
|||
<!-- Cassandra -->
|
||||
<cassandra-driver-core.version>3.1.2</cassandra-driver-core.version>
|
||||
<cassandra-unit.version>3.1.1.0</cassandra-unit.version>
|
||||
<datastax-cassandra.version>4.1.0</datastax-cassandra.version>
|
||||
<io-netty.version>4.1.34.Final</io-netty.version>
|
||||
<guava.version>18.0</guava.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.datastax.cassandra;
|
||||
|
||||
import com.baeldung.datastax.cassandra.domain.Video;
|
||||
import com.baeldung.datastax.cassandra.repository.KeyspaceRepository;
|
||||
import com.baeldung.datastax.cassandra.repository.VideoRepository;
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
public class Application {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Application().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
CassandraConnector connector = new CassandraConnector();
|
||||
connector.connect("127.0.0.1", 9042, "datacenter1");
|
||||
CqlSession session = connector.getSession();
|
||||
|
||||
KeyspaceRepository keyspaceRepository = new KeyspaceRepository(session);
|
||||
|
||||
keyspaceRepository.createKeyspace("testKeyspace", "SimpleStrategy", 1);
|
||||
keyspaceRepository.useKeyspace("testKeyspace");
|
||||
|
||||
VideoRepository videoRepository = new VideoRepository(session);
|
||||
|
||||
videoRepository.createTable();
|
||||
|
||||
videoRepository.insertVideo(new Video("Video Title 1", Instant.now()));
|
||||
videoRepository.insertVideo(new Video("Video Title 2",
|
||||
Instant.now().minus(1, ChronoUnit.DAYS)));
|
||||
|
||||
List<Video> videos = videoRepository.selectAll();
|
||||
|
||||
videos.forEach(x -> LOG.info(x.toString()));
|
||||
|
||||
connector.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.datastax.cassandra;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class CassandraConnector {
|
||||
|
||||
private CqlSession session;
|
||||
|
||||
public void connect(final String node, final Integer port, final String dataCenter) {
|
||||
CqlSessionBuilder builder = CqlSession.builder();
|
||||
builder.addContactPoint(new InetSocketAddress(node, port));
|
||||
builder.withLocalDatacenter(dataCenter);
|
||||
|
||||
session = builder.build();
|
||||
}
|
||||
|
||||
public CqlSession getSession() {
|
||||
return this.session;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.datastax.cassandra.domain;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Video {
|
||||
|
||||
private UUID id;
|
||||
private String title;
|
||||
private Instant creationDate;
|
||||
|
||||
public Video(UUID id, String title, Instant creationDate) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.creationDate = creationDate;
|
||||
}
|
||||
|
||||
public Video(String title, Instant creationDate) {
|
||||
this.title = title;
|
||||
this.creationDate = creationDate;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Instant getCreationDate() {
|
||||
return creationDate;
|
||||
}
|
||||
|
||||
public void setCreationDate(Instant creationDate) {
|
||||
this.creationDate = creationDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[id:" + id.toString() + ", title:" + title + ", creationDate: " + creationDate.toString() + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.datastax.cassandra.repository;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
|
||||
public class KeyspaceRepository {
|
||||
private final CqlSession session;
|
||||
|
||||
public KeyspaceRepository(CqlSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public void createKeyspace(String keyspaceName, String replicationStrategy, int numberOfReplicas) {
|
||||
StringBuilder sb = new StringBuilder("CREATE KEYSPACE IF NOT EXISTS ").append(keyspaceName)
|
||||
.append(" WITH replication = {")
|
||||
.append("'class':'").append(replicationStrategy)
|
||||
.append("','replication_factor':").append(numberOfReplicas)
|
||||
.append("};");
|
||||
|
||||
final String query = sb.toString();
|
||||
|
||||
session.execute(query);
|
||||
}
|
||||
|
||||
public void useKeyspace(String keyspace) {
|
||||
session.execute("USE " + keyspace);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.datastax.cassandra.repository;
|
||||
|
||||
import com.baeldung.datastax.cassandra.domain.Video;
|
||||
import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
import com.datastax.oss.driver.api.core.cql.BoundStatement;
|
||||
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
|
||||
import com.datastax.oss.driver.api.core.cql.ResultSet;
|
||||
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VideoRepository {
|
||||
|
||||
private static final String TABLE_NAME = "videos";
|
||||
|
||||
private final CqlSession session;
|
||||
|
||||
public VideoRepository(CqlSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public void createTable() {
|
||||
createTable(null);
|
||||
}
|
||||
|
||||
public void createTable(String keyspace) {
|
||||
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME).append(" (")
|
||||
.append("video_id UUID,")
|
||||
.append("title TEXT,")
|
||||
.append("creation_date TIMESTAMP,")
|
||||
.append("PRIMARY KEY(video_id));");
|
||||
|
||||
String query = sb.toString();
|
||||
|
||||
executeStatement(SimpleStatement.newInstance(query), keyspace);
|
||||
}
|
||||
|
||||
public UUID insertVideo(Video video) {
|
||||
return insertVideo(video, null);
|
||||
}
|
||||
|
||||
public UUID insertVideo(Video video, String keyspace) {
|
||||
UUID videoId = UUID.randomUUID();
|
||||
|
||||
video.setId(videoId);
|
||||
|
||||
String absoluteTableName = keyspace != null ? keyspace + "." + TABLE_NAME: TABLE_NAME;
|
||||
|
||||
StringBuilder sb = new StringBuilder("INSERT INTO ").append(absoluteTableName)
|
||||
.append("(video_id, title, creation_date) values (:video_id, :title, :creation_date)");
|
||||
|
||||
PreparedStatement preparedStatement = session.prepare(sb.toString());
|
||||
|
||||
BoundStatement statement = preparedStatement.bind()
|
||||
.setUuid("video_id", video.getId())
|
||||
.setString("title", video.getTitle())
|
||||
.setInstant("creation_date", video.getCreationDate());
|
||||
|
||||
session.execute(statement);
|
||||
|
||||
return videoId;
|
||||
}
|
||||
|
||||
public List<Video> selectAll() {
|
||||
return selectAll(null);
|
||||
}
|
||||
|
||||
public List<Video> selectAll(String keyspace) {
|
||||
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME);
|
||||
|
||||
String query = sb.toString();
|
||||
|
||||
ResultSet resultSet = executeStatement(SimpleStatement.newInstance(query), keyspace);
|
||||
|
||||
List<Video> result = new ArrayList<>();
|
||||
|
||||
resultSet.forEach(x -> result.add(
|
||||
new Video(x.getUuid("video_id"), x.getString("title"), x.getInstant("creation_date"))
|
||||
));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private ResultSet executeStatement(SimpleStatement statement, String keyspace) {
|
||||
if (keyspace != null) {
|
||||
statement.setKeyspace(CqlIdentifier.fromCql(keyspace));
|
||||
}
|
||||
|
||||
return session.execute(statement);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue