{"id":7250,"date":"2014-10-31T14:19:16","date_gmt":"2014-10-31T19:19:16","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=7250"},"modified":"2014-11-05T07:50:48","modified_gmt":"2014-11-05T13:50:48","slug":"adding-optional-json-encoding-to-bolt","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/7250","title":{"rendered":"Adding Optional JSON Encoding to Bolt"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2014\/09\/StormLogo.jpg\" alt=\"Storm Logo\" title=\"StormLogo.jpg\" border=\"0\" width=\"125\" height=\"122\" style=\"float:right;\" \/><\/p>\n<p>I wanted to see what the effect would be to the topology to remove the communication overhead between the JSON encoder bolt and the Kafka sender bolt. Even with the <tt>:local-or-shuffle<\/tt> directive, I was worried that the objects created were causing garbage collection, or some kind of overhead, so I modified the <tt>defbolt<\/tt> I was using to send kafka messages, to take an optional parameter to indicate that it should use cheshire's JSON encoding on the message <em>prior<\/em> to sending it out.<\/p>\n<p>That way, if it's there, then it's encoded, and if it's not, then the overhead of this is very minor:<\/p>\n<pre class=\"clojure\" style=\"font-family:monospace;\">  <span style=\"color: #66cc66;\">&#40;<\/span>defbolt mk<span style=\"color: #66cc66;\">-<\/span>sender <span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #ff0000;\">&quot;message&quot;<\/span><span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>:<span style=\"color: #555;\">params<\/span> <span style=\"color: #66cc66;\">&#91;<\/span>cfg topic <span style=\"color: #66cc66;\">&amp;<\/span> <span style=\"color: #66cc66;\">&#91;<\/span>fmt<span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">&#93;<\/span> :<span style=\"color: #555;\">prepare<\/span> true<span style=\"color: #66cc66;\">&#125;<\/span>\n    <span style=\"color: #66cc66;\">&#91;<\/span>conf context collector<span style=\"color: #66cc66;\">&#93;<\/span>\n    <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #b1b100;\">let<\/span> <span style=\"color: #66cc66;\">&#91;<\/span>dest <span style=\"color: #66cc66;\">&#40;<\/span>ep<span style=\"color: #66cc66;\">\/<\/span>config cfg<span style=\"color: #66cc66;\">&#41;<\/span>\n          top <span style=\"color: #66cc66;\">&#40;<\/span>get<span style=\"color: #66cc66;\">-<\/span>topic dest topic<span style=\"color: #66cc66;\">&#41;<\/span>\n          zk <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #b1b100;\">or<\/span> <span style=\"color: #66cc66;\">&#40;<\/span>:<span style=\"color: #555;\">zkStoreConnect<\/span> dest<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span>rand<span style=\"color: #66cc66;\">-<\/span>nth <span style=\"color: #66cc66;\">&#40;<\/span>:<span style=\"color: #555;\">zookeepers<\/span> dest<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n          p <span style=\"color: #66cc66;\">&#40;<\/span>kp<span style=\"color: #66cc66;\">\/<\/span>producer <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#40;<\/span>kafka<span style=\"color: #66cc66;\">-<\/span>tag zk<span style=\"color: #66cc66;\">&#41;<\/span> zk<span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#93;<\/span>\n      <span style=\"color: #66cc66;\">&#40;<\/span>bolt\n        <span style=\"color: #66cc66;\">&#40;<\/span>execute <span style=\"color: #66cc66;\">&#91;<\/span>tuple<span style=\"color: #66cc66;\">&#93;<\/span>\n          <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #b1b100;\">doseq<\/span> <span style=\"color: #66cc66;\">&#91;<\/span>raw <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">.<\/span>getValues tuple<span style=\"color: #66cc66;\">&#41;<\/span>\n                  :<span style=\"color: #b1b100;\">when<\/span> raw\n                  :<span style=\"color: #b1b100;\">let<\/span> <span style=\"color: #66cc66;\">&#91;<\/span>msg <span style=\"color: #66cc66;\">&#40;<\/span>case fmt\n                              :<span style=\"color: #555;\">json<\/span> <span style=\"color: #66cc66;\">&#40;<\/span>nil<span style=\"color: #66cc66;\">-<\/span>if<span style=\"color: #66cc66;\">-<\/span>empty <span style=\"color: #66cc66;\">&#40;<\/span>json<span style=\"color: #66cc66;\">\/<\/span>generate<span style=\"color: #66cc66;\">-<\/span>string raw<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n                              raw<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#93;<\/span>\n                  :<span style=\"color: #b1b100;\">when<\/span> msg<span style=\"color: #66cc66;\">&#93;<\/span>\n            <span style=\"color: #66cc66;\">&#40;<\/span>kp<span style=\"color: #66cc66;\">\/<\/span>send<span style=\"color: #66cc66;\">-<\/span>messages p top <span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #66cc66;\">&#40;<\/span>kp<span style=\"color: #66cc66;\">\/<\/span>message <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">.<\/span>getBytes msg<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n            <span style=\"color: #66cc66;\">&#40;<\/span>emit<span style=\"color: #66cc66;\">-<\/span>bolt<span style=\"color: #66cc66;\">!<\/span> collector <span style=\"color: #66cc66;\">&#91;<\/span>msg<span style=\"color: #66cc66;\">&#93;<\/span> :<span style=\"color: #555;\">anchor<\/span> tuple<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n          <span style=\"color: #66cc66;\">&#40;<\/span>ack<span style=\"color: #66cc66;\">!<\/span> collector tuple<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre>\n<p>It works like a charm, and while the overhead wasn't even measurable, I left it in just in case there comes a time I want to simplify the topology by having a kayak bolt that also encodes to JSON.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to see what the effect would be to the topology to remove the communication overhead between the JSON encoder bolt and the Kafka sender bolt. Even with the :local-or-shuffle directive, I was worried that the objects created were causing garbage collection, or some kind of overhead, so I modified the defbolt I was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,6,4],"tags":[],"class_list":["post-7250","post","type-post","status-publish","format-standard","hentry","category-clojure-coding","category-cube-life","category-open-source-software"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/7250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=7250"}],"version-history":[{"count":1,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/7250\/revisions"}],"predecessor-version":[{"id":7251,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/7250\/revisions\/7251"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=7250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=7250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=7250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}