只是轉換了QueryBuilder.update
到使用toString()
方法
如果要存儲,檢索和執行QueryBuilder.update
查詢字符串,你不必序列化和反序列化成JSON。
String query = QueryBuilder.update("exp").with(QueryBuilder.set("data", "This is a test data")).toString();
//Now you can store the text query directly to cassandra
//And retrieve the text query
session.execute(query); //Execute the query
這裏是toString()
@Override
public String toString() {
try {
if (forceNoValues)
return getQueryString();
// 1) try first with all values inlined (will not work if some values require custom codecs,
// or if the required codecs are registered in a different CodecRegistry instance than the default one)
return maybeAddSemicolon(buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE)).toString();
} catch (RuntimeException e1) {
// 2) try next with bind markers for all values to avoid usage of custom codecs
try {
return maybeAddSemicolon(buildQueryString(new ArrayList<Object>(), CodecRegistry.DEFAULT_INSTANCE)).toString();
} catch (RuntimeException e2) {
// Ugly but we have absolutely no context to get the registry from
return String.format("built query (could not generate with default codec registry: %s)", e2.getMessage());
}
}
}
的代碼,你可以看到,它返回的實際查詢字符串
是的,我想存儲,檢索並執行'com.datastax.driver.core.querybuilder.Update'查詢。 你的意思是使用'toString()'方法可以得到正確的文本查詢,然後直接執行它? 您確定這是**是否正確**複雜**更新操作? 我不知道'toString()'方法到底做了什麼。源代碼對我來說有點困難。 – niaomingjian
儘管我可以獲取文本查詢,但我可能會在'Update'中丟失一些其他信息。 – niaomingjian
@niaomingjian更新了答案,添加了'toString()'代碼。你可以看到它返回實際的查詢字符串。 –