使用Datastax Java驱动程序以JSON查询行 [英] Using Datastax Java Driver to query a row as a JSON

查看:128
本文介绍了使用Datastax Java驱动程序以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 ?

推荐答案

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值,您需要使用

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驱动程序以JSON查询行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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