I am trying to insert into mongo DB and am getting the following error when I try and insert a joda big money object
"can't serialize class org.joda.money.BigMoney"
however according to the java doc BigMoney does implement serializable ( http://www.joda.org/joda-money/apidocs/org/joda/money/BigMoney.html )
Why would this error occur when serializable is implemented?
The Java driver can only serialise simple primitive types not complex ones - it's not using Java serialisation.
If you want to use Joda money you'll have to do the conversion yourself from the BigMoney object into one (or likely more) primitive values that the driver understands.
This will get a lot easier in the 3.x version of the driver, but for now those are your options.
Related
I'm having a problem when serializing and deserializing my objects in my project. I'm writing the object to a name.dat file.
However whenever i make a change in the Name class i can nolonger deserialize it, since it's two different objects.
Is there any way around this?
Your best options are:
Don't change your classes :-)
Throw away any serialized objects each time you change your classes.
Don't use Java object serialization.
Given that 1) and 2) are probably out of the question, option 3) should be given serious consideration. There a variety of alternatives to Java serialization, depending on the nature of the data you are persisting. These include:
Using Java properties files
Storing the data in a classical database (using SQL and the JDBC API)
Using an object-relational database mapping such as Hibernate
Using XML or JSON and a "binding" technology so that you can serialize / deserialize POJOs.
Finally, it is possible to implement class versioning using Java object serialization. However, it is tricky. And if you are continually changing the classes, then it is not going to be pleasant. Start by reading Versioning of Serializable Objects.
I'm currently using KITE API + AVRO to handle java objects to HBase. But due to various problems I'm looking for an alternative.
I've been reading about:
Phoenix
Native Hbase Api.
But there is more an alternative? .
The idea is to save and to load the java objects to Hbase and uses them in a java application.
If you're storing your objects in the Value portion of the KeyValue pair, then it's really just an array / sequence of bytes (i.e. in the code for KeyValue class there is a getValue method which returns a byte array).
At this point, you're down to object serialization and there are a host of libraries you can use with various ease of use, performance characteristics, and details of implementation. Avro is one type of serialization library which stores the schema with each record, but you could in theory use:
Standard Java serialization (implement Serializable)
Kryo
Protobuf
Just to name a few. You may want to investigate the various strengths of each library & its tradeoffs and balance that against the type of objects you plan to store (i.e. are they all effectively the same type of object or do they vary widely in type? Are they going to be long lived i.e. years and have the expectation of schema evolution & backwards compatibility etc.)
Phoenix is a JDBC api to HBase. It handles most SQL types (except intervals) - you can store arbitrary java objects using the binary data type. But if you are only storing binary data, you could easily stick with HBase. If you can coerce your data in standard SQL types, Phoenix may be a good option.
If you want to stick with the Hadoop/HBase code you can have your complex class implement org.apache.hadoop.io.Writable.
// Some complex java object
// that implements org.apache.hadoop.io.Writable
SomeObject myObject = new SomeObject();
// write the object to a byte array
// for storage in HBase
byte[] byteArr = WritableUtils.toByteArray(myObject);
Reference
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.
I have an object that its content I want to save in mysql. What I'm doing now is convert it to JSON and write it as text. If I need to read it I decode the json and create the same object. But I saw that I can save the object to mysql db as Object and read it as object, then cast it to my class. I do not need to have the ability to search in the json.
lets say this is my object:
class bus extends car{
private int seats;
private Person driver;
public bus(Person driver){
super();
this.seats = 50;
this.driver = driver;
}
}
So what is the preferred way(time, coding, memory usage etc..)?
And what is the correct way to save this kind of object in mysql db?
Rather than saving it as JSON you could use xstream and serialize your class to xml, which can then be saved as 'clob' to the database. Once you deserialize using xstream it will give back the object so you don't have to do manual conversion. xstream also supports serializing to json as well.
http://x-stream.github.io/
http://x-stream.github.io/json-tutorial.html
If you do not need options provided by this format than consider to use standard way - binary format based on serialization
There are benefits
Very less space consuming
No need third party libs
Out of box solution, because of java
Consider to use special ORM tools for this reason. It will dramatically simplify your task and save your time.
There are ORM tools
1. Hibernate
2. MyBatis
3. Eclipse link
4. Spring jdbc
Using one of them let you concentrate on a business points rather than on a low level concepts