

{"id":13961,"date":"2018-05-03T10:12:17","date_gmt":"2018-05-03T10:12:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=13961"},"modified":"2018-05-03T10:12:17","modified_gmt":"2018-05-03T10:12:17","slug":"execute-pig-script","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/","title":{"rendered":"Execute Pig Script | Apache Pig Running Scripts and Its Comments"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In this article on how to execute pig script, we will see the whole concept of<strong> Pig<\/strong> Scripts Execution. Also, we will cover the basic comments in Pig Script, that will help while writing a script in a file. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Moreover, we will see how to Execute Pig Script in a Batch mode as well as how to Execute a Pig Script from <strong>HDFS<\/strong> with proper steps and examples. <\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/pig-latin-operators-statements\/\">Let&#8217;s have a look at Pig Latin Operators and Statements<\/a><\/p>\n<h2><span style=\"font-weight: 400\">What is Apache Pig Running Scripts?<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Basically, to place Pig Latin statements and Pig commands in a single file, we use Pig scripts. It is good practice to identify the file using the *.pig extension, even while not required.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Moreover, we can run Pig scripts from the command line and from the Grunt shell. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, to pass values to parameters using parameter substitution, Pig scripts allows us to do so.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Comments in Pig Script<\/span><\/h2>\n<p><span style=\"font-weight: 400\">We can include comments in Pig Script while writing a script in a file. like:<\/span><\/p>\n<p><strong>a. Multi-line comments<\/strong><br \/>\n<span style=\"font-weight: 400\">The multi-line comments will begin with &#8216;\/*&#8217;, end them with &#8216;*\/&#8217;.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">\/* These are the multi-line comments\n In the pig script *\/<\/pre>\n<p><strong>b. Single \u2013line comments<\/strong><br \/>\n<span style=\"font-weight: 400\">The single-line comments will begin with &#8216;&#8211;&#8216;.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">--we can write single line comments like this.<\/pre>\n<h2><span style=\"font-weight: 400\">Executing Pig Script in Batch mode<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Further, follow these steps, while we execute Pig script in batch mode.<\/span><br \/>\n<strong>Step 1<\/strong><br \/>\n<span style=\"font-weight: 400\">At very first, write all the required Pig Latin statements and commands in a single file. Then save it as a .pig file.<\/span><br \/>\n<strong>Step 2<\/strong><br \/>\n<span style=\"font-weight: 400\">Afterwards, execute the Apache Pig script. To execute Pig script from the shell (Linux), see:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><strong>Local mode<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">$ pig -x local Sample_script.pig<\/pre>\n<ul>\n<li style=\"font-weight: 400\"><strong>MapReduce mode<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">$ pig -x MapReduce Sample_script.pig<\/pre>\n<p><span style=\"font-weight: 400\">It is possible to execute it from the Grunt shell as well using the exec command.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">grunt&gt; exec \/sample_script.pig<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/apache-pig-architecture\/\">Read about\u00a0Apache Pig Architecture and Execution Modes<\/a><\/p>\n<h2><span style=\"font-weight: 400\">Executing a Pig Script from HDFS<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Also, we can execute a Pig script that resides in the HDFS. Let\u2019s assume there is a Pig script with the name Sample_script.pig in the HDFS directory named \/pig_data\/. To execute it, see.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">$ pig -x mapreduce hdfs:\/\/localhost:9000\/pig_data\/Sample_script.pig<\/pre>\n<ul>\n<li style=\"font-weight: 400\"><strong>Pig Script Example<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Suppose we have a file Employee_details.txt in HDFS with the following content.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">Employee_details.txt\n001,mehul,chourey,21,9848022337,Hyderabad\n002,Ankur,Dutta,22,9848022338,Kolkata\n003,Shubham,Sengar,22,9848022339,Delhi\n004,Prerna,Tripathi,21,9848022330,Pune\n005,Sagar,Joshi,23,9848022336,Bhuwaneshwar\n006,Monika,sharma,23,9848022335,Chennai\n007,pulkit,pawar,24,9848022334,trivendram\n008,Roshan,Shaikh,24,9848022333,Chennai<\/pre>\n<p><span style=\"font-weight: 400\">Now, also we have a sample script with the name sample_script.pig, in the same HDFS directory. It contains statements performing operations and transformations on the Employee relation.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">Employee = LOAD 'hdfs:\/\/localhost:9000\/pig_data\/Employee_details.txt' USING PigStorage(',')\n  as (id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray);\nEmployee_order = ORDER Employee BY age DESC;\nEmployee_limit = LIMIT Employee_order 4;\nDump Employee_limit;<\/pre>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The script will load the data in the file named Employee_details.txt as a relation named Employee, in the first statement.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Moreover, the script will arrange the tuples of the relation in descending order, based on age, and store it as Employee_order, in the second statement.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The script will store the first 4 tuples of Employee_order as Employee_limit, in the third statement.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Ultimately, \u00a0last and the 4rth statement will dump the content of the relation Employee_limit.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Further, let\u2019s execute the sample_script.pig.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">$.\/pig -x mapreduce hdfs:\/\/localhost:9000\/pig_data\/sample_script.pig<\/pre>\n<p><span style=\"font-weight: 400\">In this way, Pig gets executed and gives you the output like:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">(7,Pulkit,Pawar,24,9848022334,trivendram)\n(8,Roshan,Shaikh,24,9848022333,Chennai)\n(5,Sagar,Joshi,23,9848022336,Bhuwaneshwar)\n(6,Monika,Sharma,23,9848022335,Chennai)\n2015-10-19 10:31:27,446 [main] INFO  org.apache.pig.Main - Pig script completed in 12\nminutes, 32 seconds and 751 milliseconds (752751 ms)<\/pre>\n<p>So, this was all about how to Execute Pig Script. Hope you like our explanation.<\/p>\n<h2><span style=\"font-weight: 400\">Conclusion &#8211; Pig Script<\/span><\/h2>\n<p><span style=\"font-weight: 400\">As a result, we have seen the whole concept of Apache Pig Running Scripts, along with Executing a Pig Script in Batch mode and from HDFS. Also, we have seen its comments to understand well. Still, if any doubt occurs, feel free to ask in the comment section.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article on how to execute pig script, we will see the whole concept of Pig Scripts Execution. Also, we will cover the basic comments in Pig Script, that will help while writing&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":35447,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[16666,1646,4446,4448,9520],"class_list":["post-13961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pig","tag-apache-pig-script","tag-basic-comments-in-pig-script","tag-execute-a-pig-script-from-hdfs","tag-execute-pig-script","tag-pig-script"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Execute Pig Script | Apache Pig Running Scripts and Its Comments - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn how to execute pig script, basic comments in Pig Script, how to Execute Pig Script in a Batch mode &amp; to Execute a Pig Script from HDFS.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/execute-pig-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Execute Pig Script | Apache Pig Running Scripts and Its Comments - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn how to execute pig script, basic comments in Pig Script, how to Execute Pig Script in a Batch mode &amp; to Execute a Pig Script from HDFS.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/execute-pig-script\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-03T10:12:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Apache-Pig-Running-Scripts-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Execute Pig Script | Apache Pig Running Scripts and Its Comments - DataFlair","description":"Learn how to execute pig script, basic comments in Pig Script, how to Execute Pig Script in a Batch mode & to Execute a Pig Script from HDFS.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/","og_locale":"en_US","og_type":"article","og_title":"Execute Pig Script | Apache Pig Running Scripts and Its Comments - DataFlair","og_description":"Learn how to execute pig script, basic comments in Pig Script, how to Execute Pig Script in a Batch mode & to Execute a Pig Script from HDFS.","og_url":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-03T10:12:17+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Apache-Pig-Running-Scripts-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Execute Pig Script | Apache Pig Running Scripts and Its Comments","datePublished":"2018-05-03T10:12:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/"},"wordCount":543,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Apache-Pig-Running-Scripts-01.jpg","keywords":["Apache Pig Script","basic comments in Pig Script","Execute a Pig Script from HDFS","execute pig script","pig script"],"articleSection":["Pig Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/execute-pig-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/","url":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/","name":"Execute Pig Script | Apache Pig Running Scripts and Its Comments - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Apache-Pig-Running-Scripts-01.jpg","datePublished":"2018-05-03T10:12:17+00:00","description":"Learn how to execute pig script, basic comments in Pig Script, how to Execute Pig Script in a Batch mode & to Execute a Pig Script from HDFS.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/execute-pig-script\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Apache-Pig-Running-Scripts-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Apache-Pig-Running-Scripts-01.jpg","width":1200,"height":628,"caption":"Execute Pig Script | Apache Pig Running Scripts and Its Comments"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/execute-pig-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Pig Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/pig\/"},{"@type":"ListItem","position":3,"name":"Execute Pig Script | Apache Pig Running Scripts and Its Comments"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=13961"}],"version-history":[{"count":0,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/35447"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=13961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=13961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=13961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}