Skip to content

Commit 568948d

Browse files
committed
Merge pull request apache#643 from datastax/java1069
Bootstrap driver-examples module
2 parents 80986fa + 16842dd commit 568948d

43 files changed

Lines changed: 773 additions & 44 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ The driver contains the following modules:
3737

3838
- driver-core: the core layer.
3939
- driver-mapping: the object mapper.
40+
- driver-extras: optional features for the Java driver.
4041
- driver-examples: example applications using the other modules which are
4142
only meant for demonstration purposes.
43+
- driver-tests: tests for the java-driver.
4244

4345
**Useful links:**
4446

changelog/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- [bug] JAVA-1046: (Dynamic)CompositeTypes need to be parsed as string literal, not blob.
2121
- [improvement] JAVA-1164: Clarify documentation on Host.listenAddress and broadcastAddress.
2222
- [improvement] JAVA-1171: Add Host method to determine if DSE Graph is enabled.
23+
- [improvement]: JAVA-1069: Bootstrap driver-examples module.
24+
- [documentation] JAVA-1150: Add example and FAQ entry about ByteBuffer/BLOB.
2325

2426
Merged from 2.1 branch:
2527

driver-examples/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# DataStax Java Driver for Apache Cassandra - Examples
2+
3+
This module contains examples of how to use the DataStax Java driver for
4+
Apache Cassandra.
5+
6+
## Usage
7+
8+
Unless otherwise stated, all examples assume that you have a single-node Cassandra 3.0 cluster
9+
listening on localhost:9042.
10+

driver-examples/pom.xml

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,45 @@
1515
limitations under the License.
1616
1717
-->
18-
<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">
19-
<modelVersion>4.0.0</modelVersion>
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2020
<parent>
21-
<groupId>com.datastax.cassandra</groupId>
2221
<artifactId>cassandra-driver-parent</artifactId>
22+
<groupId>com.datastax.cassandra</groupId>
2323
<version>3.0.1-SNAPSHOT</version>
2424
</parent>
25-
<artifactId>cassandra-driver-examples-parent</artifactId>
26-
<packaging>pom</packaging>
27-
<name>DataStax Java Driver for Apache Cassandra Examples</name>
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<artifactId>cassandra-driver-examples</artifactId>
28+
29+
<name>DataStax Java Driver for Apache Cassandra - Examples</name>
2830
<description>A collection of examples to demonstrate DataStax Java Driver for Apache Cassandra.</description>
2931
<url>https://github.com/datastax/java-driver</url>
3032

3133
<properties>
3234
<main.basedir>${project.parent.basedir}</main.basedir>
3335
</properties>
3436

35-
<modules>
36-
<module>stress</module>
37-
<module>osgi</module>
38-
</modules>
37+
<dependencies>
3938

40-
<licenses>
41-
<license>
42-
<name>Apache 2</name>
43-
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
44-
<distribution>repo</distribution>
45-
<comments>Apache License Version 2.0</comments>
46-
</license>
47-
</licenses>
39+
<dependency>
40+
<groupId>com.datastax.cassandra</groupId>
41+
<artifactId>cassandra-driver-core</artifactId>
42+
<version>${project.parent.version}</version>
43+
</dependency>
4844

49-
<scm>
50-
<connection>scm:git:git@github.com:datastax/java-driver.git</connection>
51-
<developerConnection>scm:git:git@github.com:datastax/java-driver.git</developerConnection>
52-
<url>https://github.com/datastax/java-driver</url>
53-
<tag>HEAD</tag>
54-
</scm>
45+
<dependency>
46+
<groupId>ch.qos.logback</groupId>
47+
<artifactId>logback-classic</artifactId>
48+
<version>1.1.3</version>
49+
</dependency>
5550

56-
<developers>
57-
<developer>
58-
<name>Various</name>
59-
<organization>DataStax</organization>
60-
</developer>
61-
</developers>
51+
</dependencies>
6252

6353
<build>
54+
6455
<plugins>
56+
6557
<plugin>
6658
<groupId>org.apache.maven.plugins</groupId>
6759
<artifactId>maven-install-plugin</artifactId>
@@ -70,6 +62,7 @@
7062
<skip>true</skip>
7163
</configuration>
7264
</plugin>
65+
7366
<plugin>
7467
<groupId>org.apache.maven.plugins</groupId>
7568
<artifactId>maven-deploy-plugin</artifactId>
@@ -78,6 +71,32 @@
7871
<skip>true</skip>
7972
</configuration>
8073
</plugin>
74+
8175
</plugins>
76+
8277
</build>
78+
79+
<licenses>
80+
<license>
81+
<name>Apache 2</name>
82+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
83+
<distribution>repo</distribution>
84+
<comments>Apache License Version 2.0</comments>
85+
</license>
86+
</licenses>
87+
88+
<scm>
89+
<connection>scm:git:git@github.com:datastax/java-driver.git</connection>
90+
<developerConnection>scm:git:git@github.com:datastax/java-driver.git</developerConnection>
91+
<url>https://github.com/datastax/java-driver</url>
92+
<tag>HEAD</tag>
93+
</scm>
94+
95+
<developers>
96+
<developer>
97+
<name>Various</name>
98+
<organization>DataStax</organization>
99+
</developer>
100+
</developers>
101+
83102
</project>
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (C) 2012-2015 DataStax Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.driver.examples.basic;
17+
18+
import com.datastax.driver.core.Cluster;
19+
import com.datastax.driver.core.ResultSet;
20+
import com.datastax.driver.core.Row;
21+
import com.datastax.driver.core.Session;
22+
23+
/**
24+
* Creates a keyspace and tables, and loads some data into them.
25+
* <p/>
26+
* Preconditions:
27+
* - a Cassandra cluster is running and accessible through the contacts points identified by CONTACT_POINTS and PORT.
28+
* <p/>
29+
* Side effects:
30+
* - creates a new keyspace "simplex" in the cluster. It a keyspace with this name already exists, it will be reused;
31+
* - creates two tables "simplex.songs" and "simplex.playlists". If they exist already, they will be reused;
32+
* - inserts a row in each table.
33+
*
34+
* @see <a href="http://datastax.github.io/java-driver/manual/">Java driver online manual</a>
35+
*/
36+
public class CreateAndPopulateKeyspace {
37+
38+
static String[] CONTACT_POINTS = {"127.0.0.1"};
39+
static int PORT = 9042;
40+
41+
public static void main(String[] args) {
42+
43+
CreateAndPopulateKeyspace client = new CreateAndPopulateKeyspace();
44+
45+
try {
46+
47+
client.connect(CONTACT_POINTS, PORT);
48+
client.createSchema();
49+
client.loadData();
50+
client.querySchema();
51+
52+
} finally {
53+
client.close();
54+
}
55+
}
56+
57+
private Cluster cluster;
58+
59+
private Session session;
60+
61+
/**
62+
* Initiates a connection to the cluster
63+
* specified by the given contact point.
64+
*
65+
* @param contactPoints the contact points to use.
66+
* @param port the port to use.
67+
*/
68+
public void connect(String[] contactPoints, int port) {
69+
70+
cluster = Cluster.builder()
71+
.addContactPoints(contactPoints).withPort(port)
72+
.build();
73+
74+
System.out.printf("Connected to cluster: %s%n", cluster.getMetadata().getClusterName());
75+
76+
session = cluster.connect();
77+
}
78+
79+
/**
80+
* Creates the schema (keyspace) and tables
81+
* for this example.
82+
*/
83+
public void createSchema() {
84+
85+
session.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication " +
86+
"= {'class':'SimpleStrategy', 'replication_factor':1};");
87+
88+
session.execute(
89+
"CREATE TABLE IF NOT EXISTS simplex.songs (" +
90+
"id uuid PRIMARY KEY," +
91+
"title text," +
92+
"album text," +
93+
"artist text," +
94+
"tags set<text>," +
95+
"data blob" +
96+
");");
97+
98+
session.execute(
99+
"CREATE TABLE IF NOT EXISTS simplex.playlists (" +
100+
"id uuid," +
101+
"title text," +
102+
"album text, " +
103+
"artist text," +
104+
"song_id uuid," +
105+
"PRIMARY KEY (id, title, album, artist)" +
106+
");");
107+
}
108+
109+
/**
110+
* Inserts data into the tables.
111+
*/
112+
public void loadData() {
113+
114+
session.execute(
115+
"INSERT INTO simplex.songs (id, title, album, artist, tags) " +
116+
"VALUES (" +
117+
"756716f7-2e54-4715-9f00-91dcbea6cf50," +
118+
"'La Petite Tonkinoise'," +
119+
"'Bye Bye Blackbird'," +
120+
"'Joséphine Baker'," +
121+
"{'jazz', '2013'})" +
122+
";");
123+
124+
session.execute(
125+
"INSERT INTO simplex.playlists (id, song_id, title, album, artist) " +
126+
"VALUES (" +
127+
"2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," +
128+
"756716f7-2e54-4715-9f00-91dcbea6cf50," +
129+
"'La Petite Tonkinoise'," +
130+
"'Bye Bye Blackbird'," +
131+
"'Joséphine Baker'" +
132+
");");
133+
}
134+
135+
/**
136+
* Queries and displays data.
137+
*/
138+
public void querySchema() {
139+
140+
ResultSet results = session.execute(
141+
"SELECT * FROM simplex.playlists " +
142+
"WHERE id = 2cc9ccb7-6221-4ccb-8387-f22b6a1b354d;");
143+
144+
System.out.printf("%-30s\t%-20s\t%-20s%n", "title", "album", "artist");
145+
System.out.println("-------------------------------+-----------------------+--------------------");
146+
147+
for (Row row : results) {
148+
149+
System.out.printf("%-30s\t%-20s\t%-20s%n",
150+
row.getString("title"),
151+
row.getString("album"),
152+
row.getString("artist"));
153+
154+
}
155+
156+
}
157+
158+
/**
159+
* Closes the session and the cluster.
160+
*/
161+
public void close() {
162+
session.close();
163+
cluster.close();
164+
}
165+
166+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2012-2015 DataStax Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.driver.examples.basic;
17+
18+
import com.datastax.driver.core.Cluster;
19+
import com.datastax.driver.core.ResultSet;
20+
import com.datastax.driver.core.Row;
21+
import com.datastax.driver.core.Session;
22+
23+
/**
24+
* Connects to a Cassandra cluster and extracts basic information from it.
25+
* <p/>
26+
* Preconditions:
27+
* - a Cassandra cluster is running and accessible through the contacts points identified by CONTACT_POINTS and PORT.
28+
* <p/>
29+
* Side effects: none.
30+
*
31+
* @see <a href="http://datastax.github.io/java-driver/manual/">Java driver online manual</a>
32+
*/
33+
public class ReadCassandraVersion {
34+
35+
static String[] CONTACT_POINTS = {"127.0.0.1"};
36+
static int PORT = 9042;
37+
38+
public static void main(String[] args) {
39+
40+
Cluster cluster = null;
41+
try {
42+
// The Cluster object is the main entry point of the driver.
43+
// It holds the known state of the actual Cassandra cluster (notably the Metadata).
44+
// This class is thread-safe, you should create a single instance (per target Cassandra cluster), and share
45+
// it throughout your application.
46+
cluster = Cluster.builder()
47+
.addContactPoints(CONTACT_POINTS).withPort(PORT)
48+
.build();
49+
50+
// The Session is what you use to execute queries. Likewise, it is thread-safe and should be reused.
51+
Session session = cluster.connect();
52+
53+
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
54+
// of Row objects.
55+
ResultSet rs = session.execute("select release_version from system.local");
56+
// Extract the first row (which is the only one in this case).
57+
Row row = rs.one();
58+
59+
// Extract the value of the first (and only) column from the row.
60+
String releaseVersion = row.getString("release_version");
61+
System.out.printf("Cassandra version is: %s%n", releaseVersion);
62+
63+
} finally {
64+
// Close the cluster after we’re done with it. This will also close any session that was created from this
65+
// cluster.
66+
// This step is important because it frees underlying resources (TCP connections, thread pools...). In a
67+
// real application, you would typically do this at shutdown (for example, when undeploying your webapp).
68+
if (cluster != null)
69+
cluster.close();
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)