Beispiel #1
0
        static void Main(string[] args)
        {
            //建立数据库连接
            TTransport transport = new TSocket("192.168.10.2", 9160);
            TProtocol  protocol  = new TBinaryProtocol(transport);

            Cassandra.Client client = new Cassandra.Client(protocol);
            transport.Open();

            System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;
            long       timeStamp      = DateTime.Now.Millisecond;
            ColumnPath nameColumnPath = new ColumnPath()
            {
                Column_family = "Standard1",
                Column        = utf8Encoding.GetBytes("age")
            };

            //写入数据
            client.insert("Keyspace1",
                          "studentA",
                          nameColumnPath,
                          utf8Encoding.GetBytes("18"),
                          timeStamp,
                          ConsistencyLevel.ONE);

            //读取数据
            ColumnOrSuperColumn returnedColumn = client.get("Keyspace1", "studentA", nameColumnPath, ConsistencyLevel.ONE);

            Console.WriteLine("Keyspace1/Standard1: age: {0}, value: {1}", utf8Encoding.GetString(returnedColumn.Column.Name), utf8Encoding.GetString(returnedColumn.Column.Value));

            //关闭连接
            transport.Close();
        }
        public void SimpleScenario()
        {
            TTransport transport = new TSocket("localhost", 9160);
            TProtocol protocol = new TBinaryProtocol(transport);
            var client = new Cassandra.Client(protocol);

            Console.WriteLine("Opening Connection");
            transport.Open();

            //At this point we're using the standard configuration file
            var nameColumnPath = new ColumnPath("Standard1", null, "name");

            Console.WriteLine("Inserting a column");

            client.insert("Keyspace1",
                "1",
                nameColumnPath,
                "Josh Blogs".UTF(),
                Util.UnixTimestamp,
                ConsistencyLevel.ONE
                );

            client.insert("Keyspace1",
                "2",
                nameColumnPath,
                "Something else".UTF(),
                Util.UnixTimestamp,
                ConsistencyLevel.ONE);

            //Let's get something back out (this is our select statement)
            ColumnOrSuperColumn returnedColumn = client.get(
                "Keyspace1", //The database
                "1", //The actual key we want
                nameColumnPath, //Where that key sits
                ConsistencyLevel.ONE //HAZY
                );

            Console.WriteLine("We got Name: {0}, value {1}",
                returnedColumn.Column.Name.UTFDecode(),
                returnedColumn.Column.Value.UTFDecode());

            Console.WriteLine("Now let's try getting a range");

            //This is telling us the offest to get.  This is where paging would occur.
            var predicate = new SlicePredicate(new SliceRange(false, 10));
            var parent = new ColumnParent("Standard1");

            var keyedResults =
                client.multiget_slice("Keyspace1",
                    new List<string> { "1", "2" },
                    parent,
                    predicate,
                    ConsistencyLevel.ONE);

            foreach (var keyedResult in keyedResults)
            {
                Console.WriteLine("Key: {0}", keyedResult.Key);
                foreach (ColumnOrSuperColumn result in keyedResult.Value)
                {
                    Column column = result.Column;
                    Console.WriteLine("Name: {0}, value: {1}",
                        column.Name.UTFDecode(),
                        column.Value.UTFDecode());
                }
            }

            Console.WriteLine("closing connection");
            transport.Close();
        }
        static void Main(string[] args)
        {
            TTransport transport = new TSocket("localhost", 9160);
            TProtocol  protocol  = new TBinaryProtocol(transport);

            Cassandra.Client client = new Cassandra.Client(protocol);

            Console.WriteLine("Opening connection");
            transport.Open();

            System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;

            long       timeStamp      = DateTime.Now.Millisecond;
            ColumnPath nameColumnPath = new ColumnPath()
            {
                Column_family = "Standard1",
                Column        = utf8Encoding.GetBytes("name")
            };

            Console.WriteLine("Inserting name columns");

            //Insert the data into the column 'name'
            client.insert("Keyspace1",
                          "1",
                          nameColumnPath,
                          utf8Encoding.GetBytes("Joe Bloggs"),
                          timeStamp,
                          ConsistencyLevel.ONE);

            client.insert("Keyspace1",
                          "2",
                          nameColumnPath,
                          utf8Encoding.GetBytes("Joe Soap"),
                          timeStamp,
                          ConsistencyLevel.ONE);

            //Simple single value get using the key to get column 'name'
            ColumnOrSuperColumn returnedColumn = client.get("Keyspace1", "1", nameColumnPath, ConsistencyLevel.ONE);

            Console.WriteLine("Column Data in Keyspace1/Standard1: name: {0}, value: {1}",
                              utf8Encoding.GetString(returnedColumn.Column.Name),
                              utf8Encoding.GetString(returnedColumn.Column.Value));

            Console.WriteLine("Getting splice range");

            //Read an entire row
            SlicePredicate predicate = new SlicePredicate()
            {
                Slice_range = new SliceRange()
                {
                    //Start and Finish cannot be null
                    Start    = new byte[0],
                    Finish   = new byte[0],
                    Count    = 10,
                    Reversed = false
                }
            };

            ColumnParent parent = new ColumnParent()
            {
                Column_family = "Standard1"
            };
            Dictionary <string, List <ColumnOrSuperColumn> > results = client.multiget_slice("Keyspace1",
                                                                                             new List <string>()
            {
                "1", "2"
            },
                                                                                             parent,
                                                                                             predicate,
                                                                                             ConsistencyLevel.ONE);

            foreach (KeyValuePair <string, List <ColumnOrSuperColumn> > resultPair in results)
            {
                Console.WriteLine("Key: {0}", resultPair.Key);

                foreach (ColumnOrSuperColumn resultColumn in resultPair.Value)
                {
                    Column column = resultColumn.Column;
                    Console.WriteLine("name: {0}, value: {1}", utf8Encoding.GetString(column.Name), utf8Encoding.GetString(column.Value));
                }
            }

            Console.WriteLine("Closing connection");
            transport.Close();
        }
Beispiel #4
0
 public void AddColumn(string key, string cf, string name, int value, Cassandra.Client client)
 {
     client.insert(ToByte(key), GetParent(cf), NewColumn(name, value), ConsistencyLevel.ONE);
 }