Reputation: 550
I have been trying to insert data into cassandra keyspace using datastax cassandra c# driver using batch .Trying to insert 100 rows in a batch.Code is working fine but when i check the column family there is no data. Please suggest why data insertion is not working if anyone knows? If there is any exception why catch is unable to get that exception ? There is no issue while inserting data through cqlsh command line.
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;
}
}
For mapping C# class to Cassandra Column Family, Cassandra.Mapper namespace is used. Mapper class code :
[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; }
}
Upvotes: 1
Views: 2014
Reputation: 6600
If you want to execute a batch containing some queries, you should call Batch.Execute()
once.
In your case, it would be:
var batch = session.CreateBatch();
foreach (var val in lsData)
{
batch.Append(table.Insert(val));
}
batch.Execute();
That said, for "Bulk" insertions, using Batch is not the best approach. It will be faster if you do single inserts.
You should read the post suggested by @oleksii: https://lostechies.com/ryansvihla/2014/08/28/cassandra-batch-loading-without-the-batch-keyword/
You can read the DataStax C# driver documentation for the Linq component.
Upvotes: 6