Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ The driver contains the following modules:

- driver-core: the core layer.
- driver-mapping: the object mapper.
- driver-extras: optional features for the Java driver.
- driver-examples: example applications using the other modules which are
only meant for demonstration purposes.
- driver-tests: tests for the java-driver.

**Useful links:**

Expand Down
2 changes: 2 additions & 0 deletions changelog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
- [bug] JAVA-1046: (Dynamic)CompositeTypes need to be parsed as string literal, not blob.
- [improvement] JAVA-1164: Clarify documentation on Host.listenAddress and broadcastAddress.
- [improvement] JAVA-1171: Add Host method to determine if DSE Graph is enabled.
- [improvement]: JAVA-1069: Bootstrap driver-examples module.
- [documentation] JAVA-1150: Add example and FAQ entry about ByteBuffer/BLOB.

Merged from 2.1 branch:

Expand Down
10 changes: 10 additions & 0 deletions driver-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# DataStax Java Driver for Apache Cassandra - Examples

This module contains examples of how to use the DataStax Java driver for
Apache Cassandra.

## Usage

Unless otherwise stated, all examples assume that you have a single-node Cassandra 3.0 cluster
listening on localhost:9042.

79 changes: 49 additions & 30 deletions driver-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,45 @@
limitations under the License.

-->
<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>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-parent</artifactId>
<groupId>com.datastax.cassandra</groupId>
<version>3.0.1-SNAPSHOT</version>
</parent>
<artifactId>cassandra-driver-examples-parent</artifactId>
<packaging>pom</packaging>
<name>DataStax Java Driver for Apache Cassandra Examples</name>
<modelVersion>4.0.0</modelVersion>

<artifactId>cassandra-driver-examples</artifactId>

<name>DataStax Java Driver for Apache Cassandra - Examples</name>
<description>A collection of examples to demonstrate DataStax Java Driver for Apache Cassandra.</description>
<url>https://github.com/datastax/java-driver</url>

<properties>
<main.basedir>${project.parent.basedir}</main.basedir>
</properties>

<modules>
<module>stress</module>
<module>osgi</module>
</modules>
<dependencies>

<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>Apache License Version 2.0</comments>
</license>
</licenses>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>${project.parent.version}</version>
</dependency>

<scm>
<connection>scm:git:git@github.com:datastax/java-driver.git</connection>
<developerConnection>scm:git:git@github.com:datastax/java-driver.git</developerConnection>
<url>https://github.com/datastax/java-driver</url>
<tag>HEAD</tag>
</scm>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>

<developers>
<developer>
<name>Various</name>
<organization>DataStax</organization>
</developer>
</developers>
</dependencies>

<build>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
Expand All @@ -70,6 +62,7 @@
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
Expand All @@ -78,6 +71,32 @@
<skip>true</skip>
</configuration>
</plugin>

</plugins>

</build>

<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>Apache License Version 2.0</comments>
</license>
</licenses>

<scm>
<connection>scm:git:git@github.com:datastax/java-driver.git</connection>
<developerConnection>scm:git:git@github.com:datastax/java-driver.git</developerConnection>
<url>https://github.com/datastax/java-driver</url>
<tag>HEAD</tag>
</scm>

<developers>
<developer>
<name>Various</name>
<organization>DataStax</organization>
</developer>
</developers>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (C) 2012-2015 DataStax Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.examples.basic;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

/**
* Creates a keyspace and tables, and loads some data into them.
* <p/>
* Preconditions:
* - a Cassandra cluster is running and accessible through the contacts points identified by CONTACT_POINTS and PORT.
* <p/>
* Side effects:
* - creates a new keyspace "simplex" in the cluster. It a keyspace with this name already exists, it will be reused;
* - creates two tables "simplex.songs" and "simplex.playlists". If they exist already, they will be reused;
* - inserts a row in each table.
*
* @see <a href="http://datastax.github.io/java-driver/manual/">Java driver online manual</a>
*/
public class CreateAndPopulateKeyspace {

static String[] CONTACT_POINTS = {"127.0.0.1"};
static int PORT = 9042;

public static void main(String[] args) {

CreateAndPopulateKeyspace client = new CreateAndPopulateKeyspace();

try {

client.connect(CONTACT_POINTS, PORT);
client.createSchema();
client.loadData();
client.querySchema();

} finally {
client.close();
}
}

private Cluster cluster;

private Session session;

/**
* Initiates a connection to the cluster
* specified by the given contact point.
*
* @param contactPoints the contact points to use.
* @param port the port to use.
*/
public void connect(String[] contactPoints, int port) {

cluster = Cluster.builder()
.addContactPoints(contactPoints).withPort(port)
.build();

System.out.printf("Connected to cluster: %s%n", cluster.getMetadata().getClusterName());

session = cluster.connect();
}

/**
* Creates the schema (keyspace) and tables
* for this example.
*/
public void createSchema() {

session.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication " +
"= {'class':'SimpleStrategy', 'replication_factor':1};");

session.execute(
"CREATE TABLE IF NOT EXISTS simplex.songs (" +
"id uuid PRIMARY KEY," +
"title text," +
"album text," +
"artist text," +
"tags set<text>," +
"data blob" +
");");

session.execute(
"CREATE TABLE IF NOT EXISTS simplex.playlists (" +
"id uuid," +
"title text," +
"album text, " +
"artist text," +
"song_id uuid," +
"PRIMARY KEY (id, title, album, artist)" +
");");
}

/**
* Inserts data into the tables.
*/
public void loadData() {

session.execute(
"INSERT INTO simplex.songs (id, title, album, artist, tags) " +
"VALUES (" +
"756716f7-2e54-4715-9f00-91dcbea6cf50," +
"'La Petite Tonkinoise'," +
"'Bye Bye Blackbird'," +
"'Joséphine Baker'," +
"{'jazz', '2013'})" +
";");

session.execute(
"INSERT INTO simplex.playlists (id, song_id, title, album, artist) " +
"VALUES (" +
"2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," +
"756716f7-2e54-4715-9f00-91dcbea6cf50," +
"'La Petite Tonkinoise'," +
"'Bye Bye Blackbird'," +
"'Joséphine Baker'" +
");");
}

/**
* Queries and displays data.
*/
public void querySchema() {

ResultSet results = session.execute(
"SELECT * FROM simplex.playlists " +
"WHERE id = 2cc9ccb7-6221-4ccb-8387-f22b6a1b354d;");

System.out.printf("%-30s\t%-20s\t%-20s%n", "title", "album", "artist");
System.out.println("-------------------------------+-----------------------+--------------------");

for (Row row : results) {

System.out.printf("%-30s\t%-20s\t%-20s%n",
row.getString("title"),
row.getString("album"),
row.getString("artist"));

}

}

/**
* Closes the session and the cluster.
*/
public void close() {
session.close();
cluster.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2012-2015 DataStax Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.examples.basic;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

/**
* Connects to a Cassandra cluster and extracts basic information from it.
* <p/>
* Preconditions:
* - a Cassandra cluster is running and accessible through the contacts points identified by CONTACT_POINTS and PORT.
* <p/>
* Side effects: none.
*
* @see <a href="http://datastax.github.io/java-driver/manual/">Java driver online manual</a>
*/
public class ReadCassandraVersion {

static String[] CONTACT_POINTS = {"127.0.0.1"};
static int PORT = 9042;

public static void main(String[] args) {

Cluster cluster = null;
try {
// The Cluster object is the main entry point of the driver.
// It holds the known state of the actual Cassandra cluster (notably the Metadata).
// This class is thread-safe, you should create a single instance (per target Cassandra cluster), and share
// it throughout your application.
cluster = Cluster.builder()
.addContactPoints(CONTACT_POINTS).withPort(PORT)
.build();

// The Session is what you use to execute queries. Likewise, it is thread-safe and should be reused.
Session session = cluster.connect();

// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
// of Row objects.
ResultSet rs = session.execute("select release_version from system.local");
// Extract the first row (which is the only one in this case).
Row row = rs.one();

// Extract the value of the first (and only) column from the row.
String releaseVersion = row.getString("release_version");
System.out.printf("Cassandra version is: %s%n", releaseVersion);

} finally {
// Close the cluster after we’re done with it. This will also close any session that was created from this
// cluster.
// This step is important because it frees underlying resources (TCP connections, thread pools...). In a
// real application, you would typically do this at shutdown (for example, when undeploying your webapp).
if (cluster != null)
cluster.close();
}
}
}
Loading