简体   繁体   中英

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.

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();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM