getting started with datastax .net driver for cassandra
TRANSCRIPT
•
•
•
•
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();
// 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 keyspace
ISession 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)
);
•
•
•
var statement = new SimpleStatement("SELECT * FROM users WHERE userid = ?"); statement = statement.Bind(145);
•
•
PreparedStatement prepared = session.Prepare( "SELECT * FROM user_credentials WHERE email = ?");
•
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 mutations var batchStatement = new BatchStatement(); batchStatement.Add(bound); batchStatement.Add(simple);
•
IStatement bound = prepared.Bind("[email protected]") .SetPageSize(100) .SetConsistencyLevel(ConsistencyLevel.LocalOne) .SetRetryPolicy(new DefaultRetryPolicy()) .EnableTracing();
•
•
•
•
•
RowSet rows = await _session.ExecuteAsync(boundStatement);
RowSet rows = _session.Execute(boundStatement);
• IEnumerable<Row>
• foreach
RowSet 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") }); }
• IEnumerable<Row>
• 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();
• IEnumerable<Row>
• Single()SingleOrDefault()
RowSet rows = await _session.ExecuteAsync(boundStatement);
Row row = rows.SingleOrDefault();
•
•
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]");
•
•
•
•
boundStatement = boundStatement.SetPageSize(100); RowSet rows = await _session.ExecuteAsync(boundStatement); foreach (Row row in rows) { }
[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; } }
•
public UserCredentials GetCredentials(string emailAddress)
{
IEnumerable<UserCredentials> results =
_session.GetTable<UserCredentials>()
.Where(uc => uc.EmailAddress == emailAddress)
.Execute();
return results.SingleOrDefault();
}
•
•