I stored the java object in hbase (i.e) Let's say I have an object 'User' with 3 parameters like firstname, middlename and lastname. I used the following code for serialization in java
Object object = (object) user;
byte[] byteData = SerializationUtils.serialize((Serializable) object);
and stored in hbase like 'storing complete object (in byte[] format of above) in the Value portion of the KeyValue pair'
It is stored in hbase like (Example)
column=container:container, timestamp=1480016194005, value=\xAC\xED\x00\x05sr\x00&com.test.container\x07\x89\x83\xFA\x7F\xD0F\xA5\x02\x00\x08I\x00\x07classIdJ\x00\x14dateTimeInLongFormatZ\x00\x04rootZ\x00\x09undefinedL\x00\x03keyt\x00\x12Ljava/lang/String;L\x00\x04modeq\x00~\x00\x01L\x00\x04nameq\x00~\x00\x01L\x00\x06userIdq\x00~\x00\x01xp\x00\x00\x00\x02\x00\x00\x01X\x967\xBA\xF0\x00\x00t\x00\x1Econtainer_393_5639181044834024t\x00\x06expandt\x00\x02ert\x00\x08testadmin
when I try to retrieve the data, I used the following deserialization in java and converted back to object of readable format
object = SerializationUtils.deserialize(bytes);
I would like to retrieve the data stored in java format via happybase using python and I achieved it and received the data as available in hbase like
It is stored in hbase like (Example)
column=container:container, timestamp=1480016194005, value=\xAC\xED\x00\x05sr\x00&com.test.container\x07\x89\x83\xFA\x7F\xD0F\xA5\x02\x00\x08I\x00\x07classIdJ\x00\x14dateTimeInLongFormatZ\x00\x04rootZ\x00\x09undefinedL\x00\x03keyt\x00\x12Ljava/lang/String;L\x00\x04modeq\x00~\x00\x01L\x00\x04nameq\x00~\x00\x01L\x00\x06userIdq\x00~\x00\x01xp\x00\x00\x00\x02\x00\x00\x01X\x967\xBA\xF0\x00\x00t\x00\x1Econtainer_393_5639181044834024t\x00\x06expandt\x00\x02ert\x00\x08testadmin
Is there a way to deserialize the java object via python
Thanks Much
Hari
There is a Python library for that:
https://pypi.python.org/pypi/javaobj-py3/
Usage seems pretty easy with:
import javaobj
jobj = self.read_file("obj5.ser")
pobj = javaobj.loads(jobj)
print(pobj)
Related
So Im trying to translate a working python code into Java. One of the steps required is to url encode the data. But when I encode the data in Java it looks different than the one in encoded in Python.
In one of the block of Python code theres this:
data = {'request-json': json}
print('Sending form data:', data)
data = urlencode(data)
data = data.encode('utf-8')
print('Sending data:', data)
The Output
Sending form data: {'request-json': '{"apikey": "xewpjipcpovwiiql"}'}
The output after being encoded
Sending data: b'request-json=%7B%22apikey%22%3A+%22xewpjipcpovwiiql%22%7D'
So this is what im trying to do in Java. As you can imagine Java is more involved. I used gson to convert to Json
Gson gson = new Gson();
API_Key key = new API_Key("xewpjipcpovwiiql");
String jsonInputString = gson.toJson(key);
Data data = new Data(key);
String request_form = gson.toJson(data);
System.out.println(request_form);
String urlencoded = URLEncoder.encode(request_form,StandardCharsets.UTF_8);
System.out.println(urlencoded);
The output:
Sending form data: {"request-json":{"apikey":"xewpjipcpovwiiql"}}
The output of the encoded string:
%7B%22requestjson%22%3A%7B%22apikey%22%3A%22xewpjipcpovwiiql%22%7D%7D
So they dont look the same so why are they coming differently ? How do I get the same python encoded String in Java ? I noticed in Python it used a combination of single and double quotes and in Java its only Double quotes so I dont know if that makes a difference.
Thank You!
On the Python side: The data.encode('utf-8') call is not necessary or at least the documentation describes with a different intention compared to this use https://docs.python.org/3/library/stdtypes.html#str.encode (and that's why there's a b' at the beggining).
The outer brackets are missing because it is interpreting request-json as the URL parameter name (it may be easier to understand if you add a second property at the json's top/first property level, you'll see you end with request-json=%7B%22apikey%22%3A+%22xewpjipcpovwiiql%22%7D&second-property=<second-property-value>).
On the Java side: the request_form is being completely interpreted as a single value to encode so you can put the encoded value as part of some parameter in a URL, as in: https://host:port?some-parameter-name=%7B%22requestjson%22%3A%7B%22apikey%22%3A%22xewpjipcpovwiiql%22%7D%7D
I want to get the data from DB using stored procedures in the form of JSon file.
In simple words , my output should be json file which should be result of data in DB(based on stored procedure) .How can i move forward?
You have to create Object Mapper which will convert your data to an object (I think there's an apache library that can do this). Then you can use existing APIs to convert your objects to JSON string example of this is Google's or Jackson. When you have the JSON string you can now write it to a file. Hope this helps.
Hi I have a json input file as follows,
{'Latitude':'20',
'coolness':2.0,
'altitude':39000,
'pilot':{'firstName':'Buzz',
'lastName':'Aldrin'},
'mission':'apollo 11'}
How to create a java object from the json input file.
Thanks
You can use the very simple GSON library, with the Gson#fromJson() method.
Here's an example: Converting JSON to Java
There are more than one APIs that can be used. The simplest one is JSONObject
Just do the following:
JSONObject o = new JSONObject(jsonString);
int alt = o.getInt("altitude");
....
there are getXXX methods for each type. It basically stores the object as a map. This is a slow API.
You may use Google's Gson, which is an elegant and better library -- slightly more work required than JSONObject. If you are really concerned about speed, use Jackson.
I am trying to use Protocol buffers to store serialized data in Database for a web application built in java.
I have created .proto files and compiled them to get the generated classes. Also I can build the message objects using the setters & finally build() method. But to store it to database, I need serialized data as byte[] or byte buffers. How do I finally get that from the message instances ??
import com.paratha.serializers.protocolbuffers.CommentProto.Comment;
Comment.Builder comment=Comment.newBuilder();
comment.setCommentBody("This is the first comment!").setUserId(32433).build();
How do I get the serialized data from here to write to database ?
Google have made it very easy :) :
MyProtocolBufferObject myObject = MyProtocolBufferObject.newBuilder().setName("bob").build();
byte[] bytes = myObject.toByteArray();
Edit
With your example:
Comment.Builder commentBuilder=Comment.newBuilder();
Comment comment = commentBuilder.setCommentBody("This is the first comment!").setUserId(32433).build();
byte[] bytes = comment.toByteArray();
Note that when you call the newBuilder() method you are getting an instance of Comment.Builder, not an instance of Comment. It is only when you call the Comment.Builder's build() method that you get an instance of Comment.
Is there a BSON serializer/deserializer library out there for PHP or Java?
Another possibility is BSON4Jackson extension for Jackson, which adds support for BSON reading/writing.
BSON encoder/decoder in Java is pretty trivial. The following snippet of code is from my app, so it's in Scala. I am sure you could build a Java implementation from it easily.
import org.bson.BSON
import com.mongodb.{DBObject, DBDecoder, DefaultDBDecoder}
def convert(dbo: DBObject): Array[Byte] =
BSON.encode(dbo)
// NB! this is a stateful object and thus it's not thread-safe, so have
// to create one per decoding
def decoder: DBDecoder = DefaultDBDecoder.FACTORY.create
def convert(data: Array[Byte]): DBObject =
// NOTE: we do not support Ref in input, that's why "null" for DBCollection
decoder.decode(data, null)
def convert(is: InputStream): DBObject =
// NOTE: we do not support Ref in input, that's why "null" for DBCollection
decoder.decode(is, null)
The only significant note is that DBEncoder instance has an internal state it (re)uses during decoding, so it's not thread-safe. It should be ok if you decode objects one by one, but otherwise you'd better create an instance per decoding session.
check this link
http://php.net/manual/en/ref.mongo.php
bson_decode — Deserializes a BSON object into a PHP array
bson_encode — Serializes a PHP variable into a BSON string
You might check the MongoDB drivers for those languages, since MongoDB uses BSON. See what they use, or steal their implementation.
And here is a C++11 JSON encoder and decoder I've made using Rapidjson, because the native JSON encoder (BSONObj::jsonString) uses a non-standard encoding for longs: https://gist.github.com/ArtemGr/2c44cb451dc6a0cb46af
Also, unlike the native JSON encoder, this one doesn't have a problem decoding top-level arrays.