{"id":378,"date":"2013-11-14T16:30:06","date_gmt":"2013-11-14T21:30:06","guid":{"rendered":"http:\/\/digitallibraryworld.com\/?p=378"},"modified":"2013-11-14T16:30:06","modified_gmt":"2013-11-14T21:30:06","slug":"re-using-variable-containing-a-single-value-in-pig","status":"publish","type":"post","link":"https:\/\/heisbudi.com\/?p=378","title":{"rendered":"re-using variable containing a single value in PIG"},"content":{"rendered":"<p>Pig doesn&#8217;t support scalar variable assignment. That is you can not have a statement like this<\/p>\n<pre lang=\"pig\">var = 3<\/pre>\n<p>The smallest unit you can have is a tuple, containing a single value<\/p>\n<pre lang=\"pig\">var = {3}<\/pre>\n<p>So, say that you have a variable X containing 2 columns,<\/p>\n<pre lang=\"pig\">(word1,1)\r\n(word2,4)\r\n(word3,14)<\/pre>\n<p>and you need to do some math against the second column, based on the result of a value stored in a variable, <strong>var<\/strong> above.<\/p>\n<p>The following statement won&#8217;t work:<\/p>\n<pre lang=\"pig\">result = FOREACH X GENERATE $1*var;<\/pre>\n<p>Instead you need to join two variables together so that for every row of <strong>X<\/strong>, you will have an additional column containing the value from <strong>var<\/strong>. You need to produce the following data before proceeding with your calculation<\/p>\n<pre lang=\"pig\">(word1,1)\r\n(word2,4,3)\r\n(word3,14,3)<\/pre>\n<p>To accomplish this, you need to do the following:<\/p>\n<pre lang=\"pig\">\r\ntemp = JOIN X BY 1, var BY 1 USING 'replicated';\r\n<\/pre>\n<p>Now you can do your math operation<\/p>\n<pre lang=\"pig\">result = FOREACH temp GENERATE $1*$2;<\/pre>\n<div data-counters='1' data-style='square' data-size='regular' data-url='https:\/\/heisbudi.com\/?p=378' data-title='re-using variable containing a single value in PIG' class='linksalpha_container linksalpha_app_3'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div><div data-position='' data-url='https:\/\/heisbudi.com\/?p=378' data-title='re-using variable containing a single value in PIG' class='linksalpha_container linksalpha_app_7'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>using variable containing a single value in PIG<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,23],"tags":[],"_links":{"self":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/378"}],"collection":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=378"}],"version-history":[{"count":1,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/378\/revisions\/379"}],"wp:attachment":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}