Quantcast
Channel: DataStax Support Forums » Recent Topics
Viewing all articles
Browse latest Browse all 387

Gibu on "java driver InvalidQueryException: no keyspace has been specified"

$
0
0

I was running the SimpleClient example provided with Java Driver for Apache Cassandra 1.0 using CQL 3.

I spent quite some time to figure out why I was getting the error. I get the error "no keyspace has been specified" even though I explicitly mention the keyspace in the query. Later figured out that I need to explicitly set it in the "session" in the case of PreparedStatement usage as seen in the code attached.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Connected to cluster: MyCluster
Datatacenter: datacenter1; Host: localhost/127.0.0.1; Rack: rack1
Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException: no keyspace has been specified
at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:32)
at com.datastax.driver.core.ResultSetFuture.extractCause(ResultSetFuture.java:242)
at com.datastax.driver.core.Session.toPreparedStatement(Session.java:243)
at com.datastax.driver.core.Session.prepare(Session.java:167)
at com.example.cassandra.SimpleClient.loadDataUsingBoundStatements(SimpleClient.java:30)
at com.example.cassandra.SimpleClient.main(SimpleClient.java:152)
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: no keyspace has been specified
at com.datastax.driver.core.ResultSetFuture.convertException(ResultSetFuture.java:272)

Can someone confirm if this is the expected behavior as I am setting it in the query as keyspace.table name.

Code attached.

Thanks
Gibu
ps: If its the expected behaviour, we could change the tutorial here to include that.
http://www.datastax.com/doc-source/developer/java-driver/index.html#quick_start/qsSimpleClientBoundStatements_t.html

//$Id$
package com.example.cassandra;

import com.datastax.driver.core.Cluster;

import com.datastax.driver.core.Host;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.*;
import java.util.*;

public class SimpleClient {
private Cluster cluster;
private Session session;

public Session getSession() {

return session;
}

public void loadDataUsingBoundStatements() {

//Bug ? comment this line and this gives an exception that there is no keyspace set even though its set in the query.
//getSession().execute("use simplex");

PreparedStatement statement = getSession().prepare(
"INSERT INTO simplex.songs " +
"(id, title, album, artist, tags) " +
"VALUES (?, ?, ?, ?, ?);");
BoundStatement boundStatement = new BoundStatement(statement);
Set<String> tags = new HashSet<String>();
tags.add("jazz");
tags.add("2013");
getSession().execute(boundStatement.bind(
UUID.fromString("756716f7-2e54-4715-9f00-91dcbea6cf50"),
"La Petite Tonkinoise'",
"Bye Bye Blackbird'",
"Joséphine Baker",
tags ) );
statement = getSession().prepare(
"INSERT INTO simplex.playlists " +
"(id, song_id, title, album, artist) " +
"VALUES (?, ?, ?, ?, ?);");
boundStatement = new BoundStatement(statement);
getSession().execute(boundStatement.bind(
UUID.fromString("2cc9ccb7-6221-4ccb-8387-f22b6a1b354d"),
UUID.fromString("756716f7-2e54-4715-9f00-91dcbea6cf50"),
"La Petite Tonkinoise",
"Bye Bye Blackbird",
"Joséphine Baker") );
}

public void querySchema() {
ResultSet results = session.execute("SELECT * FROM simplex.playlists " +
"WHERE id = 2cc9ccb7-6221-4ccb-8387-f22b6a1b354d;");
System.out.println("\n"+String.format("%-30s\t%-20s\t%-20s\n%s", "title", "album", "artist",
"-------------------------------+-----------------------+--------------------"));
for (Row row : results) {
System.out.println(String.format("%-30s\t%-20s\t%-20s", row.getString("title"),
row.getString("album"), row.getString("artist")));
}
System.out.println();

}

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'" +
");");

}

public void createSchema() {

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

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

}

public void connect(String node) {
cluster = Cluster.builder()
.addContactPoint(node).build();
session = cluster.connect();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for ( Host host : metadata.getAllHosts() ) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
}

public void close() {
cluster.shutdown();
}

public static void main(String[] args) {
SimpleClient client = new SimpleClient();
client.connect("localhost");
try {
client.createSchema();
}
catch(Exception alreadyExists)
{

System.out.print("Exception: Schema already exists.");
}
//client.loadData();
client.loadDataUsingBoundStatements();
client.querySchema();

client.close();
}
}


Viewing all articles
Browse latest Browse all 387

Trending Articles