Big Data and Hadoop (170 Blogs) Become a Certified Professional

Pig Programming: Apache Pig Script in Local Mode

Last updated on Apr 28,2020 5.9K Views


In our previous blog post we have seen learnt how to start with Pig Programming and Scripting. There, we learnt the steps to write a Pig Script in HDFS Mode. In the second part of this series, we will review the steps to write a Pig script in Local Mode.

Pig Script in Local Mode

Step1: Writing a Script

  • Open an editor (e.g. gedit) in your Cloudera Demo VM environment.
  • Write the following command to create ‘sample.pig’ file inside the home directory of cloudera user:

Command:  gedit sample.pig

Creating a "sample.pig’ file

Let’s write few PIG commands in the sample script!

Let’s say our task is to read data from a data file and to display the required contents on the console as output.

The sample data file contains the following data:

Sample Data file of Pig script

Save the text file with the name ‘information.txt’

Text file in Apache Pig

The file contains five columns FirstName, LastName, MobileNo, City, and Profession separated by tab key. Our task is to read the content of this file and display First Name, Mobile Number and Profession of a contact.

To process this data using Pig, this file should be present in local file system because we are working in local mode of Pig.

Edit the Pig script (sample.pig) to include following commands:

Commands in Pig script

Here, the data-set file ‘information.txt’ is present in cloudera directory and hence, we have specified the file path ‘/home/cloudera/information.txt’.

Commands in Pig script

Save and close the file.

  • The first command loads the file information.txt into variable A with indirect schema
    (FName, LName, MobileNo, City, and Profession).
  • The second command loads the required data from variable A to variable B.
  • The third line displays the content of variable B on the terminal/console.

Step 2: Execute the Pig Script

To execute the pig script in local mode, run the following command:

Command: pig –x local sample.pig

local sample pig command

Review the result.

Apache Pig script in local mode

Congratulations on successful execution of the Pig script and getting a step ahead in Pig Programming!

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Get started with Big Data and Hadoop

Upcoming Batches For Big Data Hadoop Certification Training Course
Course NameDate
Big Data Hadoop Certification Training Course

Class Starts on 11th February,2023

11th February

SAT&SUN (Weekend Batch)
View Details
Big Data Hadoop Certification Training Course

Class Starts on 8th April,2023

8th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Pig Programming: Apache Pig Script in Local Mode

edureka.co