Top Banner
Getting Started with DataStax C# Driver Luke Tillman Language Evangelist @LukeTillman
37

Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Jan 15, 2015

Download

Technology

So you’ve grabbed the latest 2.0 beta of DataStax C# driver from NuGet. Now what? In this talk, Luke will walk you through some of the basics of the C# driver--how to bootstrap the driver and connect to a cluster, execute statements, and retrieve result sets. Wondering what the difference between a PreparedStatement and a SimpleStatement is? Not sure what the appropriate lifetime for a Cluster or a Session object is and whether you should reuse one (from multiple threads)? What about ADO.NET and LINQ support? We’ll cover this and more, so that you can get on with building applications on top of Cassandra and .NET.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Getting Started with DataStax C# Driver

Luke TillmanLanguage Evangelist

@LukeTillman

Page 2: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Life as the .NET Language Evangelist

Page 3: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Where do I get the driver?

• NuGet

• GitHub• https://github.com/datastax/csharp-driver

Page 4: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Bootstrapping the Driver

Page 5: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Cluster

• Singleton - one per application• Use the Builder

Cluster cluster = Cluster.Builder() .AddContactPoint("127.0.0.1") .Build();

Page 6: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Cluster

• Fluent Interface with Lots of Options

var authProvider = new PlainTextAuthProvider("username", "password");var queryOptions = new QueryOptions() .SetConsistencyLevel(ConsistencyLevel.LocalQuorum)    .SetPageSize(1000);             Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1")                 .WithSSL()                 .WithQueryOptions(queryOptions)                 .WithAuthProvider(authProvider)                 .Build();

Page 7: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Session

• Singleton per keyspace

• Inspired by the (N)Hibernate session object

• Get it from your Cluster object

ISession session = cluster.Connect("killrvideo");

Page 8: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Sample IoC Container Registration

// Use the Cluster builder to create a cluster Cluster cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build(); // Use the cluster to connect a session to the appropriate keyspaceISession session = cluster.Connect("killrvideo");

// Register both Cluster and ISession instances with Windsor (as// Singletons since it will reuse the instance)container.Register( Component.For<Cluster>().Instance(cluster),    Component.For<ISession>().Instance(session));

Page 9: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Creating Statements

Page 10: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Types of Statements

• SimpleStatement

• PreparedStatement / BoundStatement

• BatchStatement

Page 11: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

SimpleStatement

• It’s… simple?• Can use bind parameters

• Useful for one-off statements or dynamic CQL where you can’t prepare it

var statement =  new SimpleStatement("SELECT * FROM users WHERE userid = ?");statement = statement.Bind(145);

Page 12: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

PreparedStatement / BoundStatement• Pay the cost of Prepare once (server roundtrip)

• Save the PreparedStatement instance and reuse

PreparedStatement prepared = session.Prepare( "SELECT * FROM user_credentials WHERE email = ?");

Page 13: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

PreparedStatement / BoundStatement• Bind variable values to get BoundStatement

for execution

• Execution only has to send variable values• You will use these all the time

BoundStatement bound =  prepared.Bind("[email protected]");

Page 14: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

BatchStatement

• Add Simple/Bound statements to a batch

BoundStatement bound = prepared.Bind(video.VideoId, video.Name);var simple = new SimpleStatement( "UPDATE videos SET name = ? WHERE videoid = ?").Bind(video.Name, video.VideoId);             // Use an atomic batch to send over all the mutationsvar batchStatement = new BatchStatement();batchStatement.AddQuery(bound);batchStatement.AddQuery(simple);

Page 15: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

BatchStatement

• Batches are Logged, atomic (by default) and this is the most common use case

• Set the batch type to use a different type of batch

• Counters have their own batch type (can’t mix)

var batch =  new BatchStatement().SetBatchType(BatchType.Unlogged);

Page 16: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Statements – You’ve Got Options

• Simple and Bound statements have options that can be set at the Statement level

• Consistency Level

• Retry Policy

• Paging Size (for automatic paging, we’ll come back to this)

• Tracing

• If not set at the statement level, defaults set when configuring/building the Cluster are used

Page 17: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Statements – You’ve Got Options

• Example of binding a PreparedStatement and setting available options:

IStatement bound =  prepared.Bind("[email protected]")            .SetPageSize(100)            .SetConsistencyLevel(ConsistencyLevel.LocalOne)            .SetRetryPolicy(new DefaultRetryPolicy())            .EnableTracing();

Page 18: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Executing Statements and Getting the Results

Page 19: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Executing Statements• Use your Session object to execute statements• You can execute statements synchronously or

asynchronously• Synchronous

• Asynchronous

• Execute methods return a RowSet

RowSet rows = await _session.ExecuteAsync(boundStatement);

RowSet rows = _session.Execute(boundStatement);

Page 20: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

RowSet

• RowSet implements IEnumerable<Row>

• Use GetValue<T> method on a Row to get a column’s value

• By column name

• By ordinal (position)

Page 21: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

RowSet

• Because RowSet implements IEnumerable<Row>:

• Iterate with foreachRowSet rows = await _session.ExecuteAsync(boundStatement);foreach (Row row in rows){    returnList.Add(new VideoPreview    {        VideoId = row.GetValue<Guid>("videoid"),        AddedDate = row.GetValue<DateTimeOffset>("added_date"),        Name = row.GetValue<string>("name")    });}

Page 22: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

RowSet

• Because RowSet implements IEnumerable<Row>:

• Project Rows with LINQ to Objects Select()RowSet rows = await _session.ExecuteAsync(boundStatement);var returnList = rows.Select(row => new VideoPreview{ VideoId = row.GetValue<Guid>(0),    AddedDate = row.GetValue<DateTimeOffset>(1),    Name = row.GetValue<string>(2)}).ToList();

Page 23: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

RowSet

• Because RowSet implements IEnumerable<Row>:

• Get a single row with LINQ to Objects Single() or SingleOrDefault()

RowSet rows = await _session.ExecuteAsync(boundStatement);Row row = rows.SingleOrDefault();

Page 24: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

CQL 3 Data Types to .NET Types

Full listing available in driver docs (http://www.datastax.com/docs)

CQL 3 Data Type .NET Type

bigint, counter long

boolean bool

decimal, float float

double double

int int

uuid, timeuuid System.Guid

text, varchar string (Encoding.UTF8)

timestamp System.DateTimeOffset

varint System.Numerics.BigInteger

Page 25: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Driver 2.0 Features

Page 26: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Lightweight Transactions (LWT)

• Use when you don’t want writes to step on each other

• AKA Linearizable Consistency

• Serial Isolation Level

• Be sure to read the fine print: has a latency cost associated with using it, so use only where needed

• The canonical example: unique user accounts

Page 27: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Lightweight Transactions (LWT)

• Returns a column called [applied] indicating success/failure

• Different from the relational world where you might expect an Exception (i.e. PrimaryKeyViolationException or similar)

var statement = new SimpleStatement("INSERT INTO user_credentials (email, password) VALUES (?, ?) IF NOT EXISTS");statement = statement.Bind("[email protected]", "Password1!");

RowSet rows = await _session.ExecuteAsync(statement);var userInserted = rows.Single().GetValue<bool>("[applied]");

Page 28: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Automatic Paging

• The Problem: Loading big result sets into memory is a recipe for disaster (OutOfMemoryExceptions, etc.)

• Better to load and process a large result set in pages (chunks)

• Doing this manually with Cassandra prior to 2.0 was a pain

• Automatic Paging makes paging on a large RowSet transparent

Page 29: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Automatic Paging• Set a page size on a statement (or will use default from

Cluster)• Iterate over the resulting RowSet

• As you iterate, new pages are fetched transparently when the Rows in the current page are exhausted

• Will allow you to iterate until all pages are exhausted

boundStatement = boundStatement.SetPageSize(100);RowSet rows = await _session.ExecuteAsync(boundStatement);foreach (Row row in rows){}

Page 30: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Typical Paging in a Web Application

• Show page of records in UI and allow user to navigate

• Automatic Paging – this is not the feature you are looking for

Page 31: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

Where To Go From Here

Page 32: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

LINQ to CQL

• Comes in the NuGet package as Cassandra.Data.Linq

• Has support for all CRUD operations

• Start by decorating the objects you’ll be querying with Table, Column, and PartitionKey attributes

Page 33: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

LINQ to CQL

[Table("user_credentials")]public class UserCredentials{    [Column("email")]    [PartitionKey]    public string EmailAddress { get; set; }

    [Column("password")]    public string Password { get; set; }

    [Column("userid")]    public Guid UserId { get; set; }}

Page 34: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

LINQ to CQL

• Then query with LINQ using the Session’s GetTable<T> method as your starting point

public UserCredentials GetCredentials(string emailAddress){    IEnumerable<UserCredentials> results =  _session.GetTable<UserCredentials>()                .Where(uc => uc.EmailAddress == emailAddress)                .Execute();

    return results.SingleOrDefault();}

Page 35: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

ADO.NET Support

• Available in the NuGet package as Cassandra.Data

• Allows you to use your “favorite” ADO.NET objects like DbConnection, DbCommand, etc. to query Cassandra

• My recommendation? Avoid it.

• Cassandra concepts don’t always map well to ADO.NET

Page 36: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

The KillrVideo Sample Application

• Many of this presentation’s samples are taken from here

• https://github.com/luketillman/killrvideo-csharp

Page 37: Cassandra Day NY 2014: Getting Started with the DataStax C# Driver

What Next?• Data Modeling, Data Modeling, Data Modeling• Planet Cassandra (

http://www.planetcassandra.org)• Links to videos, drivers, documentation, tutorials, etc.

Follow me on Twitter for updates: @LukeTillman