1
votes

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; }
}
1
When you execute this code, do you get exactly 1 record in the cf_Data table? Check this by running select count(*) from cf_Data in cqlsh. How do you generate id: Guid Id?oleksii
If you are doing this for performance reasons, you probably doing this wrong. It's generally faster to insert records one-by-one as inserts can hit multiple nodes in a cluster directly. In case of a batch inserts the request will hit a single coordinator node which will then redirect writes to other nodes.oleksii
Another note on performance; but you should not be using ALLOW FILTERING on a production system.Aaron
@oleksii: not getting any rows.107

1 Answers

6
votes

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.