通过以下方式清除我的密钥空间后:
drop keyspace simplex
我正在通过datastax java Cassandra客户端(来自scala代码)执行以下命令:
val songsTable = (
"CREATE TABLE IF NOT EXISTS simplex.songs ("
+ "id uuid PRIMARY KEY,"
+ "title text,"
+ "album text,"
+ "artist text,"
+ "tags set<text>,"
+ "data blob"
+ ");")
val listsTable = (
"CREATE TABLE IF NOT EXISTS simplex.playlists ("
+ "id bigint,"
+ "title text,"
+ "album text, "
+ "artist text,"
+ "song_id uuid,"
+ "PRIMARY KEY (id, title, album, artist)"
+ ");")
val songs = (
"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'}"
+ ");")
...
...
...
val rsf = session.executeAsync(command) // in parallel
rsf.addListener(() => p.success(rsf.get), exec)
这导致仅创建播放列表表,并且“创建歌曲表”和“插入歌曲”命令的回调永远不会被执行。
据我所知,datastax java Cassandra客户端可以安全地同时使用。这不是这种情况吗?我的假设有什么问题?
答案 0 :(得分:1)
当您创建表或键空间时 - 在集成测试中很常见 - 您需要关闭Session
和Cluster
实例,因为Cluster
元数据已过期。虽然我看到一条日志消息说驱动程序正在尝试异步更新元数据但它似乎永远不会及时返回 - 关闭并重新打开Cluster
和Session
个实例,以便进行集成测试。
因为Cluster.connect(keyspaceName)
存在而且我似乎无法让USE keyspace;
作为执行命令正常工作(永远不要明白这对于在任何客户端可以任意使用其他键空间的应用程序中共享会话意味着什么)我最后写了以下内容:
(此处_cluster
是Option[Cluster]
,preStart
期间为CassandraSessionActor
初始化,keyspace
是实施者定义的Option[String]
。 CassandraSessionActor
在请求时返回Session
,但在getSession
期间调用preStart
一次,以便根据Datastax 4 Simple Rules在整个应用程序中共享会话。)< / p>
trait AutoCreateKeyspace extends CassandraSessionActor {
override def getSession = {
keyspace match {
case None => _cluster.get.connect()
case Some(ks) =>
_cluster.map { cluster =>
if (cluster.getMetadata.getKeyspace(ks) == null &&
cluster.getMetadata.getKeyspace(ks.toLowerCase) == null) {
val temporarySession = cluster.connect()
log.debug(s"creating keyspace $ks")
temporarySession.execute(s"""CREATE KEYSPACE $ks
|with replication = {
|'class': 'SimpleStrategy',
|'replication_factor': 2 }""".stripMargin)
temporarySession.close()
cluster.close()
}
}
_cluster = Some(Cassandra.createNewCluster())
_cluster.get.connect(ks)
}
}
}
private object Cassandra extends LazyLogging {
def conf = ConfigFactory.load()
val hosts = conf.getStringList("cassandra.hosts")
logger.info(s"cassandra.hosts specified: $hosts")
def createNewCluster(): Cluster = {
Cluster.builder()
.addContactPoints(hosts: _*)
.build()
}
}