未加星标

Scala Object Serialization for MapR-DB

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Scala Object Serialization for MapR-DB

P reviously, we have discussed some of the advantages and features accompanying MapR-DB. However, this time we are going to get our hand dirty while using this enterprise-grade database.

When using MapR-DB, it is a common practice to serialize and deserialize our business objects (commonly known as POJO) from/to JSON all the time since MapR-DB data is stored using JSON format. These operations are very common and frequent so we will take a look at them from Scala’s point of view.

MapR-DB DocumentAPI

There are a series of steps to be followed to create new objects and to insert them into MapR-DB. Let’s look at the typical workflow.

This is the basic steps to insert into MapR-DB. This snippet can be extended for more complex use cases, but essentially, they will look very similar to this one.

There is one issue that can be easily recognized here. The way we create the document object through the fluent API is far from convenient. Normally, we would like to pass a POJO instead of building the document manually.

In Java, we could do the following.

In the code above, we can see that our class Link is used to create the document that will be saved to the database. MapR-DB will utilize the Java Beam object to create the document object.

Now, the problem becomes a title more tedious when using Scala, which should be your language of choice, anyway.

The ScalaIssue

Using Scala, we could also use Java Bean to create the desired objects as in Java, yet other problems quickly arise. Let’s see the same example we used before, but this time in Scala.

If you try this out, you will discover that the object link cannot be converted to Java Bean because of the value _id starts with _ . This might look small, but all documents inserted to MapR-DB should have the field _id , converting this initial, small issue into a deal breaker.

We can always go back to use manual object construction for each POJO object we have, but we should walk away from this idea as soon it comes to us, for obvious reasons.

Another alternative is to look at mechanisms to convert Scala objects to Document. It is evident we need a type class for doing the heavy lifting and bring flexibility to the conversion system.

Let’s define a type class for doing this work. Let’s name it MySerializer for a lack of a better name.

As we can see, MySerializer uses a default way to convert objects to document using Jackson serialization. Having a default serializer is a good option since the majority of the objects will use it, yet not everyone is built the same, so we need specializations as well.

Now, our code will look like as follows.

As mentioned before, sometimes the default document conversion won’t work, for instance, let’s look at the following example.

Using the default converter with Person will cause an error when trying to save the generated document to the database. MapR-DB needs an _id as the document key as stated before. In this case, we need to a custom converter for the class Person .

This is where the type class mechanism shines. We can specify the exact way to create documents from Person. Let’s see how.

Notice that we have both options, one is to use the default serializer and the second is to use a custom serializer to the specific object in question. This allows a fine-grained serialization mechanism that ultimately yields genericity without given specialization up.

At the same time, the serialization system is outside of the object itself. We should be able to modify how the serialization works without affecting the objects at all. Ultimately, we could override how serialization is done based on a specific context while having different serialization mechanics for different situations as they are needed. This is almost impossible to do in Java, but Scala is a beast at the Ad-Hoc polymorphism world.

Conclusions

MapR-DB OJAI API is nice, but it does not play well with Scala objects, especially around those that do not comply with the Java Bean specifications. On the other hand, Scala offers advanced constructs like type classes that allow us to go around many of the interoperability issues out there while keeping type safety and enabling ad-hoc polymorphism.

本文数据库(综合)相关术语:系统安全软件

代码区博客精选文章
分页:12
转载请注明
本文标题:Scala Object Serialization for MapR-DB
本站链接:https://www.codesec.net/view/627925.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(56)