Introduction To Marketing Technology Course

Pig Scripts and Oozie Workflow

Don’t teach a pig to sing. It wastes your time and annoys the pig – Robert Heinlein

Pig Scripts query big data and generate output files.


If you want them to do the same thing everyday, you need to schedule them.

That is where Oozie comes in. Oozie is a pig scheduler.

Now, obviously, you want pig script to process new data. And generate a new output.

Therefore, the location of input and output for the pig script needs to be a variable that depends on time.

In the following example, we will examine a simple pig script that runs hourly. It tries to parse the beacon URL

benzene = LOAD '$BENZENE_DB_AND_TABLE' USING org.apache.hive.hcatalog.pig.HCatLoader();
spaceid_filter = FILTER benzene BY dt == '$DATE' AND spaceid == '1197805094';
spaceid_data = FOREACH spaceid_filter GENERATE page_uri AS page_uri;
spaceid_fields = FOREACH spaceid_data GENERATE FLATTEN (REGEX_EXTRACT_ALL(page_uri, '^.*ptyid=(.+?)&event=(.+?)&bid=(.+?)&ts=(.+?)&batch=(.+?)&logtime=(.+?)$'));
data = DISTINCT spaceid_fields PARALLEL 100;
STORE data INTO '$OUTPUT' USING PigStorage('\u0001');

As expected, we see that there are two variables – $DATE and $OUTPUT

These variables are specified in a workflow. Workflow is one unit of the job. If your script runs daily, you will have one workflow per day, 30 workflows per month, etc.

Here is the workflow:

<?xml version="1.0"?>
<workflow-app xmlns="uri:oozie:workflow:0.5" xmlns:sla="uri:oozie:sla:0.2" name="Benzene_Appsflyer_SpaceId_Hourly">
  <credential name="hcatauth" type="hcat">
<start to="benzene"/>
<action name="benzene" cred="hcatauth">
      <delete path="${output_dir}"/>
  <ok to="change_file_permission"/>
  <error to="fail"/>
<action name="change_file_permission">
    <chmod path='${output_dir}' permissions='750' dir-files='true'/>
  <ok to="end"/>
  <error to="fail"/>
<kill name="fail">
 <message>Failed Oozie pipeline.</message>
<end name='end' />

The key lines have been highlighted.

The first highlighted line is the “start” line. It specifies which action will be executed first.

The workflow is a specification of a series of actions. In this workflow, we have four actions –

  1. benzene
  2. change_file_permission
  3. end
  4. fail

The action we are interested in the most is called ‘benzene’. It executes the pig script, by calling it with params. We note that ‘datehour’ and ‘output_dir’ are the variables being referenced in this action.

This workflow is scheduled by the coordinator. The coordinator first creates an instance of the workflow, and then schedules that. Let us take a look at the coordinator:

<?xml version="1.0"?>
<coordinator-app xmlns="uri:oozie:coordinator:0.4" frequency="60" start="${start_time}" end="${end_time}" timezone="UTC" name="Benzene_Appsflyer_SpaceId_Hourly">
  <dataset name="benzene_hcat" frequency="60" initial-instance="${initial_instance}" timezone="UTC">
  <dataset name="out_dir" frequency="60" initial-instance="${initial_instance}" timezone="UTC">
  <data-in name="BENZENE_HCAT" dataset="benzene_hcat">
  <data-out name="OUT_DIR" dataset="out_dir">
        <value>${coord:formatTime(coord:nominalTime(), 'yyyyMMddHH')}</value>

As can be seen by focusing on the bolded items, the output_dir and datehour are set by the coordinator. The datehour is derived programmatically. The output_dir, though, depends on one more variable – OUT_DIR.

We trace OUT_DIR to earlier code in the coordinator – in the dataset definition – where it is looking for ‘out_dir’.

When the coordinator is submitted to oozie, the coordinator can accept a few configuration parameters, specified in a properties file.  Here is an example.




# Oozie



The out_dir has been bolded.


  1. We need to upload the pig script, the workflow and the coordinator to HDFS.
  2. Then, we submit the coordinator to oozie by submitting the properties file.
  3. The variables defined in the properties file are used by the coordinator file to create workflow and schedule them.
  4. The workflow itself consists of actions – one of which could be the pig script. The workflow inherits the variables from coordinator and passes them to the pig script as command line arguments.
  5. The pig script is invoked, it runs, and completes successfully. It stores the data in the output location.
  6. Pig script’s completion is typically one of a series of actions in the workflow. Once the pig script completes, the next action is taken by the workflow.
  7. After the final action by the workflow, typically sending a success email, the workflow ends.
  8. The coordinator keeps scheduling subsequent workflows based on the frequency and end time specified in the properties file.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s