本文整理汇总了C#中CassandraContext.Attach方法的典型用法代码示例。如果您正苦于以下问题:C# CassandraContext.Attach方法的具体用法?C# CassandraContext.Attach怎么用?C# CassandraContext.Attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CassandraContext
的用法示例。
在下文中一共展示了CassandraContext.Attach方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateComments
private static void CreateComments()
{
using (var db = new CassandraContext(keyspace: keyspaceName, server: server))
{
var key = "first-blog-post";
// get the comments family
var commentsFamily = db.GetColumnFamily<TimeUUIDType, UTF8Type>("Comments");
ConsoleHeader("create comments");
dynamic postComments = commentsFamily.CreateRecord(key: key);
// 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 = "Nick Berardi";
comment.Email = "[email protected]";
comment.Website = "www.coderjournal.com";
comment.Comment = "Wow fluent cassandra is really great and easy to use.";
var commentPostedOn = DateTime.Now;
postComments[commentPostedOn] = comment;
Console.WriteLine("Comment " + (i + 1) + " Posted On " + commentPostedOn.ToLongTimeString());
Thread.Sleep(TimeSpan.FromSeconds(2));
}
// save the comments
db.SaveChanges();
}
}
示例2: DoWork
private static void DoWork()
{
using (var db = new CassandraContext(keyspace: keyspaceName, server: server))
{
Stopwatch watch = new Stopwatch();
watch.Start();
Random random = new Random();
byte[] data = new byte[dataLength];
random.NextBytes(data);
int errors = 0;
for (int i = 0; i < count; i++)
{
// Insert
Guid postId = Guid.NewGuid();
string titleName = i.ToString();
var family = db.GetColumnFamily<UTF8Type, UTF8Type>("Posts");
dynamic post = family.CreateRecord(postId);
dynamic details = post.CreateSuperColumn();
details.Body = data;
post[DateTime.Now] = details;
db.Attach(post);
db.SaveChanges();
}
double rate = count / watch.Elapsed.TotalSeconds;
double throughput = rate * data.Length;
Console.WriteLine("Completed: " + watch.Elapsed + "\tRate: " + rate + "\tThroughput: " + throughput + "\tErrors:" + errors);
}
}
示例3: CreateComments
private static void CreateComments()
{
using (var db = new CassandraContext(keyspace: KeyspaceName, server: Server))
{
var key = "first-blog-post";
// get the comments family
var commentsFamily = db.GetSuperColumnFamily("Comments");
ConsoleHeader("create comments");
var postComments = commentsFamily.CreateRecord(key: key);
// lets attach it to the database before we add the comments
db.Attach(postComments);
var dt = new DateTime(2010, 11, 29, 5, 03, 00, DateTimeKind.Local);
// add 5 comments
for (int i = 0; i < 5; i++)
{
var comment = postComments.CreateSuperColumn();
comment["Name"] = "Nick Berardi";
comment["Email"] = "[email protected]";
// you can also use it as a dynamic object
dynamic dcomment = comment;
dcomment.Website = "www.coderjournal.com";
dcomment.Comment = "Wow fluent cassandra is really great and easy to use.";
var commentPostedOn = dt;
postComments[commentPostedOn] = comment;
Console.WriteLine("Comment " + (i + 1) + " Posted On " + commentPostedOn.ToLongTimeString());
dt = dt.AddMinutes(2);
}
// save the comments
db.SaveChanges();
}
}
示例4: UpdateFirstPost
private static void UpdateFirstPost()
{
using (var db = new CassandraContext(keyspace: KeyspaceName, server: Server))
{
var key = "first-blog-post";
var postFamily = db.GetColumnFamily("Posts");
// get the post back from the database
ConsoleHeader("getting 'first-blog-post' for update");
// query using API
dynamic post = postFamily.Get(key).FirstOrDefault();
post.Title = post.Title + "(updated)";
post.Body = post.Body + "(updated)";
post.Author = post.Author + "(updated)";
post.PostedOn = DateTimeOffset.Now;
// attach the post to the database
ConsoleHeader("attaching record");
db.Attach(post);
// save the changes
ConsoleHeader("saving changes");
db.SaveChanges();
}
}
示例5: CreateSecondPost
private static void CreateSecondPost()
{
using (var db = new CassandraContext(keyspace: KeyspaceName, server: Server))
{
var key = "second-blog-post";
var postFamily = db.GetColumnFamily("Posts");
var tagsFamily = db.GetColumnFamily("Tags");
// create post
ConsoleHeader("create post");
dynamic post = postFamily.CreateRecord(key: key);
post.Title = "My Second Cassandra Post";
post.Body = "Blah. Blah. Blah. about my second post on how great Cassandra is to work with.";
post.Author = "Nick Berardi";
post.PostedOn = DateTimeOffset.Now;
// create tags
ConsoleHeader("create post tags");
dynamic tags = tagsFamily.CreateRecord(key: key);
tags[0] = "Cassandra";
tags[1] = ".NET";
tags[2] = "Database";
tags[3] = "NoSQL";
// attach the post to the database
ConsoleHeader("attaching record");
db.Attach(post);
db.Attach(tags);
// save the changes
ConsoleHeader("saving changes");
db.SaveChanges();
}
}
示例6: Main
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";
//.........这里部分代码省略.........
示例7: CassandraDatabaseSetup
public CassandraDatabaseSetup(bool volitile = false)
{
var keyspaceName = "Testing";
var server = new Server("localhost");
DB = new CassandraContext(keyspaceName, server);
DB.ThrowErrors = true;
var exists = DB.KeyspaceExists(keyspaceName);
Users = new[] {
new User { Id = 1, Name = "Darren Gemmell", Email = "[email protected]", Age = 32 },
new User { Id = 2, Name = "Fernando Laubscher", Email = "[email protected]", Age = 23 },
new User { Id = 3, Name = "Cody Millhouse", Email = "[email protected]", Age = 56 },
new User { Id = 4, Name = "Emilia Thibert", Email = "[email protected]", Age = 67 },
new User { Id = 5, Name = "Allyson Schurr", Email = "[email protected]", Age = 21 }
};
Family = DB.GetColumnFamily<AsciiType>("Standard");
SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super");
UserFamily = DB.GetColumnFamily("Users");
if (exists && !volitile)
return;
using (var session = DB.OpenSession())
{
if (exists)
DB.DropKeyspace(keyspaceName);
var keyspace = new CassandraKeyspace(new CassandraKeyspaceSchema { Name = keyspaceName, Strategy = CassandraKeyspaceSchema.ReplicaPlacementStrategySimple, ReplicationFactor = 1 }, DB);
DB.Keyspace = keyspace;
keyspace.TryCreateSelf();
keyspace.TryCreateColumnFamily<AsciiType>("Standard");
keyspace.TryCreateColumnFamily<AsciiType, AsciiType>("Super");
keyspace.TryCreateColumnFamily<AsciiType>("StandardAsciiType");
keyspace.TryCreateColumnFamily<BytesType>("StandardBytesType");
keyspace.TryCreateColumnFamily<IntegerType>("StandardIntegerType");
keyspace.TryCreateColumnFamily<LexicalUUIDType>("StandardLexicalUUIDType");
keyspace.TryCreateColumnFamily<LongType>("StandardLongType");
keyspace.TryCreateColumnFamily<TimeUUIDType>("StandardTimeUUIDType");
keyspace.TryCreateColumnFamily<UTF8Type>("StandardUTF8Type");
keyspace.TryCreateColumnFamily<UUIDType>("StandardUUIDType");
keyspace.TryCreateColumnFamily<CompositeType<LongType, UTF8Type>>("StandardCompositeType");
keyspace.TryCreateColumnFamily<DynamicCompositeType>("StandardDynamicCompositeType");
Family = DB.GetColumnFamily<AsciiType>("Standard");
SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super");
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);
DB.ExecuteNonQuery(@"
CREATE COLUMNFAMILY Users (
KEY int PRIMARY KEY,
Name ascii,
Email ascii,
Age int
);");
DB.ExecuteNonQuery(@"CREATE INDEX User_Age ON Users (Age);");
DB.Keyspace.ClearCachedKeyspaceSchema();
UserFamily = DB.GetColumnFamily("Users");
foreach (var user in Users)
{
dynamic record = UserFamily.CreateRecord(user.Id);
record.Name = user.Name;
record.Email = user.Email;
record.Age = user.Age;
DB.Attach(record);
}
DB.SaveChanges();
}
}