简体   繁体   中英

cassandra keyspace with capital letter

Using the framework Cassandra Achilles , it generates the CQL below but it works only when the keyspace name is lowercase

Session session = cluster.connect();
session.execute("DROP KEYSPACE IF EXISTS Smart;");
session.execute("CREATE KEYSPACE Smart WITH replication = { "                  
                  + " 'class': 'SimpleStrategy',  'replication_factor': '3' }; ");
session.execute("CREATE TYPE IF NOT EXISTS smart.bio_udt("+
                "birthplace text,"+
                "diplomas list<text>,"+
                "description text);");
session.execute("CREATE TABLE IF NOT EXISTS Smart.users("+
                "id bigint,"+
                "age_in_year int,"+
                "bio frozen<\"Smart\".bio_udt>,"+
                "favoritetags set<text>,"+
                "firstname text,"+
                "lastname text,"+
                "preferences map<int, text>,"+
                "PRIMARY KEY(id))");

with the error :

com.datastax.driver.core.exceptions.InvalidQueryException: 
Statement on keyspace smart cannot refer to a user type in keyspace Smart; 
user types can only be used in the keyspace they are defined in

what is the problem ?

The issue is that the check for the UDT creation query that is failing is case sensititive, where as all the other queries are not. Because you have not provided the "Smart" syntax, Cassandra thought you really meant "smart" with all lower case.

So if you write your final query to work, all you have to do is to write it like this:

CREATE TABLE IF NOT EXISTS Smart.users(
  id bigint,
  age_in_year int,
  bio frozen<"smart".bio_udt>,
  favoritetags set<text>,
  firstname text,lastname text,
  preferences map<int, text>,
  PRIMARY KEY(id)
);

You have several options actually, you can use smart , Smart , "smart" , but not "Smart" , because the first three are referring to the same thing, namely smart , the last variant however is telling Cassandra "I'm looking for a keyspace with this exact casing, starting with capital S .

Without the quote notation, Cassandra thinks you meant case insensitive keyspaces, which will make it all lowercase by default.

As proof, try this in cqlsh:

cqlsh> CREATE KEYSPACE THISISUPPER WITH replication = { 'class': 'SimpleStrategy',  'replication_factor': '3' };
cqlsh> DESCRIBE KEYSPACE thisisupper ;

CREATE KEYSPACE thisisupper WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

If you really want to have it all case sensitive use quotes, and then you won't be able to access it unless you input the exact name of the keyspace.

cqlsh> CREATE KEYSPACE "HEYAQUOTES" WITH replication = { 'class': 'SimpleStrategy',  'replication_factor': '3' };
cqlsh> DESCRIBE KEYSPACE heyquotes;

Keyspace 'heyquotes' not found.
cqlsh> DESCRIBE KEYSPACE "heyaquotes";

Keyspace 'heyaquotes' not found.
cqlsh> DESCRIBE KEYSPACE HEYAQUOTES;

Keyspace 'heyaquotes' not found.
cqlsh> DESCRIBE KEYSPACE "HEYAQUOTES";

CREATE KEYSPACE "HEYAQUOTES" WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM