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.
Related
I'm working on a springboot project and having some trouble with ElasticSearch.
The user will put some JSON-format elasticsearch DSL query strings in the database and they are black-box to me. What I need to do is get the query strings and use them so search information in elasticsearch.
In python, the DSL can be a parameter like this:
body = {
"query":{
"match_all":{}
}
}
es.search(index="my_index",doc_type="test_type",body=body)
How can I perform the search without knowing the details of the string and just using the JSON format query in Java?
I believe there are two ways to do it in modern ES client libraries. I haven't tried them myself, but first one seems to be pretty straightforward.
First one is using low-level client:
Request request = new Request("POST", "/index/_search");
request.setJsonEntity(jsonString);
Response response = client.performRequest(request);
Seems like it's enough just to push JSON as string into setJsonEntity, and you're already set.
Second one is to use high level client, and this gets tricky, though it can provide more robust API. As you might know, elasticsearch has concept of XContent, which is serialization/deserialization to/from different formats, including JSON. Theoretically, it is possible to create JsonXContentParser, which then can be used to instantiate SearchSourceBuilder:
SearchSourceBuilder.fromXContent(jsonXContentParser);
The problem is only that JsonXContentParser requires number of arguments to be instantiated, and i'm not sure how to properly create those dependencies.
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
Sometimes, we need to create some thrift objects in unit tests. We can do it by manually create object using Java code, like:
MyObj myObj = new MyObj();
myObj.setName("???");
myObj.setAge(111);
But which is not convenient. I'm looking for a way to create objects with some readable text.
We can convert thrift objects to JSON with TSimpleJSONProtocol, and get very readable JSON string, like:
{ "name": "???", "age": 111 }
But the problem is TSimpleJSONProtocol is write only, thrift can't read it back to construct an instance of MyObj.
Although there is a TJSONProtocol which supports to serialize and deserialize, but the generated JSON is not readable, it uses a very simplified JSON format and most of the field names are missing. Not convenient to construct it in tests.
Is there any way to convert thrift objects to readable string and also can convert it back? If TSimpleJSONProtocol supports converting back, which is just what I'm looking for
The main goal of Thrift is to provide efficient serialization and RPC mechanisms. What you want is something that is - at least partially - contrary to that. Human-readable data structures and machine processing efficiency are to a good extent conflicting goals, and Thrift favors the latter over the former.
You already found out about the TSimpleJson and TJson protocols and about their pros and cons, so there is not much to add. The only thing that is left to say is this: the protocol/transport stack of Thrift is simple enough.
This simplicity makes it possible to add another protocol based on your specific needs without much or overly complicated work. One could probably even write an XML protocol (if anyone really wants such bloatware) in short time.
The only caveat, especially vis-à-vis your specific case, is the fact that Thrift needs the field ID to deserialize the data. So you either need to store them in the data, or you need some other mechanism which is able to retrieve that field ID based on the field and structure names.
What is the easiest to use JSON Java library to parse a JSON (It's structure may vary so I can't mapp it to a Java class as I seen multiple libraries do it) update some elements in an array in this JSON and then save it back to disk?
There are so many libraries for JSON in Java (Gson, Jackson, etc.) and so complex that seem like a total overkill for what i need as opposed to other programming languages.
What's the most straightforward one to use? (that maybe also has a few examples on how to do this)
I have had great success with Json-Simple.
You can check it out here.
I used it to parse 1.5 Million Twitter Streaming data (which is in JSON).
You may find some sample code here on my blog.
You can use java-json jar. The docs for this jar can be found here
I use net.sf.json to do this.
here is an example :
String fromFile="{\"he\",\"hello\"}" //read from file instead
JSONObject object=(JSONObject)JSONSerializer.toJSON( fromFile );
object.put("he", "hello2");
System.out.println(object);
output:
{"he":"hello2"}
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.