zoukankan      html  css  js  c++  java
  • datastax的C# Driver for Apache Cassandra

    原版的说明文档:http://www.datastax.com/documentation/developer/csharp-driver/1.0/webhelp/index.html#csharp-driver/quick_start/qsSimpleClientAddSession_t.html

    先吐槽一下说明文档非人的简单

    说明的简洁明快,一下子就明白怎么编写代码,但是有些地方确实不该太简洁的。给出的方法名和方法调用太不一致了,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();
            }


  • 相关阅读:
    [LeetCode] Same Tree, Solution
    图搜索
    1 sec on Large Judge (java): https://github.com/l...
    [LeetCode] Path Sum, Solution
    嗯哪
    海量数据处理总结
    [LeetCode] Unique Binary Search Trees II, Solution
    [Interview] Serialize and Deserialize a tree
    设计题
    [LeetCode] Convert Sorted Array to Binary Search Tree, Solution
  • 原文地址:https://www.cnblogs.com/AI001/p/3368876.html
Copyright © 2011-2022 走看看