Mapping neo4j ogm query results to java objects - java

I'm collecting infos from a neo4j db, but the values I return are picked out from multiple nodes, so what I'm basically returning is a table with some properties. For this example, let's say I return the properties color:String, name:String, count:String. I query these results using session.query(*QUERY*, queryParams).
Now, when I get the results, I want to map it to an existing Java Object, that I created to hold this data. This is kind of different to the 'normal' mapping, as in general, you want to map your graph nodes to objects that represent those nodes. Here, my POJOs have nothing to do with the graph nodes.
I managed to do this using custom CompositeAttributeConverter classes for each of my data-objects, but I feel there must be a better solution than writing a new class for every new object.

You might want to take a look at executing arbitrary Cypher queries using the Session object. You can get an Iterable<Map<String,Object>> from the returned Result object, which you could process over or just output to a collection of Map results.
Or, if you have APOC Procedures installed, you can always write up a query to return your results as a JSON string, and convert that to JSON objects in Java with the appropriate library and use those as needed.

Related

Best way to convert Cassandra ResultSet to java objects in OSS driver

We are in the process of migrating from Cassandra DSE to OSS driver and the pain point is converting the result set into a list of Java objects. I don't want to use Mapper annotation, like detailed here
So what is the most efficient way to convert ResultSet into list of java objects? Any suggestion highly appreciated
If you don't want to use Mapper then just need to write converting function that will take Row, extract all necessary columns, and return POJO constructed from teh extracted data... I don't see other ways, except writing an adhoc version of object mapper that would use the reflection to match POJO's field names with the column names

Hibernate, hooking into hibernate criteria list call for optimization purposes

I have an #Entity Person.
I am creating a Criteria query for this entity and calling criteria.list().
This will return a List with several Person's in it.
This List I then iterate over to do some work, like generating a Json string.
Being obsessed with doing things as performant as possible, it would be ideal to work on the entities as they are generated and before being added to the list.
If possible, then I wouldn't have to iterate over them again.
Is this possible?
The 'some work' is based on complete object data, then there is no way, but if it is at attribute level, then you could write your own '#Converter' to do the work before mapping the column data to your entity.
Reference
And
If you want to retrieve the object as JSON then map your entity as JSON (use jackson hibernate).

Parsing a cypher query result (JSON) to a Java object

I use the jersey/jackson stack to address a neo4j database via the REST api, but I have some issues how to interpret the result.
If I read the node by its ID (/db/data/node/xxx) the result can be mapped to my DTO very easy by calling readEntity(MyDto.class) on the response. However, usage of internal IDs is not recommended and various use cases require to query by custom properties. Here cypher comes into play (/db/data/cypher).
Assuming a node exists with a property "myid" and a value of "1234", I can fetch it with the cypher query "MATCH (n {myid: 1234}) RETURN n". The result is a JSON string with a bunch of resources and eventually the "data" I want do unmarshall to a java object. Unmarshalling it directly fails with a ProcessingException (error reading entity from input stream). I see no API allowing to iterate the result's data.
My idea is to define some kind of generic wrapper class with an attribute "data", giving this one to the unmarshaller, and unwrapping my DTO afterwards. I wonder if there is a more elegant way to do this, like using "RETURN n.data" (which does not work) or something like this. Is it?
You should look into neo4j 2.0 where return n just returns the property map.
I usually tend to deserialize the result as a nested list/map (i.e. have ObjectMapper read to Object.class or Map.class) structure and grab the data map directly out of that.
There's probably a way to tell jackson to ignore all the information around that data field
If you want to have a nicer presentation you can also check out my cypher-rs project which returns only the data in question, nothing more.

How to read schema-less documents using Java from MongoDB

MongoDB gives the ability to write documents of any structure i.e. any number and types of key/value pairs can be written. Assuming that I use this features that my documents are indeed schema-less then how do I manage reads, basically how does the application code ( I'm using Java ) manage reads from the database.
The java driver reads and writes documents as BasicBSONObjects, which implement and are used as Map<String, Object>. Your application code is then responsible for reading this map and casting the values to the appropriate types.
A mapping framework like Morphia or Spring MongoDB can help you to convert BSONObject to your classes and vice versa.
When you want to do this yourself, you could use a Factory method which takes a BasicBSONObject, checks which keys and values it has, uses this information to create an object of the appropriate class and returns it.

get json data from java-mongo driver(not list of BasicDataObject)

I am doing following in my application server,
DBCollection collection = mongoDB.getCollection("collection");
DBCursor dbCursor = collection.find();
I have to send a JSON object to the client from the server, so how can
I convert DBCursor Object to JSON.
Actually i am sending a large collection to client, and if i convert documents of this collection into java object than it eat lot of memory, so if there a way to convert dbcursor direct to json string , or any other method which solve my problem.
it will be great help.
Thanks.
Its look like mongodriver create DataObject's objects, when call find method, so it will not help to convert dbcursor into json (which is possible by using serialize() method of com.mongodb.util.JSON class.
Revise Question:
is there any way to get data of a collection directly without using find method.
You don't want to convert the cursor to a JSON String, the cursor is simply a way to iterate over the values returned from find.
Asking if there's a way to get data from the collection without using "find" is a bit like asking how to get data from a SQL database without querying it - you can't. At some point you have to make a call to the database to get the data you want. In the MongoDB Java driver, this is done via the find() method. If you want to limit the amount of objects returned (not surprisingly a whole collection could take a lot of memory), then you want to pass the query details into the find method to filter your results.
However, if you are working exclusively with JSON, you might want to investigate third party libraries like MongoJack - this uses the Java driver under the covers, but translates the results of your query directly into JSON for you.

Categories