如何在 INSERT 之后获取插入的数据......如果不存在

How to get inserted data after INSERT …. IF NOT EXISTS

我有一个 scala 代码可以使用带有 datastax 驱动程序的 cassandra。我想使用 INSERT …. IF NOT EXISTS 而不是 SELECT and INSERT。在INSERT之后我找不到正确的方法得到INSERTED INFORMATION,session.executereturns只有我FALSE or TRUE。你能帮我写一段代码,让我在插入带有这样一个键的行时获取数据吗?

连接代码:

  private val query=
    s"INSERT INTO ${keyspace}.${table} (code,date) VALUES (?, ?) IF NOT EXISTS"

  override def insertIfNotExist(code: String, date: String): Boolean = {
    connector.withSessionDo { session =>
      val prep = connector.prepare(session, query)
      val res: Boolean = session.execute(prep.bind(code, date) // return only boolean

      ).wasApplied()
    }
  }


insert into date_table(code, date) VALUES ('5', '2028-01-01') if not exists;

如果行在 table 中,控制台中的响应是:

[applied]| code| date
false,5,2021-11-15

我想在 Scala 代码中访问 5,2021-11-15

if not only
[applied]
true

不,对 session.execute 的调用总是 returns ResultSet,它只是 wasApplied returns 布尔值。如果想了解更多详情,可以改成:

val res: Boolean = session.execute(prep.bind(code, date))
if (res.wasApplied) {
  // handle applied case - in this case you get only the boolean value
} else {
  // handle not-applied - in this case you can extract information
  // about conflicting values
}