当我使用cql创建列族时,它给出了非常意外的输出。
public static void createColumnfamily()
{
try
{
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
Connection con = DriverManager.getConnection("jdbc:cassandra://192.168.1.32:9160/temp");
String qry = "CREATE TABLE users(user_name varchar," +
"password varchar," +
"gender varchar," +
"session_token varchar," +
"birth_year bigint," +
"PRIMARY KEY (user_name)" +
")";
Statement smt = con.createStatement();
smt.executeUpdate(qry);
System.out.println("TABLE(column family) is created");
con.close();
}
catch(Exception e)
{
System.out.println(" : " + e.getMessage());
}
}
这里我得到了:第1行:132无关输入')'期待EOF
答案 0 :(得分:3)
您正在使用CQL 2,它不支持该主键声明样式。如果你想以这种方式声明它,你应该使用CQL 3,你可以通过在连接URL中请求该版本来完成:
DriverManager.getConnection("jdbc:cassandra://192.168.1.32:9160/temp?version=3.0.0");
但是,CQL 3并不仅仅是为此。正如Steve Van Opstal建议的那样,您可以简单地将PRIMARY KEY
标记与列定义本身放在一起,因为您没有多组件主键。
CREATE TABLE users(
user_name varchar PRIMARY KEY,
password varchar,
gender varchar,
session_token varchar,
birth_year bigint,
);
如果您的Cassandra支持它,通常首选CQL 3,因为它是前进的方式,但如果您不想立即切换,您可以进行第二次更改。
答案 1 :(得分:1)
我在这里只看到两个可能的问题。
查询不接受您定义主键的方式
试试这个:
String qry = "CREATE TABLE users(user_name varchar PRIMARY KEY," +
"password varchar," +
"gender varchar," +
"session_token varchar," +
"birth_year bigint"
")";
查询不接受varchar作为主键
试试这个:
String qry = "CREATE TABLE users(user_id int" +
"user_name varchar," +
"password varchar," +
"gender varchar," +
"session_token varchar," +
"birth_year bigint," +
"PRIMARY KEY (user_id)" +
")";