0
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
4

1 回答 1

-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。目前在 Cassandra 中没有一个很好的方法来计算记录。最好的方法是启动 CQL 语句并运行

SELECT count(*) FROM <COLUMN FAMILY>

希望这可以帮助。我不知道您从该 Count 方法中获得了什么价值,但它肯定不是有意的。

于 2012-09-25T03:01:34.093 回答