这里有一个简单的问题,我遇到了问题,只有我发现的教程在我的 IDE(带有 Maven 的 Eclipse)中生成了很多错误。我现在可以读取 JSON 响应,但我想将它们写入(并最终转换)到我的 Cassandra DB。有人可以分享一些基本代码来帮助我开始这方面的工作吗?
最佳答案
datastax java-driver 包括几个使用 Jackson、Jsr353 或纯文本从 cassandra 写入/读取 JSON 的示例,可以在 driver-examples here 中找到它。 .
下面是来自 PlainTextJson.java 的示例,展示了如何作为字符串插入:
private static void insertWithCoreApi(Session session) {
// Bind in a simple statement:
session.execute("INSERT INTO examples.querybuilder_json JSON ?",
"{ \"id\": 1, \"name\": \"Mouse\",
\"specs\": { \"color\": \"silver\" } }");
// Bind in a prepared statement:
// (we use a local variable here for the sake of example, but in a real application you would cache and reuse
// the prepared statement)
PreparedStatement pst = session.prepare(
"INSERT INTO examples.querybuilder_json JSON :payload");
session.execute(pst.bind()
.setString("payload",
"{ \"id\": 2, \"name\": \"Keyboard\",
\"specs\": { \"layout\": \"qwerty\" } }"));
// fromJson lets you provide individual columns as JSON:
session.execute("INSERT INTO examples.querybuilder_json " +
"(id, name, specs) VALUES (?, ?, fromJson(?))",
3, "Screen", "{ \"size\": \"24-inch\" }");
}
关于java - 用于向 Cassandra 写入 JSON 响应的示例 Java 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667587/