先吐槽一下说明文档非人的简单
说明的简洁明快,一下子就明白怎么编写代码,但是有些地方确实不该太简洁的。给出的方法名和方法调用太不一致了,Session已经不是静态类了,但是文档里的例子还是静态类的写法。而在调用keyspace里的东西时必须连接上空间的,类似在命令端里的use keyspace,可是这个重要的过程变成了
_session = _cluster.Connect();猿们痛恨写文档可以理解,不过如果文档过时了的话,建议还是改改的好。吐槽完毕,上一个完整过程的代码。
static void Main(string[] args) { Cluster cluster = Cluster.Builder().AddContactPoint("192.168.84.168").Build(); Metadata metadata = cluster.Metadata; var list=metadata.GetKeyspaces(); for (int i = 0; i < list.Count; i++) { Console.WriteLine("spacename: " + list.ElementAt(i)); } Session session = cluster.Connect();//Session的方法不是静态的 string keyspace="t1"; session.CreateKeyspace(keyspace);//创建空间 session.ChangeKeyspace(keyspace);//连接到空间,文档里少这个 session.Execute("CREATE TABLE " + keyspace + "." + keyspace + "t (id uuid PRIMARY KEY,title text);"); PreparedStatement statement = session.Prepare("INSERT INTO " + keyspace + "." + keyspace + "t (id, title) VALUES (?, ?);"); BoundStatement boundStatement = new BoundStatement(statement); session.Execute(boundStatement.Bind(new Guid("756716f7-2e54-4715-9f00-91dcbea6cf50"),"La Petite Tonkinoise")); var tablelist= metadata.GetTables(keyspace); for (int i = 0; i < tablelist.Count; i++) { Console.WriteLine("table: " + tablelist.ElementAt(i)); } RowSet results = session.Execute("SELECT * FROM " + keyspace + "." + keyspace + "t WHERE id = 756716f7-2e54-4715-9f00-91dcbea6cf50;"); Console.WriteLine("-------------------------------+-----------------------+--------------------"); foreach (Row row in results.GetRows()) { Console.WriteLine(String.Format("{0, -40} {1, -20} ",row.GetValue<Guid>("id").ToString(), row.GetValue<String>("title"))); } cluster.Shutdown(); Console.Read(); }