这里的简单问题我遇到了问题,只有我发现的教程在我的IDE(Eclipse with Maven)中产生了很多错误。我现在可以阅读我的JSON响应,但我想将它们写入(并最终将它们转换)给我的Cassandra DB。任何人都有一些他们可以分享的基本代码让我开始这个?
答案 0 :(得分:1)
datastax java-driver包含几个用于使用Jackson,Jsr353或纯文本从cassandra写入/读取JSON的示例,它可以在驱动程序示例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\" }");
}