Giter Club home page Giter Club logo

csharp-driver's Introduction

Datastax C# Driver for Apache Cassandra

A C# client driver for Apache Cassandra. This driver works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's binary protocol.

Installation

Get it on Nuget

PM> Install-Package CassandraCSharpDriver

Features

  • Connection pooling
  • Node discovery
  • Automatic failover
  • Several load balancing and retry policies
  • Result paging
  • Query batching
  • Linq2Cql and Ado.Net support

Documentation

Getting Help

You can use the project Mailing list or create a ticket on the Jira issue tracker.

Upgrading from 1.x branch

If you are upgrading from the 1.x branch of the driver, be sure to have a look at the upgrade guide.

Basic Usage

//Create a cluster instance using 3 cassandra nodes.
var cluster = Cluster.Builder()
  .AddContactPoints("host1", "host2", "host3")
  .Build();
//Create connections to the nodes using a keyspace
var session = cluster.Connect("sample_keyspace");
//Execute a query on a connection synchronously
var rs = session.Execute("SELECT * FROM sample_table");
//Iterate through the RowSet
foreach (var row in rs)
{
  var value = row.GetValue<int>("sample_int_column");
  //do something with the value
}

Prepared statements

Prepare your query once and bind different parameters to obtain the better performance.

//Prepare a statement once
var ps = session.Prepare("UPDATE user_profiles SET birth=? WHERE key=?");

//...bind different parameters every time you need to execute
var statement = ps.Bind(new DateTime(1942, 11, 27), "hendrix");
//Execute the bound statement with the provided parameters
session.Execute(statement);

Batching statements

You can execute multiple statements (prepared or unprepared) in a batch to update/insert several rows atomically even in different column families.

//Prepare the statements involved in a profile update once
var profileStmt = session.Prepare("UPDATE user_profiles SET email=? WHERE key=?");
var userTrackStmt = session.Prepare("INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)");
//...you should reuse the prepared statement
//Bind the parameters and add the statement to the batch batch
var batch = new BatchStatement()
  .Add(profileStmt.Bind(emailAddress, "hendrix"))
  .Add(userTrackStmt.Bind("hendrix", "You changed your email", DateTime.Now));
//Execute the batch
session.Execute(batch);

Asynchronous API

Session allows asynchronous execution of statements (for any type of statement: simple, bound or batch) by exposing the ExecuteAsync method.

//Execute a statement on asynchronously using TPL
var task = session.ExecuteAsync(statement);
//The task can waited, awaited, continued, ...
task.ContinueWith((t) =>
{
  var rs = t.Result;
  //Iterate through the rows
  foreach (var row in rs)
  {
    //Get the values from each row
  }
}, TaskContinuationOptions.OnlyOnRanToCompletion);

Automatic pagination of results

You can iterate indefinitely over the RowSet, having the rows fetched block by block until the rows available on the client side are exhausted.

var statement = new SimpleStatement("SELECT * from large_table");
//Set the page size, in this case the RowSet will not contain more than 1000 at any time
statement.SetPageSize(1000);
var rs = session.Execute(statement);
foreach (var row in rs)
{
  //The enumerator will yield all the rows from Cassandra
  //Retrieving them in the back in blocks of 1000.
}

Setting cluster and statement execution options

You can set the options on how the driver connects to the nodes and the execution options.

//Example at cluster level
var cluster = Cluster
  .Builder()
  .AddContactPoints(hosts)
  .WithCompression(CompressionType.LZ4)
  .WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy("west"));

//Example at statement (simple, bound, batch) level
var statement = new SimpleStatement(query)
  .SetConsistencyLevel(ConsistencyLevel.Quorum)
  .SetRetryPolicy(DowngradingConsistencyRetryPolicy.Instance)
  .SetPageSize(1000);

Building and running the tests

You can use Visual Studio or msbuild to build the solution.

Check the documentation for building the driver from source and running the tests.

License

Copyright 2014, DataStax

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

csharp-driver's People

Contributors

aaronontheweb avatar al3xandru avatar alprema avatar bbucher avatar bogdangrigg avatar datrollmon avatar joaquincasares avatar jorgebay avatar kcieslinski avatar luketillman avatar medvekoma avatar sindhudweep avatar sonofsatoshi avatar teddymaef avatar vytautassurvila avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.