Пример #1
0
		/// <summary>
		/// 
		/// </summary>
		private void InitTransportAndClient()
		{
			var socket = new TSocket(Server.Host, Server.Port, Server.Timeout * 1000);

			switch (ConnectionType)
			{
				case ConnectionType.Simple:
					_transport = socket;
					break;

				case ConnectionType.Buffered:
					_transport = new TBufferedTransport(socket, BufferSize);
					break;

				case ConnectionType.Framed:
					_transport = new TFramedTransport(socket);
					break;

				default:
					goto case ConnectionType.Framed;
			}

			var protocol = new TBinaryProtocol(_transport);
			_client = new Cassandra.Client(protocol);
		}
Пример #2
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();
        }
Пример #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        internal Connection(Server server, ConnectionBuilder builder)
        {
            Created = DateTime.Now;
            Server = server;

            var socket = new TSocket(server.Host, server.Port, server.Timeout * 1000);

            switch(builder.ConnectionType)
            {
                case ConnectionType.Simple:
                    _transport = socket;
                    break;

                case ConnectionType.Buffered:
                    _transport = new TBufferedTransport(socket, builder.BufferSize);
                    break;

                case ConnectionType.Framed:
                    _transport = new TFramedTransport(socket);
                    break;
            }

            _protocol = new TBinaryProtocol(_transport);
            _client = new Cassandra.Client(_protocol);
        }
        public CassandraClient Create(CassandraEndpoint endpoint)
        {
            TSocket socket = null;
            TTransport transport = null;
            if (endpoint.Timeout == 0)
            {
                socket = new TSocket(endpoint.Address, endpoint.Port);
            }
            else
            {
                socket = new TSocket(endpoint.Address, endpoint.Port, endpoint.Timeout);
            }

            if (this.isBufferSizeSet)
            {
                transport = new TBufferedTransport(socket, this.bufferSize);
            }
            else
            {
                transport = new TBufferedTransport(socket);
            }

            TProtocol protocol = new TBinaryProtocol(transport);
            Cassandra.Client cassandraClient = new Cassandra.Client(protocol);
            CassandraClient client = new CassandraClient(cassandraClient, endpoint);

            this.logger.Debug(logger.StringFormatInvariantCulture("Created a new connection using: '{0}'", endpoint.ToString()));

            return client;
        }
Пример #5
0
        public static Cassandra.Client GetClient()
        {
            if (_client == null)
            {
                if (_transport == null) _transport = new TFramedTransport(new TSocket("localhost", 9160));
                var client = new Cassandra.Client(new TBinaryProtocol(_transport));
                if (!_transport.IsOpen)
                {
                    try
                    {
                        _transport.Open();
                    }
                    catch (Exception e)
                    {
                        Utility.Logging("transport open fail:" + e.Message);
                    }
                }

                if (!_setKeySpace)
                {
                    client.set_keyspace(_keySpace);
                    _setKeySpace = true;
                }
                //return client;
                _client = client;
            }
            return _client;
        }
Пример #6
0
        private static void Main()
        {
            TTransport framedTransport = new TFramedTransport(new TSocket("localhost", 9160));
            TTransport socketTransport = new TSocket("localhost", 9160);
            TProtocol framedProtocol = new TBinaryProtocol(framedTransport);
            TProtocol socketProtocol = new TBinaryProtocol(socketTransport);

            var client = new Cassandra.Client(framedProtocol, framedProtocol); // all framed
            //var client = new Cassandra.Client(socketProtocol, socketProtocol); // all socket
            //var client = new Cassandra.Client(framedProtocol, socketProtocol); // in: framed out: socket
            //var client = new Cassandra.Client(socketProtocol, framedProtocol); // in: socket out: framed

            framedTransport.Open();
            socketTransport.Open();
            Console.WriteLine("Start");

            client.set_keyspace("Keyspace1");

            Console.WriteLine("Count Key");
            var key = Encoding.ASCII.GetBytes("MyKey");
            var columns = new List<byte[]>(new[] { Encoding.ASCII.GetBytes("MyColumn") });
            var column_parent = new ColumnParent {
                Column_family = "Standard1"
            };
            var predicate = new SlicePredicate {
                Column_names = columns
            };
            client.get_count(key, column_parent, predicate, ConsistencyLevel.ALL);

            Console.WriteLine("Done");
            Console.Read();
        }
Пример #7
0
 public static Cassandra.Client GetWebClient()
 {
     var transport = new TFramedTransport(new TSocket("localhost", 9160));
     var client = new Cassandra.Client(new TBinaryProtocol(transport));
     transport.Open();
     client.set_keyspace(_keySpace);
     return client;
 }
        public override void Open(string hostname)
        {
            base.Open(hostname);

            TTransport transport = new TFramedTransport(new TSocket(hostname, 9160));
            TProtocol protocol = new TBinaryProtocol(transport);
            _client = new Cassandra.Client(protocol);

            transport.Open();
        }
Пример #9
0
 public static Cassandra.Client GetClient(string keyspace, ref TTransport transport)
 {
     TTransport frameTransport = new TFramedTransport(new TSocket("localhost", 9160));
     TProtocol frameProtocol = new TBinaryProtocol(frameTransport);
     var client = new Cassandra.Client(frameProtocol, frameProtocol);
     frameTransport.Open();
     client.set_keyspace(keyspace);
     transport = frameTransport;
     return client;
 }
Пример #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        internal Connection(Server server)
        {
            Created = DateTime.Now;
            Server = server;

            TTransport socket = new TSocket(server.Host, server.Port, server.Timeout);

            _transport = new TFramedTransport(socket);
            _protocol = new TBinaryProtocol(_transport);
            _client = new Cassandra.Client(_protocol);
        }
Пример #11
0
        private static bool CheckCassandraIsRunning()
        {
            TTransport transport = new TSocket("localhost", 9160);
            TProtocol protocol = new TBinaryProtocol(transport);
            Cassandra.Client client = new Cassandra.Client(protocol);
            try
                {
                    transport.Open();
                    success = true;
                }
                catch
                {
                    success = false;
                }

            return success;
        }
Пример #12
0
        private void connectButton_Click(object sender, EventArgs e)
        {
            TSocket socket;
            Cassandra.Client client;

            socket = new TSocket(hostNameTextBox.Text, int.Parse(portTextBox.Text));
            //socket.Open();

            TFramedTransport transport = new TFramedTransport(socket);
            TBinaryProtocol protocol = new TBinaryProtocol(transport);
            client = new Cassandra.Client(protocol);

            transport.Open();

            mainForm.Client = client;
            this.DialogResult = DialogResult.OK;
        }
Пример #13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        internal Connection(Server server, int timeout = 0)
        {
            Created = DateTime.Now;
            Server = server;
            Timeout = timeout;

            //TcpClient client = new TcpClient(server.Host, server.Port);
            //client.NoDelay = true;
            //client.SendBufferSize = timeout;
            //client.ReceiveTimeout = timeout;

            //TTransport socket = new TSocket(client);

            TTransport socket = new TSocket(server.Host, server.Port, timeout);

            _transport = new TFramedTransport(socket);
            _protocol = new TBinaryProtocol(_transport);
            _client = new Cassandra.Client(_protocol);
        }
Пример #14
0
        public Keyspace(
			ICassandraClient client,
			string keyspaceName,
			IDictionary<string, Dictionary<string, string>> description,
			ConsistencyLevel consistencyLevel,
			FailoverPolicy failoverPolicy,
			IKeyedObjectPool<Endpoint, ICassandraClient> pool,
			ICassandraClientMonitor monitor)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            this.Client = client;
            this.ConsistencyLevel = consistencyLevel;
            this.Description = description;
            this.Name = keyspaceName;
            this.cassandra = client.Client as Cassandra.Client;
            this.FailoverPolicy = failoverPolicy;
            this.pool = pool;
            this.monitor = monitor;
            InitFailover();
        }
Пример #15
0
        /// <summary>
        /// Updates the client member and cassandra member to the next host in the ring.
        /// Returns the current client to the pool and retreives a new client from the
        /// next pool.
        /// </summary>
        void SkipToNextHost()
        {
            //log.info("Skipping to next host. Current host is: {}", client.getUrl());
            try
            {
                Client.MarkAsError();
                pool.Return(Client.Endpoint, Client);
                Client.RemoveKeyspace(this);
            }
            catch// (Exception e)
            {
                //log.error("Unable to invalidate client {}. Will continue anyhow.", client);
            }

            string nextHost = GetNextHost(Client.Endpoint.Host, Client.Endpoint.IP);
            if (nextHost == null)
            {
                //log.error("Unable to find next host to skip to at {}", tostring());
                throw new Exception("Unable to failover to next host");
            }
            // assume they use the same port
            Client = pool.Borrow(new Endpoint(nextHost, Client.Port));
            cassandra = Client.Client as Apache.Cassandra.Cassandra.Client;
            monitor.IncrementCounter(ClientCounter.SKIP_HOST_SUCCESS);
            //log.info("Skipped host. New host is: {}", client.getUrl());
        }
        public void ThriftRunWritePerformanceSingleThread()
        {
            Console.WriteLine("============================================================");
            Console.WriteLine(" Thrift Driver write performance test single thread ");
            Console.WriteLine("============================================================");

            TTransport transport = new TFramedTransport(new TSocket("localhost", 9160));
            TProtocol protocol = new TBinaryProtocol(transport);
            Cassandra.Client client = new Cassandra.Client(protocol);

            transport.Open();

            const string dropFoo = "drop keyspace Tests";
            try
            {
                client.execute_cql3_query(Encoding.UTF8.GetBytes(dropFoo), Compression.NONE, Apache.Cassandra.ConsistencyLevel.QUORUM);
            }
            catch
            {
            }

            const string createFoo = "CREATE KEYSPACE Tests WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}";
            Console.WriteLine("============================================================");
            Console.WriteLine(createFoo);
            Console.WriteLine("============================================================");

            client.execute_cql3_query(Encoding.UTF8.GetBytes(createFoo),
                                      Compression.NONE, Apache.Cassandra.ConsistencyLevel.QUORUM);
            Console.WriteLine();
            Console.WriteLine();

            const string createBar = "CREATE TABLE Tests.stresstest (strid varchar,intid int,PRIMARY KEY (strid))";
            Console.WriteLine("============================================================");
            Console.WriteLine(createBar);
            Console.WriteLine("============================================================");

            client.execute_cql3_query(Encoding.UTF8.GetBytes(createBar), Compression.NONE,
                                      Apache.Cassandra.ConsistencyLevel.QUORUM);
            Console.WriteLine();
            Console.WriteLine();

            CqlPreparedResult query = client.prepare_cql3_query(Encoding.UTF8.GetBytes("UPDATE tests.stresstest SET intid = ? WHERE strid = ?"),
                                                                Compression.NONE);

            int n = 0;
            while (n < NUM_ROUND)
            {
                var timer = new Stopwatch();
                timer.Start();

                for (int i = 0; i < NUM_WRITES_PER_ROUND; i++)
                {
                    CqlResult res = client.execute_prepared_cql3_query(query.ItemId,
                                                                       new List<byte[]>
                                                                           {
                                                                                   BitConverter.GetBytes(i).Reverse().ToArray(),
                                                                                   Encoding.ASCII.GetBytes(i.ToString("X"))
                                                                           },
                                                                       Apache.Cassandra.ConsistencyLevel.QUORUM);
                }

                timer.Stop();
                double rate = (1000.0*NUM_WRITES_PER_ROUND)/timer.ElapsedMilliseconds;
                Console.WriteLine("[Cassandra-Thrift] Time : " + timer.ElapsedMilliseconds + " (rate: " + rate + " qps)");
                n++;
            }

            Console.WriteLine("============================================================");
            Console.WriteLine(dropFoo);
            Console.WriteLine("============================================================");
            client.execute_cql3_query(Encoding.UTF8.GetBytes(dropFoo), Compression.NONE, Apache.Cassandra.ConsistencyLevel.QUORUM);
        }
Пример #17
0
		/// <summary>
		/// 
		/// </summary>
		public void Close()
		{
			CheckWasDisposed();

			if (!IsOpen)
				return;

			lock (_lock)
			{
				_transport.Close();
				_transport = null;
				_client = null;
			}
		}
Пример #18
0
 public Datacenter()
 {
     Client = ThriftTool.GetClient();
 }