This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Program.cs
160 lines (130 loc) · 4.48 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Apache.Cassandra;
using FluentCassandra.Connections;
using FluentCassandra.Types;
namespace FluentCassandra.Sandbox
{
internal class Program
{
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 + " nick@coderjournal.com";
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();
}
}
}