语法错误:“@”没有可行的替代方案

时间:2013-10-10 09:29:14

标签: cassandra datastax-enterprise datastax-java-driver

我正在尝试使用datastax驱动程序将地图插入cassandra。地图有价值

英特尔(R)酷睿(TM)i5-2520M CPU @ 2.50GHz

如果我尝试使用查询生成器插入值,我会收到语法错误,指出“@”没有可行字符。

如果我使用insert语句直接构造cql3注释并将地图构造为字符串,则会插入它。对这个问题有任何想法

1 个答案:

答案 0 :(得分:1)

(修订版) 添加值时是否使用Map类型?以下是添加包含地图列的行的几种方法。也许最后一个例子是你在尝试什么?字符串的使用仅用于调试,因此您可以看到在每种情况下创建的字符串。

       session.execute("DROP TABLE imdb.mapper;");

       String value = "Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz";

       session.execute("CREATE TABLE imdb.mapper (id int, maptest map<int,text>, PRIMARY KEY(id) );");
       session.execute("INSERT into imdb.mapper (id,maptest) VALUES (0,{});");
       System.out.println("Row inserted with empty map");

       session.execute(QueryBuilder.update("imdb","mapper").with(QueryBuilder.put("maptest", 5, value)).
               where(QueryBuilder.eq("id", 0)));
       System.out.println("Map updated with QueryBuilder");

       String s1 = "INSERT into imdb.mapper (id,maptest) VALUES (1,{1 : \'Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz\'});";
       System.out.println(s1);
       session.execute(s1);
       System.out.println("Row inserted as string");

       String s2 = "INSERT into imdb.mapper (id,maptest) VALUES (2,{1 : '"+value+"'});";
       System.out.println(s2);
       session.execute(s2);
       System.out.println("Row inserted as strings");

       Map<Integer,String> m = new HashMap<Integer,String>();
       m.put(1, value);
       String s3= QueryBuilder.insertInto("imdb", "mapper").value("id", 3)
               .value("maptest", m).toString() ;


       System.out.println("S="+s3);
       session.execute(s3);
       System.out.println("Row inserted as QueryBuilder");