2015-05-26 35 views
1

我一直在嘗試使用使用批處理.Trying datastax卡桑德拉C#駕駛員在batch.Code插入100行工作正常,將數據插入到Cassandra的密鑰空間插入數據,但是當我檢查柱族沒有數據。請提出爲什麼有人知道數據插入不工作的原因?如果有任何異常,爲什麼catch無法獲得該異常? 有同時通過cqlsh命令行插入數據沒有問題。Datastax卡桑德拉C#驅動程序不是通過批處理

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命名空間。 映射器類代碼:

[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; } 
} 
+0

當你執行這個代碼,你得到完全記錄1在'cf_Data'表?通過在'cqlsh'中運行'select count(*)from cf_Data'來檢查。你如何生成id:'Guid Id'? – oleksii

+2

如果你這樣做是出於性能的考慮,你可能[這樣做不對](https://lostechies.com/ryansvihla/2014/08/28/cassandra-batch-loading-without-the-batch-keyword/)。一次插入記錄通常會更快,因爲插入可以直接打擊羣集中的多個節點。在批量插入的情況下,請求將命中一個協調器節點,然後該節點會將寫入重定向到其他節點。 – oleksii

+0

關於表演的另一個說明;但你不應該在生產系統上使用ALLOW FILTERING。 – Aaron

回答

6

如果要執行包含一些查詢批,你應該叫Batch.Execute()一次。

在你的情況,這將是:

var batch = session.CreateBatch(); 
foreach (var val in lsData) 
{ 
    batch.Append(table.Insert(val)); 
} 
batch.Execute(); 

也就是說,爲「批量」插入,使用批處理是不是最好的方法。如果你做單一插入,速度會更快。

你應該閱讀@oleksii建議後:https://lostechies.com/ryansvihla/2014/08/28/cassandra-batch-loading-without-the-batch-keyword/

您可以閱讀DataStax C# driver documentation for the Linq component

+0

批次可能不是理想的性能,但它必須將數據插入列族。我懷疑一些數據驗證問題,但仍然有一些例外必須拋出。 – 107

+0

以下鏈接提示批次準備語句是更快https://github.com/davidtinker/cassandra-perf – 107

相關問題