FluentCassandra / Cassandra是否丢失了我的数据?

时间:2012-08-16 15:43:07

标签: cassandra fluentcassandra

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentCassandra;

namespace ConsoleApplication2
{
        class Program
        {
                static void Main(string[] args)
                {
                        var session = new CassandraSession("Tester", "localhost");

                        CreateData(session);
                        SelectData(session);
                        Console.WriteLine("Done");
                        Console.ReadLine();
                }

                private static void SelectData(CassandraSession session)
                {
                        using (var db = new CassandraContext(session))
                        {
                                var users = db.GetColumnFamily("Users2");
                                var count = users.AsObjectQueryable<User>().Count();
                                Console.WriteLine("Counted: " + count.ToString());
                        }
                }

                private static void CreateData(CassandraSession session)
                {
                        var startTime = DateTime.UtcNow;
                        for (int index = 0; index < 11 * 1000; index++)
                        {
                                using (var db = new CassandraContext(session))
                                {
                                        var users = db.GetColumnFamily("Users2");
                                        dynamic user = users.CreateRecord(index);
                                        user.Name = "UserX" + index.ToString();
                                        user.Password = "PasswordX" + index.ToString();
                                        db.Attach(user);
                                        db.SaveChanges();
                                        if (index % 1000 == 0)
                                        {
                                                var taken = DateTime.UtcNow - startTime;
                                                Console.WriteLine(string.Format("{0} thousand in {1} seconds", (index / 1000) + 1, taken.TotalSeconds));
                                        }
                                }
                        }
                }
        }

        class User
        {
                public string Name { get; set; }
                public string Text { get; set; }
        }
}

此应用的输出是

1 thousand in 0.2760158 seconds
2 thousand in 0.8180468 seconds
3 thousand in 1.3700784 seconds
4 thousand in 1.8971085 seconds
5 thousand in 2.4091378 seconds
6 thousand in 3.0791761 seconds
7 thousand in 3.6002059 seconds
8 thousand in 4.126236 seconds
9 thousand in 4.8292762 seconds
10 thousand in 5.3513061 seconds
11 thousand in 5.8543349 seconds
Counted: 9018
Done

1 个答案:

答案 0 :(得分:-1)

此代码无法正常工作,我应该知道,我是FluentCassandra的开发人员。

         using (var db = new CassandraContext(session))
         {
              var users = db.GetColumnFamily("Users2");
              var count = users.AsObjectQueryable<User>().Count();
              Console.WriteLine("Counted: " + count.ToString());
         }

您正在使用LINQ进行计数,但尚未在LINQ支持中实现Count。现在,没有很好的方法可以统计卡桑德拉的记录。最好的方法是启动CQL语句并运行

SELECT count(*) FROM <COLUMN FAMILY>

希望这会有所帮助。我不知道你从那个Count方法得到了什么价值,但肯定不是什么意思。