Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:使用Cassandra数据库缓慢插入_C#_.net_Cassandra_Cql - Fatal编程技术网

C#:使用Cassandra数据库缓慢插入

C#:使用Cassandra数据库缓慢插入,c#,.net,cassandra,cql,C#,.net,Cassandra,Cql,我正在尝试将数百万条记录,甚至可能是数十亿条记录插入卡桑德拉数据库。有没有更快的办法?每个文件需要30分钟,我有100多个文件。我需要遍历某个目录中的每个文件,遍历所有行,然后将每个文件的每一行插入Cassandra数据库。这些文件的最大大小都在1KB到300000KB之间 我指的是9734KB,已经处理了30分钟没有完成。肯定有更快的插入记录的方法吗?其处理的文件有942345行 以这种速度插入所有这些记录需要几天时间 使用批处理和不使用批处理进行试验,两者的速度(大致相同) 您不需要每次使用

我正在尝试将数百万条记录,甚至可能是数十亿条记录插入卡桑德拉数据库。有没有更快的办法?每个文件需要30分钟,我有100多个文件。我需要遍历某个目录中的每个文件,遍历所有行,然后将每个文件的每一行插入Cassandra数据库。这些文件的最大大小都在1KB到300000KB之间

我指的是9734KB,已经处理了30分钟没有完成。肯定有更快的插入记录的方法吗?其处理的文件有942345行

以这种速度插入所有这些记录需要几天时间

使用批处理和不使用批处理进行试验,两者的速度(大致相同)


您不需要每次使用该语句时都准备它。您应该准备一次,并为每个插入操作绑定

此外,您还应该按照建议将关注点分离出来。您将能够隔离创建多个列表和从这些列表开始删除的成本


您不需要批处理,因为批处理将为您提供您不需要的事务性保证。如果不知道您的模式是什么样子,就很难知道确切的影响是什么。看看


还要记住,您可以并行地向cassandra发送多个insert操作。

您能在代码中分离关注点并将结果发布到这里吗?也就是说,将i/o操作与用于连接和写入cassandra的数据库分离?您不需要批处理,因为批处理将为您提供不需要的事务性保证。只需将所有插入内容与准备好的语句一起单独发送即可。
Console.CursorVisible = false;

var cluster = Cluster.Builder().AddContactPoints("127.0.0.1").Build();
var session = cluster.Connect("cracking");

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Connected to the Cassandra Database");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;

string filepath = @"C:\Users\admin\Desktop\wecrack lists\test";
DirectoryInfo directory = new DirectoryInfo(filepath);

int fileCount = 0;

foreach (var file in directory.GetFiles("*"))
{
    fileCount++;

    Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Working through file: {" + file + "} {" + fileCount + "/" + directory.GetFiles("*").Count() + "}");

    var lines = File.ReadLines(filepath + @"\" + file.ToString()).ToList();

    var batch = new BatchStatement();

    int lineCount = 0;

    while (lines.Count > 0)
    {
        foreach (string line in lines.ToList())
        {
            if (lineCount >= 2000)
            {
                lineCount = 0;

                Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Changing batch for file: {" + file + "} {" + fileCount + "/" + directory.GetFiles("*").Count() + "}");
                session.Execute(batch);
                batch = new BatchStatement();
                break;
            }

            lineCount++;
            lines.Remove(line);

            var userTrackStmt = session.Prepare("INSERT INTO passwords (id, password) VALUES (?, ?)");
            batch.Add(userTrackStmt.Bind(Guid.NewGuid(), line));
        }
    }
}

Console.WriteLine();
Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Finished inserting records, press any key to get the count.");
Console.ReadKey(true);

Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("  " + string.Format("{0:n0}", session.Execute("SELECT * FROM passwords").Count()) + " records.");

while (true)
{
    Console.ReadKey(true);
}