以下函数将数据保存在cassandra
. 它调用abstract
rowToModel
在同一个类中定义的方法将返回的数据cassandra
转换为相应的数据模型。
def saveDataToDatabase(data:M):Option[M] = { //TODOM - should this also be part of the Repository trait?
println("inserting in table "+tablename+" with partition key "+partitionKeyColumns +" and values "+data)
val insertQuery = insertValues(tablename,data)
println("insert query is "+insertQuery)
try {
val resultSet:ResultSet = session.execute(insertQuery) //execute can take a Statement. Insert is derived from Statement so I can use Insert.
println("resultset after insert: " + resultSet)
println("resultset applied: " + resultSet.wasApplied())
println(s"columns definition ${resultSet.getColumnDefinitions}")
if(resultSet.wasApplied()){
println(s"saved row ${resultSet.one()}")
val savedData = rowToModel(resultSet.one())
Some(savedData)
} else {
None
}
}catch {
case e:Exception => {
println("cassandra exception "+e)
None
}
}
}
的abstract
rowToModel
定义如下
override def rowToModel(row: Row): PracticeQuestionTag = {
PracticeQuestionTag(row.getLong("year"),row.getLong("month"),row.getLong("creation_time_hour"),
row.getLong("creation_time_minute"),row.getUUID("question_id"),row.getString("question_description"))
}
但是我在其中定义的打印语句saveDataToDatabase
没有打印数据。我原以为打印件会打印出来,PracticeQuestionTag
但我看到的是以下内容
我希望看到这样的东西 -PracticeQuestionTag(2018,6,1,1,11111111-1111-1111-1111-111111111111,some description1)
当我one
从 ResultSet`打印时。但我看到的是
resultset after insert: ResultSet[ exhausted: false, Columns[[applied](boolean)]]
resultset applied: true
columns definition Columns[[applied](boolean)]
saved row Row[true]
row to Model called for row null
cassandra exception java.lang.NullPointerException
为什么ResultSet
,one
并且columnDefinitions
没有显示数据模型中的值?
这是设计使然。插入的结果集将只包含一行,表明是否应用了结果。
执行条件语句时,ResultSet 将包含一个单行,其中有一列名为“applied”的布尔类型。这表明条件语句是否成功。
这也是有道理的,因为 ResultSet 应该返回查询的结果,以及为什么要通过重新调整结果集中的所有输入来使结果集对象变重。可以在此处找到更多详细信息。
当然 Get 查询会有详细的结果集。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句