使用 Datastax Java Driver 以 JSON 格式查询一行 [英] Using Datastax Java Driver to query a row as a JSON

查看:28
本文介绍了使用 Datastax Java Driver 以 JSON 格式查询一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 datastax java 驱动程序并将行检索为 JSON.

I am trying to use the datastax java driver and retrieve the row as a JSON.

我做经典SELECT JSON * from myTable WHERE id=1 这将在 CQL 上返回一个 Json 格式的字符串.

I do the classic SELECT JSON * from myTable WHERE id=1 and this returns a Json formatted string on CQL.

例如 { "uuid" : "12324567-...." }

这行得通.

现在,当我尝试使用 Java 驱动程序做同样的事情时,我使用(在 Scala 中)

Now when, I try to do the same use the Java driver, I use (in scala)

val resultSet = session.execute(queryString)

我使用:"resultSet.one()" 从这个结果集中选取一行.这有我需要的字符串,但我如何选择它?

I pick up one row from this result set using: "resultSet.one()". This has the string I need, but how do I pick this up?

实验:resultSet.one().getColumnDefinitions.toString

打印:Columns[[json] (varchar)]

实验:resultSet.one().toString()
打印:Row[{"uuid": "3ce19e07-2280-4b31-9475-992bda608e70"}] <- 我需要的字符串

Experiment: resultSet.one().toString()
Prints: Row[{"uuid": "3ce19e07-2280-4b31-9475-992bda608e70"}] <- String I need

如何在我的程序中选取代表 JSON 的简单字符串,而不尝试拆分上面的字符串?

How do I pick up a simple string that represents the JSON in my program, without trying to split the strings above ?

推荐答案

Cassandra 文档:

SELECT JSON 的结果将只包含一个名为 [json] 的列.此列将包含用于 INSERT JSON 的行的相同 JSON 编码地图表示.

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 类通过索引或通过名称:

In order to access the JSON value of the returned row, you need to use one of the getString methods defined on the Row class to get the value of this column either by index or by name:

Row row = resultSet.one();
String json1 = row.getString(0);
String json2 = row.getString("[json]");

这篇关于使用 Datastax Java Driver 以 JSON 格式查询一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆