Using Datastax Java Driver to query a row as a JSON

南笙酒味 提交于 2019-12-13 13:25:45

问题


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

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

e.g { "uuid" : "12324567-...." }

This works.

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

val resultSet = session.execute(queryString)

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?

Experiment: resultSet.one().getColumnDefinitions.toString

Prints: Columns[ [json] (varchar) ]

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

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


回答1:


As noted in the the 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.

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]");


来源:https://stackoverflow.com/questions/34521108/using-datastax-java-driver-to-query-a-row-as-a-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!