cassandra data ingestion feature

98 views
Skip to first unread message

Jagadeesh N M

unread,
May 11, 2021, 5:25:18 AM5/11/21
to DataStax Java Driver for Apache Cassandra User Mailing List
helllo,

i am using cassandra java driver 4.9.0 i want to insert list of objects in one go from my dao , i could see with current implementation @Insert will accept only a single object in a method signature is it possible to incorporate inserting list of objects? please help me understand.

Alexandre Dutra

unread,
May 25, 2021, 7:47:50 AM5/25/21
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi,

First of all, some context: if you are looking for atomicity guarantees, you should know that atomically inserting multiple rows in one single statement is only possible in Cassandra if you use a BATCH statement. You can read more about BATCH statements in the driver here:


However, batches are not always a good idea, and often do not yield any noticeable performance gain. The following articles should give you a good picture of their pros and cons:


This is why, in the driver object mapper, @Insert methods can only insert one row at a time: they generate simple INSERT statements. I'd recommend that you stick with that and simply call your @Insert method multiple times, one for each row to insert.

If you really, really want to use BATCH statements with the driver object mapper framework, you could do so by using @Query or @QueryProvider methods:


But again, I wouldn't recommend doing that unless you benchmark your options.

And finally, if all you need is a syntactic sugar to generate INSERT statements for a List of entities, you can do something like below:

@Dao
public interface FooDao {

    @Insert
    void insertFoo(Foo foo);
   
    default void insertFoos(List<Foo> foos) {
      foos.forEach(this::insertFoo);
    }
}

But in the above example, the insertion of all rows is not atomic in any way (and could fail mid-course).

Hope that helps.

Alex Dutra

On Tue, May 11, 2021 at 11:25 AM Jagadeesh N M <jagadee...@gmail.com> wrote:
helllo,

i am using cassandra java driver 4.9.0 i want to insert list of objects in one go from my dao , i could see with current implementation @Insert will accept only a single object in a method signature is it possible to incorporate inserting list of objects? please help me understand.

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
To view this discussion on the web visit https://groups.google.com/a/lists.datastax.com/d/msgid/java-driver-user/8ee102d5-cb9f-4444-8a7a-a78ad769cdc0n%40lists.datastax.com.


--
Alexandre Dutra
Software Engineer, DataStax
Reply all
Reply to author
Forward
0 new messages