Converting ExecutionResult object to json - java

I need to expose a public API, and i need to convert the result of a cypher query into json. I have begun the process, but i am having problems serializing an object of type scala.collection.convert.Wrappers$SeqWrapper that gets returned when using collect() in cypher.
Here is the cypher query:
MATCH (orders:Orders {id:'locationrestaurant'}), (order:Order), (orders)-[:hasOrder]-(order),(order)-[:orderedProduct]->(product),(client)-[:ordered]->(order) return (order),(client), collect(product) as products;
How can i handle this type of object? Can i cast it to a List? Also, are there any libraries for converting ExecutionResult to json?
If you need any more details, please ask. Here is my code
public QueryResult runCypher(String query, Map<String,Object> params)
{
QueryResult result = new QueryResult();
Transaction tx = service.beginTx();
ExecutionResult execResult = null;
boolean success = true;
System.out.println(query);
try
{
if(params!=null) execResult = engine.execute(query, params);
else execResult = engine.execute(query);
result.result = getReturnedObjectsToJson(execResult);
}
catch(Exception e)
{
System.out.println(e.getMessage()+" exception message");
result.result = e.getMessage();
success = false;
}
finally
{
if(success) tx.success();
else tx.failure();
}
tx.close();
result.success = success;
return result;
}
Basically, getReturnedObjectsToJson does the work.

How can i handle this type of object?
data.get("labels") instanceof java.util.Collection
Can i cast it to a List?
yes
Also, are there any libraries for converting ExecutionResult to json?
ExecutionResult is iterable, i think you can use any popular java json framework, for example gson or jackson

You can use Jacksons databind framework. That will allow you to turn it back into an execution result at the destination.
If you want to get the actual graph back from an execution result so that the Json is in some kind of graph schema. That is a lot more work.
I have been writing a library that will parse arbitary graphs into Cypher and execution results back into graphs to work with a virtual query builder, and it is reasonably hard work. Hopefully it will all be working soon. :)

Related

Jooq dsl one to many relations

I'm using Spring Data + Jooq DSL. As result entity I'm using not jooq generated entity, but simple one, without any annotations and for relations One To Many getting result:
[{
"id":2,
"name":"James",
"addresses":[
{
"id":null,
"country":null,
"street":null
}
]
}]
Is any way to return an empty array for addresses?
My code to perform a request:
public Set<User> getUserById(Set<Long> id) {
Set<User> result = new HashSet<>();
ResultQuery users = dsl.select(
field("u.id", Long.class).as("id"),
field("u.name", String.class).as("name"),
field("a.id", Long.class).as("addresses_id"),
field("a.country", String.class).as("addresses_country"),
field("a.street", String.class).as("addresses_street")
).from("schema.user_table u")
.leftJoin("schema.address_table a")
.on("u.id = a.user_id")
.where(field("u.id").in(id));
try(ResultSet rs = users.fetchResultSet()) {
JdbcMapper<User> mapper = JdbcMapperFactory
.newInstance()
.addKeys("id")
.newMapper(User.class);
result = mapper.stream(rs).collect(Collectors.toSet());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
Why not just use SQL/JSON to produce JSON documents directly from within your database?
public String getUserById(Set<Long> id) {
return dsl.select(coalesce(jsonArrayAgg(
jsonObject(
key("id").value(field("u.id", Long.class)),
key("name").value(field("u.name", String.class)),
key("addresses").value(coalesce(
jsonArrayAgg(jsonObject(
key("id").value(field("a.id", Long.class)),
key("country").value(field("a.country", String.class)),
key("street").value(field("a.street", String.class))
)),
jsonArray()
))
),
jsonArray()
)))
.from("schema.user_table u")
.leftJoin("schema.address_table a")
.on("u.id = a.user_id")
.where(field("u.id").in(id))
.fetchSingle().value1().data();
}
If you really need the intermediate User representation, then you can either:
Use Jackson or Gson to map the JSON document to the nested User DTO structure using reflection (works with jOOQ 3.14)
Use jOOQ 3.15's new MULTISET value constructor operator or MULTISET_AGG aggregate function along with ad-hoc converters, see below:
public Set<User> getUserById(Set<Long> id) {
return dsl.select(
field("u.id", Long.class),
field("u.name", String.class),
multisetAgg(
field("a.id", Long.class),
field("a.country", String.class),
field("a.street", String.class)
).convertFrom(r -> r == null
? Collections.<Address>emptyList()
: r.map(Records.mapping(Address::new)))
)
.from("schema.user_table u")
.leftJoin("schema.address_table a")
.on("u.id = a.user_id")
.where(field("u.id").in(id))
.fetchSet(Records.mapping(User::new));
}
Side note on code generation and execution
While not strictly relevant to this question, unless your schema is dynamic (not known at compile time), I really urge you to reconsider using source code generation. If you're not using it, you're missing out on a lot of jOOQ API advantages, just like when you're executing a jOOQ query with something other than jOOQ.
For me worked specify as a key addressId:
.addKeys("id", "addresses_id")

Handling data return types with Mybatis SQL Builder

I am relatively new to Java MyBatis. I came across SQL Builder class in MyBatis. However, I don't understand how to handle the result of the SELECT SQL query, especially if the columns are going to be different in each case while using SQL Builder. Is there an example which can help me understand how to write this?
Usually, I use Mapper XML files with ResultMap to retrieve the output of an SQL statement.
I figured out the way to get it to work. I am not sure if it is the correct way.
In the XML I made the following entry
<select id="readSignals" resultType="map">
${query}
</select>
The ${query} is passed from a QueryBuilder class and the resultType is set to "map". This cause myBatis to return a List> where each Map in the list is a row. The String contains the column name and Object contains the data.
I use the following code to convert the List> into JSON.
public static JSONObject convertToJSON(List<Map<String, Object>> queryData) {
JSONObject queryJSONOutput = new JSONObject();
JSONArray outputArray = new JSONArray();
queryData.stream().forEach(d -> {
JSONObject jsonObject = new JSONObject();
for (String key: d.keySet()) {
jsonObject.put(key, d.get(key));
}
outputArray.put(jsonObject);
});
queryJSONOutput.put("data", outputArray);
return queryJSONOutput;
}

How to get the elasticsearch json response using aggregations in spring-data-elasticsearch?

I have the following:
I notice that at the end of running the code, if I print out aggregations.asMap().get('subjects');
I am getting:
org.elasticsearch.search.aggregations.bucket.terms.StringTerms#6cff59fa
Printing out "aggregations" gives me: org.elasticsearch.search.aggregations.InternalAggregations#65cf321d
What I really want is the entire string/json response that is normally returned if you were to curl on elasticsearch to get aggregations. How do I get to the raw response from the aggregation query? Also, is there a way to iterate and print out what's in those "wrapped up" objects?
https://github.com/spring-projects/spring-data-elasticsearch/blob/ab7e870d5f82f6c0de236048bd7001e8e7d2a680/src/test/java/org/springframework/data/elasticsearch/core/aggregation/ElasticsearchTemplateAggregationTests.java
#Test
public void shouldReturnAggregatedResponseForGivenSearchQuery() {
// given
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("articles").withTypes("article")
.addAggregation(terms("subjects").field("subject"))
.build();
// when
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
#Override
public Aggregations extract(SearchResponse response) {
return response.getAggregations();
}
});
// then
System.out.println(aggregations); // gives me some cryptic InternalAggregations object, how do I get to the raw JSON normally returned by elasticsearch?
System.out.println(aggregations.asMap().get("subjects")); // gives me some StringTerms object I have no idea how to iterate over to get results
}
You cannot get the raw JSON response this way, since Spring Data Elasticsearch will take care of parsing it for you, that's the whole point.
If you need to parse those buckets, you can do it like this easily:
...
StringTerms subjects = aggregations.asMap().get("subjects");
for (Terms.Bucket bucket : subjects.getBuckets()) {
String key = bucket.getKey();
long docCount = bucket.getDocCount();
// do something with the key and the doc count
}
If you really want to see the JSON being returned, what you can do is to re-write the parsed Aggregations object into JSON using serialization, but that won't really be helpful:
InternalAggregations aggregations = ...;
XContentBuilder jsonBuilder = JsonXContent.contentBuilder();
aggregations.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
String rawJson = jsonBuilder.string();
Set Size of EsRequest to Zero
Get Esresponse.toString()
Convert String to Json
Get aggregation field from Json.

Neo4j Cypher results as JSON: Getting JsonMappingException when using JsonHelper.createJsonFrom() but only with certain queries

I'm working on neo4vertx, a module that makes it possible to talk to a Neo4j database using Vert.x. Specifically, I'm working on feature called "query" which allows a Vert.x user
to send a Cypher query as an eventbus message and get a JSON resultset back.
However, I seem to run into an unexpected problem when serializing to JSON using JsonHelper.createJsonFrom() with certain queries.
A quick example (database has stuff in it of course):
// This Fails with JsonMappingException (see below):
String query="MATCH (n) RETURN n";
// This Succeeds:
String query="MATCH (n) RETURN n.something";
//Rest of code:
engine = new ExecutionEngine(graphDatabaseService);
ExecutionResult result;
result = engine.execute(query);
Object object = result.iterator();
String foo = JsonHelper.createJsonFrom(object);
System.out.println("DEBUG (foo): " + foo);
Does this look familiar to anyone? We essentially want to be able to
send any kind of query and either return an empty json string or a json
representation not unlike the result.json that you can retrieve from the
web interface of neo4j!
The Exception:
testQuery(org.openpcf.neo4vertx.neo4j.Neo4jGraphTest) Time elapsed: 2.362 sec <<< ERROR!
org.neo4j.server.rest.domain.JsonBuildRuntimeException: org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.neo4j.kernel.InternalAbstractGraphDatabase$DependencyResolverImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: scala.collection.convert.MapWrapper["content"]->org.neo4j.kernel.impl.core.NodeProxy["graphDatabase"]->org.neo4j.test.["dependencyResolver"])
...
Although the ExecutionResult is an implementation of Iterator<Map<String,Object>> it is currently just a java wrapper around scala classes, as well as Neo4j classes (like Node, Relationship, Path).
So you have probably to do 2 things:
recursively replace neo4j classes with appropriate maps and lists
probably: recursively replace scala lists and maps with java lists and maps, e.g. new LinkedHashMap(row)
I did some of that some time ago here in my cypher-websocket-experiments
You can serialize the ExecutionResult of Neo4J to JSON this way:
ExecutionResult result ...
CypherResultRepresentation repr = new CypherResultRepresentation(result, false, false);
OutputFormat format = new OutputFormat(new JsonFormat(), null, null);
String json = format.assemble(repr);
The next thing you have to do is to create the appropriate vert.x JsonObject objects.
#Rubin: I'm currently adding this to neo4vertx ;)
By using RESTAPI:
String query = "MATCH (a)-[r]-(b) RETURN a,r,b";
RestAPI restAPI = new RestAPIFacade(URI);
CypherResult result = restAPI.query(query, Collections.<String, Object>emptyMap());
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String stringResult = gson.toJson(result.asMap());
JSONObject jsonResult = new JSONObject(stringResult);
By using Bolt
String query = "MATCH (a)-[r]-(b) RETURN a,r,b";
String stringConnection = "bolt://" + HOST + ":7687";
Driver driver = GraphDatabase.driver(stringConnection, AuthTokens.basic(USER, PASS));
Session session = driver.session();
StatementResult result = session.run(query);
session.close();
driver.close();
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
while (result.hasNext()) {
Record record = result.next();
stringResult = gson.toJson(record.asMap());
}
JSONObject jsonResult = new JSONObject(stringResult);

Getting a value from deep inside JSON

I am connecting to a third party API and getting back a long JSON string. I only need one value from it, but it is located pretty deep inside the hierarchy. Is there a simple way to get it, without going through the whole thing? I looked all over but nothing seems easy.
Here's my example:
"response":{"status":1,"httpStatus":200,"data":{"myDesiredInfo":"someInfo"},"errors":[],"errorMessage":null}}
I've been trying to use Gson so I can get this blob as a JsonObject. I was sure there's something simple, like this:
jsonObject.get("myDesiredInfo")
or at the minimum something like this:
jsonObject.get("response.data.myDesiredInfo")
But it doesn't seem to exist.
So is there any parser out there that will allow me to do this?
This is my json string
String s="{"age":0,"name":"name","email":"emailk","address":{"housename":"villa"}}";
I use following code to get housename
JsonElement je = new JsonParser().parse(s);
JsonObject asJsonObject = je.getAsJsonObject();
JsonElement get = asJsonObject.get("address");
System.out.println(s + "\n" + get);
JsonObject asJsonObject1 = get.getAsJsonObject();
JsonElement get1 = asJsonObject1.get("housename");
System.out.println(get1);
The Following is my output :
{"age":0,"name":"name","email":"emailk","address":{"housename":"villa"}}
{"housename":"villa"}
"villa"
I don't think there is another way to do this. I also tried to do in other ways but i didn't get any output.
The following way you can retrieve from your jsonObject.
JSONObject jObject = new JSONObject(yourresponse);
Log.i("Desired Info is ",jObject.getJSONObject("response").getJSONObject("data").getString("myDesiredInfo"));
I wrote a little utility method that uses Gson's API to get a value as String from a JSON object, based on a java.util.List of values. So for my original question the list objects will be "response", "data", "myDesiredInfo."
Surely this can be improved on, but it's a start.
/*
* Takes a JsonObject and parses it for a primitive value, going level by level
* according to the values in #infos
*/
public static String parseJson(JsonObject json, List<String> infos) {
try {
if(infos.size() == 0) {
return json.toString();
}
JsonElement je = json.get((String)infos.get(0));
infos.remove(0);
if(je instanceof JsonObject) {
return parseJson(je.getAsJsonObject(), infos);
} else {
return je.getAsString();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Most languages have a JSON decoding library, a lot of them native. No idea what language you're using so here's PHP as an example:
$jsonObj = json_decode($json);
$json->response->data->myDesiredInfo;
Ruby, Python, Java - all these languages have good libraries.

Categories

Resources