Pig Execution Mehanisms
Apache Pig scripts can be executed in three ways, namely, interactive mode, batch mode, and embedded mode.
Interactive Mode(Grunt shell) − You can run Apache Pig in interactive mode using the Grunt shell. In this shell, you can enter the Pig Latin statements and get the output (using Dump operator).
Batch Mode(Script) − You can run Apache Pig in Batch mode by writing the Pig Latin script in a single file with.pigextension.
Embedded Mode(UDF) − Apache Pig provides the provision of defining our own functions (UserDefinedFunctions) in programming languages such as Java, and using them in our script.
You can invoke the Grunt shell in a desired mode (local/MapReduce) using the−x option as shown below.
Local mode | MapReduce mode |
---|---|
Command −$ ./pig –x local | Command −$ ./pig -x mapreduce |
Output−![]() |
Output−![]() |
Either of these commands gives you the Grunt shell prompt as shown below.
grunt
>
You can exit the Grunt shell using‘ctrl + d’.
After invoking the Grunt shell, you can execute a Pig script by directly entering the Pig Latin statements in it.
grunt
>
customers
=
LOAD
'customers.txt'
USING
PigStorage
(
','
);
Executing Apache Pig in Batch Mode
You can write an entire Pig Latin script in a file and execute it using the–x command. Let us suppose we have a Pig script in a file namedsample_script.pigas shown below.
Sample_script.pig
student
=
LOAD
'hdfs://localhost:9000/pig_data/student.txt'
USING
PigStorage
(
','
)
as
(
id
:
int
,
name
:
chararray
,
city
:
chararray
);
Dump
student
;
Now, you can execute the script in the above file as shown below.
Local mode | MapReduce mode |
---|---|
$ pig -x localSample_script.pig | $ pig -x mapreduceSample_script.pig |
The Grunt shell of Apache Pig is mainly used to write Pig Latin scripts. Prior to that, we can invoke any shell commands using sh and fs.
sh Command
Usingshcommand, we can invoke any shell commands from the Grunt shell. Usingshcommand from the Grunt shell, we cannot execute the commands that are a part of the shell environment (ex− cd).
Syntax
Given below is the syntax ofshcommand.
grunt
>
sh shell command parameters
Utility Commands
The Grunt shell provides a set of utility commands. These include utility commands such as clear, help, history, quit, and set; and commands such as exec, kill, and run to control Pig from the Grunt shell. Given below is the description of the utility commands provided by the Grunt shell.