private static void SetupKeyspace() { if (!CassandraSession.KeyspaceExists(server, keyspaceName)) { CassandraSession.AddKeyspace(server, new KsDef { Name = keyspaceName, Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List <CfDef>() }); } var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Posts")) { keyspace.AddColumnFamily(server, new CfDef { Name = "Posts", Keyspace = keyspaceName, Column_type = "Super", Comparator_type = "UTF8Type", Subcomparator_type = "UTF8Type", Comment = "Used for blog posts." }); } }
private static void Main(string[] args) { var keyspaceName = "Blog"; var server = new Server("localhost"); if (!CassandraSession.KeyspaceExists(server, keyspaceName)) CassandraSession.AddKeyspace(server, new KsDef { Name = keyspaceName, Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List<CfDef>() }); var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Posts")) keyspace.AddColumnFamily(server, new CfDef { Name = "Posts", Keyspace = keyspaceName, Column_type = "Super", Comparator_type = "UTF8Type", Subcomparator_type = "UTF8Type", Comment = "Used for blog posts." }); if (!keyspace.ColumnFamilyExists(server, "Comments")) keyspace.AddColumnFamily(server, new CfDef { Name = "Comments", Keyspace = keyspaceName, Column_type = "Super", Comparator_type = "TimeUUIDType", Subcomparator_type = "UTF8Type", Comment = "Used for blog post comments." }); using (var db = new CassandraContext(keyspace: keyspaceName, server: server)) { var family = db.GetColumnFamily<UTF8Type, UTF8Type>("Posts"); // create post dynamic post = family.CreateRecord(key: "first-blog-post"); // create post details dynamic postDetails = post.CreateSuperColumn(); postDetails.Title = "My First Cassandra Post"; postDetails.Body = "Blah. Blah. Blah. about my first post on how great Cassandra is to work with."; postDetails.Author = "Nick Berardi"; postDetails.PostedOn = DateTimeOffset.Now; // create post tags dynamic tags = post.CreateSuperColumn(); tags[0] = "Cassandra"; tags[1] = ".NET"; tags[2] = "Database"; tags[3] = "NoSQL"; // add properties to post post.Details = postDetails; post.Tags = tags; // attach the post to the database Console.WriteLine("attaching record"); db.Attach(post); // save the changes Console.WriteLine("saving changes"); db.SaveChanges(); // get the post back from the database Console.WriteLine("getting 'first-blog-post'"); dynamic getPost = family.Get("first-blog-post").FirstOrDefault(); // show details dynamic getPostDetails = getPost.Details; Console.WriteLine( String.Format("=={0} by {1}==\n{2}", getPostDetails.Title, getPostDetails.Author, getPostDetails.Body )); // show tags Console.Write("tags:"); foreach (var tag in getPost.Tags) Console.Write(String.Format("{0}:{1},", tag.ColumnName, tag.ColumnValue)); Console.WriteLine(); // get the comments family var commentsFamily = db.GetColumnFamily<TimeUUIDType, UTF8Type>("Comments"); dynamic postComments = commentsFamily.CreateRecord(key: "first-blog-post"); // lets attach it to the database before we add the comments db.Attach(postComments); // add 5 comments for (int i = 0; i < 5; i++) { dynamic comment = postComments.CreateSuperColumn(); comment.Name = i + " Nick Berardi"; comment.Email = i + " [email protected]"; comment.Website = i + " www.coderjournal.com"; comment.Comment = i + " Wow fluent cassandra is really great and easy to use."; postComments[GuidGenerator.GenerateTimeBasedGuid()] = comment; Console.WriteLine("Comment " + i + " Done"); Thread.Sleep(TimeSpan.FromSeconds(5)); } // save the comments db.SaveChanges(); DateTime lastDate = DateTime.Now; for (int page = 0; page < 2; page++) { // lets back the date off by a millisecond so we don't get paging overlaps lastDate = lastDate.AddMilliseconds(-1D); Console.WriteLine("Showing page " + page + " starting at " + lastDate.ToLocalTime()); var comments = commentsFamily.Get("first-blog-post") .Reverse() .Fetch(lastDate) .Take(3) .FirstOrDefault(); foreach (dynamic comment in comments) { var dateTime = GuidGenerator.GetDateTime((Guid)comment.ColumnName); Console.WriteLine(String.Format("{0:T} : {1} ({2} - {3})", dateTime.ToLocalTime(), comment.Name, comment.Email, comment.Website )); lastDate = dateTime; } } } Console.Read(); }
public _CassandraSetup() { var keyspaceName = "Testing"; var server = new Server("localhost"); if (!CassandraSession.KeyspaceExists(server, keyspaceName)) { CassandraSession.AddKeyspace(server, new KsDef { Name = "Testing", Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List <CfDef>() }); } var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Standard")) { keyspace.AddColumnFamily(server, new CfDef { Name = "Standard", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardAsciiType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardAsciiType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardBytesType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardBytesType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "BytesType", Comment = "Used for testing BytesType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardIntegerType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardIntegerType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "IntegerType", Comment = "Used for testing IntegerType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardLexicalUUIDType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLexicalUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LexicalUUIDType", Comment = "Used for testing LexicalUUIDType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardLongType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLongType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LongType", Comment = "Used for testing LongType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardTimeUUIDType")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardTimeUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "TimeUUIDType", Comment = "Used for testing TimeUUIDType family." }); } if (!keyspace.ColumnFamilyExists(server, "StandardUTF8Type")) { keyspace.AddColumnFamily(server, new CfDef { Name = "StandardUTF8Type", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "UTF8Type", Comment = "Used for testing UTF8Type family." }); } if (!keyspace.ColumnFamilyExists(server, "Super")) { keyspace.AddColumnFamily(server, new CfDef { Name = "Super", Keyspace = "Testing", Column_type = "Super", Comparator_type = "AsciiType", Subcomparator_type = "AsciiType", Comment = "Used for testing Super family." }); } DB = new CassandraContext(keyspaceName, server); DB.ThrowErrors = true; Family = DB.GetColumnFamily <AsciiType>("Standard"); SuperFamily = DB.GetColumnFamily <AsciiType, AsciiType>("Super"); Family.RemoveAllRows(); SuperFamily.RemoveAllRows(); Family.InsertColumn(TestKey1, "Test1", Math.PI); Family.InsertColumn(TestKey1, "Test2", Math.PI); Family.InsertColumn(TestKey1, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test3", Math.PI); Family.InsertColumn(TestKey2, "Test1", Math.PI); Family.InsertColumn(TestKey2, "Test2", Math.PI); Family.InsertColumn(TestKey2, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test3", Math.PI); }
public _CassandraSetup() { var keyspaceName = "Testing"; var server = new Server("localhost"); if (!CassandraSession.KeyspaceExists(server, keyspaceName)) CassandraSession.AddKeyspace(server, new KsDef { Name = "Testing", Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List<CfDef>() }); var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Standard")) keyspace.AddColumnFamily(server, new CfDef { Name = "Standard", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); if (!keyspace.ColumnFamilyExists(server, "StandardAsciiType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardAsciiType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "AsciiType", Comment = "Used for testing Standard family." }); if (!keyspace.ColumnFamilyExists(server, "StandardBytesType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardBytesType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "BytesType", Comment = "Used for testing BytesType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardIntegerType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardIntegerType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "IntegerType", Comment = "Used for testing IntegerType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardLexicalUUIDType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLexicalUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LexicalUUIDType", Comment = "Used for testing LexicalUUIDType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardLongType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardLongType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "LongType", Comment = "Used for testing LongType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardTimeUUIDType")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardTimeUUIDType", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "TimeUUIDType", Comment = "Used for testing TimeUUIDType family." }); if (!keyspace.ColumnFamilyExists(server, "StandardUTF8Type")) keyspace.AddColumnFamily(server, new CfDef { Name = "StandardUTF8Type", Keyspace = "Testing", Column_type = "Standard", Comparator_type = "UTF8Type", Comment = "Used for testing UTF8Type family." }); if (!keyspace.ColumnFamilyExists(server, "Super")) keyspace.AddColumnFamily(server, new CfDef { Name = "Super", Keyspace = "Testing", Column_type = "Super", Comparator_type = "AsciiType", Subcomparator_type = "AsciiType", Comment = "Used for testing Super family." }); DB = new CassandraContext(keyspaceName, server); DB.ThrowErrors = true; Family = DB.GetColumnFamily<AsciiType>("Standard"); SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super"); Family.RemoveAllRows(); SuperFamily.RemoveAllRows(); Family.InsertColumn(TestKey1, "Test1", Math.PI); Family.InsertColumn(TestKey1, "Test2", Math.PI); Family.InsertColumn(TestKey1, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey1, TestSuperName, "Test3", Math.PI); Family.InsertColumn(TestKey2, "Test1", Math.PI); Family.InsertColumn(TestKey2, "Test2", Math.PI); Family.InsertColumn(TestKey2, "Test3", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test1", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test2", Math.PI); SuperFamily.InsertColumn(TestKey2, TestSuperName, "Test3", Math.PI); }
private static void SetupKeyspace() { if (!CassandraSession.KeyspaceExists(server, keyspaceName)) CassandraSession.AddKeyspace(server, new KsDef { Name = keyspaceName, Replication_factor = 1, Strategy_class = "org.apache.cassandra.locator.SimpleStrategy", Cf_defs = new List<CfDef>() }); var keyspace = new CassandraKeyspace(keyspaceName); if (!keyspace.ColumnFamilyExists(server, "Posts")) keyspace.AddColumnFamily(server, new CfDef { Name = "Posts", Keyspace = keyspaceName, Column_type = "Super", Comparator_type = "UTF8Type", Subcomparator_type = "UTF8Type", Comment = "Used for blog posts." }); }