本文将简单介绍一些在Cassandra之中如何使用Java对数据进行操作的简单例子。包括了简单的CRUD。更深入一些的内容,将在后面逐个进行讲解。
后来根据官方的例子做了一些说明,同时对其进行了一些补完。主要内容还请参考代码之中的注释。原文是英文注释,对重要内容进行了翻译,其他新增的注释,个人觉得也比较有帮助。
PS: 这真是极有态度的贴代码了!(修改之后的链接)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
/* * 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 demo; 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.<br> * 创建Keyspace与table,并插入一些数据 * <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. If 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 CassandraUsageDemo { static String[] CONTACT_POINTS = {"127.0.0.1"}; static int PORT = 9042; private Cluster cluster; private Session session; public static void main(String[] args) { CassandraUsageDemo client = new CassandraUsageDemo(); try { client.connect(CONTACT_POINTS, PORT); client.createSchema(); client.loadData(); client.querySchema(); client.deleteData(); } finally { client.close(); } } /** * Initiates a connection to the cluster * specified by the given contact point. <br> * 连接到指定的Cassandra节点。 该节点最好是Seeds server * * @param contactPoints the contact points to use. 连接点 * @param port the port to use. 端口,默认9042 */ 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() { // 创建Keyspace simplex, 如果之前已经创建了就直接复用 // 使用SimpleStrategy, 复制因子=1 (数据没有备份,只存放1份) session.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication " + "= {'class':'SimpleStrategy', 'replication_factor':1};"); // 创建Table simplex.songs session.execute( "CREATE TABLE IF NOT EXISTS simplex.songs (" + "id uuid PRIMARY KEY," + "title text," + "album text," + "artist varchar," + // Cassandra 之中varchar == text "tags set<text>," + // 这里用了set的数据类型 "data blob" + // 二进制类型 ");"); // 创建表simplex.playlists // 注意:playlists 跟 songs的数据是重复的, // 只是primary key 不太一样。 // 这就是NoSQL跟RDBMS的不一样: 反范式 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.<br> * 插入数据 */ 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.<br> * 查询数据 */ 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")); } } /** * Delete data <br> * 删除数据 */ private void deleteData() { ResultSet results = session.execute( "delete from simplex.playlists " + "WHERE id = 2cc9ccb7-6221-4ccb-8387-f22b6a1b354d;"); // 删除之后再次进行查询 querySchema(); } /** * Closes the session and the cluster.<br> * 最后一定要记得关闭! */ public void close() { session.close(); cluster.close(); } } |
更新:
2016-09-09 更新创建表的语句,之前将varchar 写成 varchart
本文为原创文章,转载请注明出处

文章评论
你好 ,,我的一个表插入100万条的时候,查询超时,
你教程5的配置超时我已经配置很大了,但还是超时,不知道你是否遇见过这种问题
read_request_timeout_in_ms: 1000000000000
下面那几个设置同上,我已经设置很大了,再大都无法启动了
select count(*) from table_name where type = '1';
你好!
我感觉这还是Cassandra的原因呢。 我猜想,还是因为select count(*) 操作会对全表进行扫描所致~
我后来类似的这些操作都是在Spark 上进行
感谢你的提问, 我后面会更详细的研究。
请继续关注我的博客,感谢!