[英]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) 我正在使用Apache Spark 2.0,Apache Cassandra 3.7和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. 我想基于键列值从Cassandra中的表中删除几行。 How do I do that using Dataset and using Apache Spark Java Connector for Cassandra?
如何使用数据集和适用于Cassandra的Apache Spark Java Connector来做到这一点? I am using SparkSession in my code.
我在代码中使用SparkSession。 Please suggest.
请提出建议。 If there is any other way to do this then let me know that.
如果还有其他方法可以做到,请告诉我。 I want to do it using Java.
我想用Java做。
Thank you. 谢谢。
Functionality deleteFromCassandra()
for deleting of Cassandra records is coming in new Cassandra Connector release. 新的Cassandra Connector版本中提供了用于删除Cassandra记录的功能
deleteFromCassandra()
。 please check SPARKC-349 and SPARKC-392 for more details. 请检查SPARKC-349和SPARKC-392了解更多详细信息。
For deleting row from Cassandra using Cassandra Connector you can do something like below. 要使用Cassandra Connector从Cassandra中删除行,您可以执行以下操作。 Say for example I have columns like
id UUID PRIMARY KEY, username TEXT
in my table. 举例来说,我的表格中有
id UUID PRIMARY KEY, username TEXT
的列。 Now I want to delete all the rows where username equals to "Mat". 现在,我要删除用户名等于“ Mat”的所有行。 To do this get Session from Cassandra Connector and execute delete query.
为此,请从Cassandra Connector获取Session并执行删除查询。
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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.