{"id":29040,"date":"2015-11-01T22:12:36","date_gmt":"2015-11-01T16:42:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=29040"},"modified":"2017-01-23T22:34:56","modified_gmt":"2017-01-23T17:04:56","slug":"hazelcast-object-deserialization-with-distributed-query","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/hazelcast-object-deserialization-with-distributed-query\/","title":{"rendered":"#Hazelcast : Enhance throughput of Java apps performance by optimization of object deserialization and distributed query"},"content":{"rendered":"<p>Hazelcast is a popular solution for In-Memory Data Grid that is often used with Databases in order to improve performance of applications, to distribute data across servers, clusters and geographies and to manage very large data sets. Here&#8217;s a <a href=\"http:\/\/www.tothenew.com\/blog\/hazelcast-integration-with-grails\/\">quick reference to\u00a0integrate\u00a0hazelcast with grails<\/a>\u00a0\u00a0.<\/p>\n<p>Since Hazelcast is a distributed system, we need to put serialized data into hazelcast.\u00a0\u00a0See\u00a0http:\/\/docs.hazelcast.org\/docs\/latest\/manual\/html\/serialization.html\u00a0for more information about object serialization &amp; deserialization. Deserialization\u00a0of objects is expensive. Therefore you want to avoid it or optimize it as much as possible while working with Hazelcast .<br \/>\nFor example:<\/p>\n<p><strong>(1)<\/strong> Use <code>IMap.delete()<\/code> instead of <code>IMap.remove()<\/code>, and <code>IMap.set()<\/code> instead of <code>IMap.put()<\/code>. These alternate methods don&#8217;t return a value and therefore cause less deserialization.<\/p>\n<p><strong>(2) Choose an efficient Serialization method.<\/strong><br \/>\n<strong>&gt;&gt;<\/strong> When Hazelcast serializes an object into Data:<\/p>\n<ul>\n<li>\u00a0It first checks whether the object is an instance of com.hazelcast.nio.serialization.DataSerializable,<\/li>\n<li>\u00a0If above fails, then it checks if it is an instance of com.hazelcast.nio.serialization.Portable,<\/li>\n<li>\u00a0If above fails, then it checks whether the object is of a well-known type like String, Long, Integer, etc. and user\u00a0specified types like ByteArraySerializer or StreamSerializer,<\/li>\n<li>If above checks fail, Hazelcast will use Java serialization.<\/li>\n<\/ul>\n<p><b>&gt;&gt; <\/b>Example of\u00a0class\u00a0implementing\u00a0<code>com.hazelcast.nio.serialization.DataSerializable.<\/code><\/p>\n<p>[sourcecode language=&#8221;java&#8221;]public class Address implements DataSerializable {<br \/>\nprivate int zipCode;<br \/>\nprivate String city;<br \/>\npublic Address() {}<br \/>\n\/\/getters setters..<br \/>\npublic void writeData( ObjectDataOutput out ) throws IOException {<br \/>\nout.writeInt(zipCode);<br \/>\nout.writeUTF(city);<br \/>\n}<br \/>\npublic void readData( ObjectDataInput in ) throws IOException {<br \/>\nzipCode = in.readInt();<br \/>\ncity = in.readUTF();<br \/>\n}<br \/>\n}[\/sourcecode]<\/p>\n<p><strong>(3) Use Distributed query instead of bring entire entrySet and\u00a0iterating over all keys or all values in an IMap locally.<\/strong><\/p>\n<p>As of Hazelcast 3.0, hazelcast\u00a0supports distributed query feature\u00a0compiled with Oracle JDK 6.<br \/>\nSee http:\/\/hazelcast.org\/features\/#query\u00a0for more information.<\/p>\n<p>Hazelcast offers two API&#8217;s for distributed query purpose,&#8211;<br \/>\n<strong>i) Criteria API <\/strong>(e.g : com.hazelcast.query.Predicates, com.hazelcast.query.PredicateBuilder)<br \/>\n<strong>ii) Distributed SQL query <\/strong>(e.g : \u00a0com.hazelcast.query.SqlPredicate)<\/p>\n<p><strong>&gt;&gt;<\/strong> How distributed query Works<\/p>\n<ul>\n<li>Requested predicate is sent to each member in the cluster.<\/li>\n<li>\u00a0Each member looks at its own local entries and filters them according to the predicate. At this stage, key\/value\u00a0pairs of the entries are deserialized and then passed to the predicate.<\/li>\n<li>Then the predicate requester merges all the results come from each member into a single set.<\/li>\n<\/ul>\n<p><strong>&gt;&gt;<\/strong>\u00a0 Below is an example of distributed query using Predicates class,&#8211;<br \/>\n<strong>Q. <\/strong>Find all addresses from city Kolkata except having zip code 700050,700052 , 700001 and all addresses from city Delhi and Mumbai. (here zip codes are not unique in all countries)<br \/>\n<strong>Sol.<\/strong><\/p>\n<p>[sourcecode language=&#8221;java&#8221;]Predicate allKolAddrs = Predicates.eq(&#8216;city&#8217;, &#8216;Kolkata&#8217;)<br \/>\nPredicate reqZipCodes = Predicates.not(Predicates.in(\u201czipCode\u201d,700050,700052,700001);<br \/>\nPredicate allDelhiMumbaiAddrs = Predicates.in(&#8216;city&#8217;, \u201cDelhi\u201d,\u201dMumbai\u201d);<br \/>\nPredicate reqPredicate = Predicates.or(Predicates.and( allKolAddrs, reqZipCodes ),allDelhiMumbaiAddrs);<br \/>\naddressMap.values(reqPredicate)[\/sourcecode]<\/p>\n<p>\/\/ desired result<\/p>\n<p>Predicates can also be applied to keySet, entrySet and localKeySet of Hazelcast distributed<br \/>\nmap.<br \/>\n<strong>*Note : for query execution each node \/ member should have the respective class and its all dependencies in their class path(here <code>Address.class<\/code>)<\/strong><\/p>\n<p><strong>(4) Use Index<\/strong><\/p>\n<p>You can increase performance of these queries using indexes , but only if you do more queries than put\/get operations .Use the below sample code in Hazelcast.xml.<\/p>\n<p>&lt;map name=&#8221;address&#8221;&gt;<br \/>\n&#8230;<br \/>\n&lt;indexes&gt;<br \/>\n&lt;index ordered=&#8221;true&#8221;&gt;zipCode&lt;\/index&gt;<br \/>\n&lt;\/indexes&gt;<br \/>\n&lt;\/map&gt;<\/p>\n<p><strong>(5) Specify\u00a0in-memory format<\/strong><\/p>\n<p>By default, Hazelcast stores data into memory in binary\u00a0(serialized) format. But sometimes, it can be efficient to store the entries in their object form, especially in cases\u00a0of local processing like entry processor and queries .Use the below sample code in Hazelcast.xml.<br \/>\n<code><\/code><\/p>\n<p><code>&lt;map name=\"address\"&gt;<br \/>\n...<br \/>\n&lt;in-memory-format&gt;OBJECT &lt;\/in-memory-format&gt;<\/code> \/\/ BINARY (default)<code><br \/>\n.....<br \/>\n&lt;\/map&gt;<\/code><\/p>\n<p>In above case the data will be stored in deserialized form<\/p>\n<p>Hope this will help for working with Hazelcast.<\/p>\n<p>For more reference :\u00a0\u00a0<a href=\"http:\/\/hazelcast.org\/mastering-hazelcast\/\" target=\"_blank\">http:\/\/hazelcast.org\/mastering-hazelcast\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hazelcast is a popular solution for In-Memory Data Grid that is often used with Databases in order to improve performance of applications, to distribute data across servers, clusters and geographies and to manage very large data sets. Here&#8217;s a quick reference to\u00a0integrate\u00a0hazelcast with grails\u00a0\u00a0. Since Hazelcast is a distributed system, we need to put serialized [&hellip;]<\/p>\n","protected":false},"author":349,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[446,1],"tags":[118,2665,2659,2660,2667,2683,2666],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/29040"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/349"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=29040"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/29040\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=29040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=29040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=29040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}