我一直在尝试使用批处理cassandra c#驱动程序将数据插入cassandra键空间 . 使用批处理插入100行 . 编码工作正常但是当我检查列族时没有数据 . 如果有人知道,请建议为什么数据插入不起作用?如果有任何异常,为什么catch无法获得该异常?通过cqlsh命令行插入数据时没有问题 .
private static void InsertData(ISession session, List<cf_Data> lsData)
{
try
{
var table = session.GetTable<cf_Data>();
table.CreateIfNotExists();
int count = 0;
var batch =session.CreateBatch();;
foreach (cf_Data val in lsData)
{
try
{
if (((count) % 100) == 1)
{
batch = session.CreateBatch();
}
batch.Append(table.Insert(val));
if (count % 100 == 0)
{
batch.Execute();
}
}
catch (Exception)
{
throw;
}
count++;
}
}
catch (Exception)
{
throw;
}
}
为了将C#类映射到Cassandra Column Family,使用了Cassandra.Mapper命名空间 . Mapper类代码:
[AllowFiltering]
[Table("cf_Data ")]
internal class cf_Data
{
[PartitionKey]
public Guid Id { get; set; }
public DateTimeOffset Rundate { get; set; }
public DateTimeOffset OtherDate{ get; set; }
public String StudentFirstName { get; set; }
public String StudentLastName { get; set; }
}
1 回答
如果要执行包含某些查询的批处理,则应调用
Batch.Execute()
一次 .在你的情况下,它将是:
那就是说, for "Bulk" insertions, using Batch is not the best approach . 如果进行单次插入,速度会更快 .
你应该阅读@oleksii建议的帖子:https://lostechies.com/ryansvihla/2014/08/28/cassandra-batch-loading-without-the-batch-keyword/
你可以阅读DataStax C# driver documentation for the Linq component .