使用 Datastax Java 驱动程序将行查询为 JSON
Using Datastax Java Driver to query a row as a JSON
我正在尝试使用 datastax java 驱动程序并将该行检索为 JSON。
我做经典
SELECT JSON * from myTable WHERE id=1
和这个 returns CQL 上的 Json 格式化字符串。
例如{ "uuid" : "12324567-...." }
这有效。
现在,我尝试使用 Java 驱动程序做同样的事情,我使用(在 scala 中)
val resultSet = session.execute(queryString)
我使用以下方法从该结果集中选取一行:"resultSet.one()"
。
这有我需要的字符串,但我该如何获取它?
实验:resultSet.one().getColumnDefinitions.toString
打印:Columns[ [json] (varchar) ]
实验:resultSet.one().toString()
打印:Row[{"uuid": "3ce19e07-2280-4b31-9475-992bda608e70"}]
<- 我需要的字符串
如何在我的程序中选取代表 JSON 的简单字符串,而不尝试拆分上面的字符串?
如 Cassandra documentation 中所述:
The results for SELECT JSON
will only include a single column named [json]
. This column will contain the same JSON-encoded map representation of a row that is used for INSERT JSON
.
为了访问返回行的 JSON 值,您需要使用 Row
class 上定义的 getString
方法之一来获取此列的值(按索引或按名称):
Row row = resultSet.one();
String json1 = row.getString(0);
String json2 = row.getString("[json]");
我正在尝试使用 datastax java 驱动程序并将该行检索为 JSON。
我做经典
SELECT JSON * from myTable WHERE id=1
和这个 returns CQL 上的 Json 格式化字符串。
例如{ "uuid" : "12324567-...." }
这有效。
现在,我尝试使用 Java 驱动程序做同样的事情,我使用(在 scala 中)
val resultSet = session.execute(queryString)
我使用以下方法从该结果集中选取一行:"resultSet.one()"
。
这有我需要的字符串,但我该如何获取它?
实验:resultSet.one().getColumnDefinitions.toString
打印:Columns[ [json] (varchar) ]
实验:resultSet.one().toString()
打印:Row[{"uuid": "3ce19e07-2280-4b31-9475-992bda608e70"}]
<- 我需要的字符串
如何在我的程序中选取代表 JSON 的简单字符串,而不尝试拆分上面的字符串?
如 Cassandra documentation 中所述:
The results for
SELECT JSON
will only include a single column named[json]
. This column will contain the same JSON-encoded map representation of a row that is used forINSERT JSON
.
为了访问返回行的 JSON 值,您需要使用 Row
class 上定义的 getString
方法之一来获取此列的值(按索引或按名称):
Row row = resultSet.one();
String json1 = row.getString(0);
String json2 = row.getString("[json]");