Shashikant Kulkarni
Shashikant Kulkarni

Reputation: 177

Delete rows from Cassandra DB using Apache Spark Java connector for Cassandra using key columns

I am using Apache Spark 2.0, Apache Cassandra 3.7 and Apache Spark Java Connector for Cassandra 2.11 (2.0.0-M3)

I want to delete few rows from table in Cassandra based on key column values. How do I do that using Dataset and using Apache Spark Java Connector for Cassandra? I am using SparkSession in my code. Please suggest. If there is any other way to do this then let me know that. I want to do it using Java.

Thank you.

Upvotes: 0

Views: 1889

Answers (1)

abaghel
abaghel

Reputation: 15297

Functionality deleteFromCassandra() for deleting of Cassandra records is coming in new Cassandra Connector release. please check SPARKC-349 and SPARKC-392 for more details.

For deleting row from Cassandra using Cassandra Connector you can do something like below. Say for example I have columns like id UUID PRIMARY KEY, username TEXT in my table. Now I want to delete all the rows where username equals to "Mat". To do this get Session from Cassandra Connector and execute delete query.

dataset.where(dataset.col("username").equalTo("Mat")).foreachPartition(partition -> {
Session session = connector.openSession();
        while (partition.hasNext()) {
            Row row = partition.next();
            String id = (String) row.get(0);//UUID is at index 0
            String delete = "DELETE FROM mykeyspace.mytable where id=" + id + ";";
            session.execute(delete);
        }
        session.close();
    });

Upvotes: 2

Related Questions