Class Cluster
Information and known state of a Cassandra cluster.
This is the main entry point of the driver. A simple example of access to a Cassandra cluster would be:
Cluster cluster = Cluster.Builder().AddContactPoint("192.168.0.1").Build();
Session session = cluster.Connect("db1");
foreach (var row in session.Execute("SELECT * FROM table1"))
// do something ...
A cluster object maintains a permanent connection to one of the cluster node that it uses solely to maintain information on the state and current topology of the cluster. Using the connection, the driver will discover all the nodes composing the cluster as well as new nodes joining the cluster.
Inheritance
Inherited Members
Namespace: Cassandra
Assembly: Cassandra.dll
Syntax
public class Cluster : ICluster, IDisposable
Properties
Configuration
Gets the cluster configuration.
Declaration
public Configuration Configuration { get; }
Property Value
| Type | Description |
|---|---|
| Configuration |
MaxProtocolVersion
Gets or sets the maximum protocol version used by this driver.
While property value is maintained for backward-compatibility, use SetMaxProtocolVersion(ProtocolVersion) to set the maximum protocol version used by the driver.
Protocol version used can not be higher than MaxSupported.
Declaration
public static int MaxProtocolVersion { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Metadata
Gets read-only metadata on the connected cluster.
This includes the know nodes (with their status as seen by the driver) as well as the schema definitions.
This method may trigger the creation of a connection if none has been established yet.
Declaration
public Metadata Metadata { get; }
Property Value
| Type | Description |
|---|---|
| Metadata |
Methods
AllHosts()
Returns all known hosts of this cluster.
Declaration
public ICollection<Host> AllHosts()
Returns
| Type | Description |
|---|---|
| ICollection<Host> |
Builder()
Creates a new Cluster.NewBuilder instance.
This is a shortcut
for new Cluster.NewBuilder()
Declaration
public static Builder Builder()
Returns
| Type | Description |
|---|---|
| Builder | the new cluster builder. |
BuildFrom(IInitializer)
Build a new cluster based on the provided initializer.
Note that for building a cluster programmatically, Cluster.NewBuilder provides a slightly less verbose shortcut with NewBuilder#Build.
Also note that that all
the contact points provided by initializer must share the same
port.
Declaration
public static Cluster BuildFrom(IInitializer initializer)
Parameters
| Type | Name | Description |
|---|---|---|
| IInitializer | initializer | the Cluster.Initializer to use |
Returns
| Type | Description |
|---|---|
| Cluster | the newly created Cluster instance |
Connect()
Creates a new session on this cluster.
Declaration
public ISession Connect()
Returns
| Type | Description |
|---|---|
| ISession |
Connect(string)
Creates a new session on this cluster and using a keyspace an existing keyspace.
Declaration
public ISession Connect(string keyspace)
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyspace | Case-sensitive keyspace name to use |
Returns
| Type | Description |
|---|---|
| ISession |
ConnectAndCreateDefaultKeyspaceIfNotExists(Dictionary<string, string>, bool)
Creates new session on this cluster, and sets it to default keyspace.
If default keyspace does not exist then it will be created and session will be set to it.
Name of default keyspace can be specified during creation of cluster object with Cluster.Builder().WithDefaultKeyspace("keyspace_name") method.
Declaration
public ISession ConnectAndCreateDefaultKeyspaceIfNotExists(Dictionary<string, string> replication = null, bool durableWrites = true)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, string> | replication | Replication property for this keyspace. To set it, refer to the ReplicationStrategies class methods. It is a dictionary of replication property sub-options where key is a sub-option name and value is a value for that sub-option. Default value is |
| bool | durableWrites | Whether to use the commit log for updates on this keyspace. Default is set to |
Returns
| Type | Description |
|---|---|
| ISession | a new session on this cluster set to default keyspace. |
ConnectAsync()
Creates a new session on this cluster.
Declaration
public Task<ISession> ConnectAsync()
Returns
| Type | Description |
|---|---|
| Task<ISession> |
ConnectAsync(string)
Creates a new session on this cluster and using a keyspace an existing keyspace.
Declaration
public Task<ISession> ConnectAsync(string keyspace)
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyspace | Case-sensitive keyspace name to use |
Returns
| Type | Description |
|---|---|
| Task<ISession> |
Dispose()
Declaration
public void Dispose()
GetHost(IPEndPoint)
Get the host instance for a given Ip address.
Declaration
public Host GetHost(IPEndPoint address)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | address | Ip address of the host |
Returns
| Type | Description |
|---|---|
| Host | The host or null if not found |
GetReplicas(byte[])
Gets a collection of replicas for a given partitionKey. Backward-compatibility only, use GetReplicas(keyspace, partitionKey) instead.
Declaration
public ICollection<Host> GetReplicas(byte[] partitionKey)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | partitionKey | Byte array representing the partition key |
Returns
| Type | Description |
|---|---|
| ICollection<Host> |
GetReplicas(string, byte[])
Gets a collection of replicas for a given partitionKey on a given keyspace
Declaration
public ICollection<Host> GetReplicas(string keyspace, byte[] partitionKey)
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyspace | Byte array representing the partition key |
| byte[] | partitionKey | Byte array representing the partition key |
Returns
| Type | Description |
|---|---|
| ICollection<Host> |
RefreshSchema(string, string)
Updates keyspace metadata (including token metadata for token aware routing) for a given keyspace or a specific keyspace table. If no keyspace is provided then this method will update the metadata and token map for all the keyspaces of the cluster.
Declaration
public bool RefreshSchema(string keyspace = null, string table = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyspace | |
| string | table |
Returns
| Type | Description |
|---|---|
| bool |
RefreshSchemaAsync(string, string)
Updates keyspace metadata (including token metadata for token aware routing) for a given keyspace or a specific keyspace table. If no keyspace is provided then this method will update the metadata and token map for all the keyspaces of the cluster.
Declaration
public Task<bool> RefreshSchemaAsync(string keyspace = null, string table = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyspace | |
| string | table |
Returns
| Type | Description |
|---|---|
| Task<bool> |
Shutdown(int)
Shutdown this cluster instance. This closes all connections from all the
sessions of this * Cluster instance and reclaim all resources
used by it.
This method has no effect if the cluster was already shutdown.
Declaration
public void Shutdown(int timeoutMs = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | timeoutMs |
ShutdownAsync(int)
Shutdown this cluster instance asynchronously. This closes all connections from all the
sessions of this * Cluster instance and reclaim all resources
used by it.
This method has no effect if the cluster was already shutdown.
Declaration
public Task ShutdownAsync(int timeoutMs = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | timeoutMs |
Returns
| Type | Description |
|---|---|
| Task |
Events
HostAdded
Event that gets triggered when a new host is added to the cluster
Declaration
public event Action<Host> HostAdded
Event Type
| Type | Description |
|---|---|
| Action<Host> |
HostRemoved
Event that gets triggered when a host has been removed from the cluster
Declaration
public event Action<Host> HostRemoved
Event Type
| Type | Description |
|---|---|
| Action<Host> |