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
Related
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.
I am looking to create a dynamic mongodb query in Mule and have modified the Java Transformer code from this post to work with MongoDB: Mule-Creating dynamic where condition for sql query through DB connector
My query is what is the best way to handle different data types coming in as query parameters for the WHERE clause e.g. a string will have '' and boolean will be without quotes.
I am thinking that I will need to add an if statement which determines whether to use quotes or not based on the field names.
I just wanted to know if there is a better way because it feels like I am hard coding the values which is something I try to avoid.
Thanks
You would be passing a Document type to find() method anyway right and you might already be using <mongo:query-attribute>
you can specify bson types for each of your query attribute. An example is shown below.
<mongo:query-attribute key="_id">
#[new org.bson.types.ObjectId('4c55576a5a42d6606cfa8267')]
</mongo:query-attribute>
you can get a full list of bson types here https://docs.mongodb.com/manual/reference/bson-types/
I need to use the MongoDB Java drive since I need to use the driver within Matlab.
At the moment I have the followed problem. I get my BSON object from database, now I need to convert the BSON tree into a Matlab structure. My problem is that the BSONObject or BasisBSONObject class does not have a function to retrieve the type of the particluar BSON object (ARRAY, OBJECTID, ...). There is a class named BSON in the java driver that defines the values I need. But I do not know how to find out what type my current BSON object is.
The C++ driver and also the C# driver has a function that returns the type of a particular BSON element, but where is it in the JAVA driver.
Any advices are welcome. I'm not oerfect in JAVA maybe I did not find it for this reason...?
Why not get the object and call getClass() on it? myBSON.get("myKey").getClass() Seems like that is just as easy as calling some myBSON.getTypeOf("myKey") method that does not exist and would also be redundant in the API.
Typically I use BSON<->Java POJO mapping libraries like Morphia or Spring-Data-Mongo. These libraries have converters that can convert to and from mongo objects to type-safe objects.
Additionally, I think the Mongo 3.x driver is suppose to have better support for this.
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.
Is there a direct way to go from JAXB object to BSON for Mongo DB? Do i have to first convert to JSON and then to BSON? Seems like a lot of overhead.
You can try to use an ORM like morphia.
Then map the java class to its representation in mongo.
This is probably easiest & cleanest.
Going through JSON would be very slow since it involves converting back & forth to text.
Worst case you can convert the java object to an equivalent BasicDBObject representation, which you can then use directly with the driver.
I realize it's more than 8 years since you asked the question. But I've written JaxBson which should do the trick.
If you still are interested then see the project on GitLab. There's a TL;DR section in the README.md that should explain the workings.
The artifact is named jaxbson and it is on the Maven Central Repository.