/// <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); }
/// <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); }
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 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); }
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); }
/// <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); }
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(); }
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); }
/// <summary> /// /// </summary> public void Close() { CheckWasDisposed(); if (!IsOpen) { return; } lock (_lock) { _transport.Close(); _transport = null; _client = null; } }
public static Cassandra.Client GetClient() { if (_transport == null) { _transport = new TFramedTransport(new TSocket("localhost", 9160)); } TProtocol frameProtocol = new TBinaryProtocol(_transport); var client = new Cassandra.Client(frameProtocol); if (!_transport.IsOpen) { _transport.Open(); } client.set_keyspace(_keySpace); return(client); }
public void BlogModelScenario() { TTransport transport = new TSocket("localhost", 9160); TProtocol protocol = new TBinaryProtocol(transport); var client = new Cassandra.Client(protocol); Console.WriteLine("Opening Connection"); transport.Open(); string entryTitle = "now with bonus batch writes"; string entryAuthor = "josh"; string entryBody = "This is my blog entry yet again"; string entryPostDate = DateTime.Now.ToShortDateString(); var cfmap = new Dictionary<string, List<ColumnOrSuperColumn>>(); //Column families are case sensitive //"BlogEntries" cfmap.Add("Standard1", new List<ColumnOrSuperColumn> { new ColumnOrSuperColumn(new Column("title", entryTitle)), new ColumnOrSuperColumn(new Column("body", entryBody)), new ColumnOrSuperColumn(new Column("author", entryAuthor)), new ColumnOrSuperColumn(new Column("postDate", entryPostDate)), }); client.batch_insert("Keyspace1", entryTitle, cfmap, ConsistencyLevel.ONE); //Now Read it back. var predicate = new SlicePredicate(new SliceRange(false, 10)); var parent = new ColumnParent("Standard1"); //"BlogEntries"); var results = client.get_slice("Keyspace1", entryTitle, parent, predicate, ConsistencyLevel.ONE); foreach (ColumnOrSuperColumn resultColumn in results) { Column column = resultColumn.Column; Console.WriteLine("Name: {0}, value: {1}", column.Name.UTFDecode(), column.Value.UTFDecode()); } Console.WriteLine("closing connection"); transport.Close(); }
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"); if (client.Version != CassandraVersion.v0_5_1) throw new ArgumentOutOfRangeException("client version is not v 0.5.1"); 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(); }
public Datacenter() { Client = ThriftTool.GetClient(); }
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); }
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(); }
public void SetFixture(RawThriftFixture fixture) { env = fixture; client = env.Client; }
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(); }
public RawThriftFixture() { client = new Cassandra.Client(new TBinaryProtocol(transport)); }
/// <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.Cassandra051.Cassandra.Client; monitor.IncrementCounter(ClientCounter.SKIP_HOST_SUCCESS); //log.info("Skipped host. New host is: {}", client.getUrl()); }
public static List <KeySlice> GetAllFromCF(string cf, int count, Cassandra.Client client) { return(client.get_range_slices(GetParent(cf), GetPredicate(count), GetKeyRange(count), ConsistencyLevel.ONE)); }