Elffuss BraidHub

kikocis/java-driver public

375951765914 · 5.3 KiBRawPermalink
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 */
16package com.datastax.driver.examples.basic;
17
18import com.datastax.driver.core.Cluster;
19import com.datastax.driver.core.ResultSet;
20import com.datastax.driver.core.Row;
21import 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 */
36public 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}