Contents
Note: For Pig 0.2.0 or later, some content on this page may no longer be applicable.
So you want to learn Pig Latin. Welcome! Lets begin with the data types.
Data Types
Every piece of data in Pig has one of these four types:
A Data Atom is a simple atomic data value. It is stored as a string but can be used as either a string or a number (see Filters). Examples of data atoms are 'apache.org' and '1.0'.
A Tuple is a data record consisting of a sequence of "fields". Each field is a piece of data of any type (data atom, tuple or data bag). We denote tuples with < > bracketing. An example of a tuple is <apache.org,1.0>.
A Data Bag is a set of tuples (duplicate tuples are allowed). You may think of it as a "table", except that Pig does not require that the tuple field types match, or even that the tuples have the same number of fields! (It is up to you whether you want these properties.) We denote bags by { } bracketing. Thus, a data bag could be {<apache.org,1.0>, <flickr.com,0.8>}
A Data Map is a map from keys that are string literals to values that can be any data type. Think of it as a HashMap<String,X> where X can be any of the 4 pig data types. A Data Map supports the expected get and put interface. We denote maps by [ ] bracketing, with ":" separating the key and the value, and ";" separating successive key value pairs. Thus. a data map could be [ 'apache' : <'search', 'news'> ; 'cnn' : 'news' ]. Here, the key 'apache' is mapped to the tuple with 2 atomic fields 'search' and 'news', while the key 'cnn' is mapped to the data atom 'news'.
Data Items
Data can be referred to in various powerful and convenient ways in Pig. Any data referred to is called a Data Item. We will illustrate all these ways by using the following example tuple.
t = < 1, {<2,3>,<4,6>,<5,7>}, ['apache':'search']>Thus, t has 3 fields. Let these fields have names f1, f2, f3. Field f1 is an atom with value 1. Field f2 is a bag having 3 tuples. Field f3 is a data map having 1 key.
The following table lists the various methods of referring to data.
Method of Referring to Data |
Example |
Value for example tuple t |
Notes |
Constant |
'1.0', or 'apache.org', or 'blah' |
Value constant irrespective of t |
|
Field referred to by position |
$0 |
Data Atom '1' |
In Pig, positions start at 0 and not 1 |
Field referred to by name |
f2 |
Bag {<2,3>,<4,6>,<5,7>} |
|
Projection of another data item |
f2.$0 |
Bag {<2>,<4>,<5>} - the bag f2 projected to the first field |
|
Map Lookup against another data item |
f3#'apache' |
Data Atom 'search' |
User's responsibility to ensure that a lookup is written only against a data map, otherwise a runtime error is thrown. If the key being looked up does not exist, a Data Atom with an empty string is returned. |
Function applied to another data item |
SUM(f2.$0) |
2+4+5 = 11 |
SUM is a builtin Pig function. See PigFunctions for how to write your own functions |
Infix Expression of other data items |
COUNT(f2) + f1 / '2.0' |
3 + 1 / 2.0 = 3.5 |
|
Bincond, i.e., the value of the data item is chosen according to some condition |
(f1 = = '1' ? '2' : COUNT(f2)) |
'2' since f1=='1' is true. If f1 were != '1', then the value of this data item for t would be COUNT(f2)=3 |
See Conditions for what the format of the condition in the bincond can be |
Pig Latin Statements
A Pig Latin statement is a command that produces a Relation. A relation is simply a data bag with a name. That name is called the relation's alias. The simplest Pig Latin statement is LOAD, which reads a relation from a file in the file system. Other Pig Latin statements process one or more input relations, and produce a new relation as a result.
Starting with Pig 1.2 release due on 09/30/07, pig commands can span multiple lines and must include ";" at the end.
Examples:
grunt> A = load 'mydata' using PigStorage()
as (a, b, c);
grunt>B = group A by a;
grunt> C = foreach B {
D = distinct A.b;
generate flatten(group), COUNT(D);
}
grunt>
LOAD: Loading data from a file
Before you can do any processing, you first need to load the data. This is done by the LOAD statement. Suppose we have a tab-delimited file called "myfile.txt" that contains a relation, whose contents are:
1 2 3 4 2 1 8 3 4 4 3 3 7 2 5 8 4 3
Suppose we want to refer to the 3 fields as f1, f2, and f3. We can load this relation using the following command:
A = LOAD 'myfile.txt' USING PigStorage('\t') AS (f1,f2,f3);Here, PigStorage is the name of a "storage function" that takes care of parsing the file into a Pig relation. This storage function expects simple newline-separated records with delimiter-separated fields; it has one parameter, namely the field delimiter(s).
Future Pig Latin commands can refer to the alias "A" and will receive data that has been loaded from "myfile.txt". A will contain this data:
<1, 2, 3> <4, 2, 1> <8, 3, 4> <4, 3, 3> <7, 2, 5> <8, 4, 3>
Notes:
- The storage function shown above is the default and can be omitted.
- In the current (1.2) and earlier releases, storage functions are case sensitive. This will get changes in the future releases.
- If you don't want give names to fields, the AS clause can be omitted. You can refer to the fields by position, $0 for the first field and so on.
- If you do provide AS clause, you need to make sure to name all the fields in your data or you can get wrong results.
You can specify more complex schemas in the AS clause (see PigLatinSchemas).
If your records are stored in some special format that our functions can't parse, you can of course write your own storage function (see PigFunctions).
In Pig, relations are unordered, which means we do not guarantee that tuples are processed in any particular order. (In fact, processing may be parallelized, in which case tuples are not processed according to any total ordering.)
- If you are in hadoop mode and pass a directory name to LOAD, it will load all files within the directory. (Throws an exception in local mode.)
You can use hadoop supported globbing to specify a file or list of files to load. See hadoop glob documentation for details on globbing syntax. Globs can be used at the file system or directory levels. (This functionality is available as of pig 1.1e.)
FILTER: Getting rid of data you are not interested in
Very often, the first thing that you want to do with data is to get rid of tuples that you are not interested in. This can be done by the filter statement. For example,
Y = FILTER A BY f1 == '8';
The result is Y =
<8, 3, 4> <8, 4, 3>
Specifying Conditions
The condition following the keyword BY can be much more general than as shown above.
- The logical connectives AND, OR and NOT can be used to build a condition from various atomic conditions.
Each atomic condition can be of the form <Data Item> <compOp> <Data Item> (see Data Items for what the format of data items can be).
- The comparison operator compOp can be one of
==, <nop>!=, >, >=, <, or <= for numerical comparisons. Note that if these operators are used on non-numeric data, a runtime error will be thrown.
eq, neq, gt, gte, lt, or lte for string comparisons
matches for regular expression matching, e.g., $0 matches '.*apache.*'. The format of regular expressions is that supported by Java.
Thus, a somewhat more complicated condition can be
Y = FILTER A BY (f1 == '8') OR (NOT (f2+f3 > f1));
Note:
If you want to get rid of specifc columns or fields, rather than whole tuples, you should use the FOREACH statement and not the filter statement.
If the builtin comparison operators are not sufficient for your needs, you can write your own filter function (see PigFunctions for details). Suppose you wrote a new equality function (say myEquals). Then the first example above can be written as Y = FILTER A BY myEquals(f1,'8');
COGROUP: Getting the relevant data together
We can group the tuples in A according to some specification. A simple specification is to group according to the value of one of the fields, e.g. the first field. This is done as follows:
X = GROUP A BY f1; X = GROUP A BY (f1, f2 ..);
The result of the group statement consists of one tuple for each group. The first field of the tuple has name group and has the value on which the grouping has been performed, and the second field has name A and is a bag containing the tuples belonging to that group. Thus, X = :
<1, {<1, 2, 3>}>
<4, {<4, 2, 1>, <4, 3, 3>}>
<7, {<7, 2, 5>}>
<8, {<8, 3, 4>, <8, 4, 3>}>Suppose we have a second relation B =
<2, 4> <8, 9> <1, 3> <2, 7> <2, 9> <4, 6> <4, 9>
We can co-group A and B, which means that we jointly group the tuples from A and B, using this command:
COGROUP A BY f1, B BY $0;
You can co-group by multiple columns the same way as for group.
The result is:
<1, {<1, 2, 3>}, {<1, 3>}>
<2, {}, {<2, 4>, <2, 7>, <2, 9>}>
<4, {<4, 2, 1>, <4, 3, 3>}, {<4, 6>,<4, 9>}>
<7, {<7, 2, 5>}, {}>
<8, {<8, 3, 4>, <8, 4, 3>}, {<8, 9>}>Now, all of the original tuples whose first field is 1 are grouped together, the original tuples whose first value is 2 are together, and so on. Thus, similar to a group, the result of a co-group has one tuple for each group. The first field is called group as before and contains the value on which grouping has been performed. Besides, every tuple has a bag for each relation being co-grouped (having the same name as the alias for that relation) that contains the tuples of that relation belonging to that group.
Note that some of the bags are empty, which indicates that no tuples from the corresponding input belong to that group. If we only wish to see groups for which <i>both</i> inputs have at least one tuple, we can write:
C = COGROUP A BY $0 INNER, B BY $0 INNER;
The result is C =
<1, {<1, 2, 3>}, {<1, 3>}>
<4, {<4, 2, 1>, <4, 3, 3>}, {<4, 6>, <4, 9>}>
<8, {<8, 3, 4>, <8, 4, 3>}, {<8, 9>}>The INNER keyword can be used asymmetrically, with the obvious meaning.
In addition to using columns to group the data, an arbitrary expression can be used:
grunt> cat a
r1 1 2
r2 2 1
r3 2 8
r4 4 4
grunt> a = load 'a';
grunt> b = group a by $1*$2;
grunt> dump b;
------ MapReduce Job -----
Input: [/user/utkarsh/a:org.apache.pig.builtin.PigStorage()]
Map: [[*]]
Group: [GENERATE {[org.apache.pig.impl.builtin.MULTIPLY(GENERATE {[PROJECT
$1],[PROJECT $2]})],[*]}]
Combine: null
Reduce: null
Output: /tmp/temp1762405695/tmp1820603819:org.apache.pig.builtin.BinStorage
Split: null
Map parallelism: -1
Reduce parallelism: -1
Job jar size = 399671
Pig progress = 0%
Pig progress = 50%
Pig progress = 100%
(2.0, {(r1, 1, 2), (r2, 2, 1)})
(16.0, {(r3, 2, 8), (r4, 4, 4)})
grunt> Note:
If we want all tuples to go to a single group, e.g., when doing aggregates across entire relations, we can write GROUP A ALL.
Similarly, if we don't care about how grouping is performed, we can write GROUP A ANY. In this case, the system will group tuples randomly into groups.
A relation can be grouped (or co-grouped) according to the composite value of multiple fields. Thus, we can write COGROUP A BY (f1,f2), B BY ($0,$1).
If the criteria on which the grouping has to be performed is more complicated that just the values of some fields, you can write your own Group Function, say myGroupFunc. Then we can write GROUP A by myGroupFunc(*). Here "*" is a shorthand for all fields in the tuple. See PigFunctions for details.
- A Group function can return multiple values for a tuple, i.e., a single tuple can belong to multiple groups.
FOREACH ... GENERATE: Applying transformations to the data
The FOREACH statement is used to apply transformations to the data and to generate new data items. The basic syntax is
<output-alias> = FOREACH <input-alias> GENERATE <data-item 1>, <data-item 2>, ... ;
For each tuple in the input alias, the data items are evaluated, and a tuple containing these data items is put in the output alias. We explain this statement in greater detail by giving examples of typical uses.
Projection
To select a subset of columns from a relation, use this command:
X = FOREACH A GENERATE f1, f2;
X contains tuples from A, but with only the first and second fields present in each tuple. For the value of A given above, X =
<1, 2> <4, 2> <8, 3> <4, 3> <7, 2> <8, 4>
Projection elements can be given names using as <alias> construct. This allows to refer to the fields of the produced expression by name in the later statements:
X = FOREACH A GENERATE f1+f2 as sumf1f2; Y = FILTER X by sumf1f2 > '5';
As with SQL, asterisk (*) is shorthand for all columns. For example, with:
X = FOREACH A GENERATE *;
X is identical to A.
Nested projection
If one of the fields in the input relation, is a non-atomic field, we can perform projection on that field. For example,
FOREACH C GENERATE group, B.$1;
The result is:
<1, {<3>}>
<4, {<6>, <9>}>
<8, {<9>}>Here is another example, in which multiple nested columns are retained:
FOREACH C GENERATE group, A.(f1, f2);
The result is:
<1, {<1, 2>}>
<4, {<4, 2>, <4, 3>}>
<8, {<8, 3>, <8, 4>}>
Applying functions
Pig has a number of built-in functions. An example is the SUM() function, which takes the sum of a set of numbers in a bag. For example:
FOREACH C GENERATE group, SUM(A.f1);
gives:
<1, 1> <4, 8> <8, 16>
You may also register your own function with Pig, and refer to it in Pig Latin commands. See PigFunctions.
note: In Pig, all functions, e.g., COUNT() and SUM(), are case-sensitive (this is true for built-in functions as well as user-supplied functions).
Flattening
Sometimes we want to eliminate nesting. This can be accomplished via the FLATTEN keyword which can be attached before any valid data item. For example:
FOREACH C GENERATE group, FLATTEN(A);
yields:
<1, 1, 2, 3> <4, 4, 2, 1> <4, 4, 3, 3> <8, 8, 3, 4> <8, 8, 4, 3>
As another example,
FOREACH C GENERATE group, FLATTEN(A.f3);
yields:
<1, 3> <4, 1> <4, 3> <8, 4> <8, 3>
As a final example,
FOREACH C GENERATE flatten(A.(f1, f2)), flatten(B.$1);
yields:
<1, 2, 3> <4, 2, 6> <4, 3, 6> <4, 2, 9> <4, 3, 9> <8, 3, 9> <8, 4, 9>
Note that for the group '4' in C, there were 2 tuples each in the bags A and B. Thus, when both the bags are flattened, the cross product of these tuples is returned, i.e., the tuples <4, 2, 6>, <4, 3, 6>, <4, 2, 9>, and <4, 3, 9> in the result.
Joining
The equi-join of A and B on column 0 can be expressed as follows:
JOIN A BY $0, B BY $0;
which is equivalent to:
X = COGROUP A BY $0 INNER, B BY $0 INNER; FOREACH X GENERATE FLATTEN(A), FLATTEN(B);
The result is:
<1, 2, 3, 1, 3> <4, 2, 1, 4, 6> <4, 3, 3, 4, 6> <4, 2, 1, 4, 9> <4, 3, 3, 4, 9> <8, 3, 4, 8, 9> <8, 4, 3, 8, 9>
<i>Note:</i> On flattening, we might end with fields that have the same name but which came from different tables. They are disambiguated by prepending <alias>:: to their names. See PigLatinSchemas.
ORDER: Sorting data according to some fields
We can sort the contents of any alias according to any set of columns. For example,
X = ORDER A BY $2;
One possible output (since ties are resolved arbitrarily) is X =
<4, 2, 1> <1, 2, 3> <4, 3, 3> <8, 4, 3> <8, 3, 4> <7, 2, 5>
Notes:
- From the point of view of the Pig data model, A and X contain the same thing (since we mentioned earlier that relations are logically unordered). If you process X further, there is no guarantee that tuples will be processes in order.
However, the only guarantee is that if we retrieve the contents of X (see Retreiving Results), they are guaranteed to be in order of $2 (the third field).
To sort according to the combination of all columns, you can write ORDER A by *
DISTINCT: Eliminating duplicates in data
We can eliminate duplicates in the contents of any alias. For example, suppose we first say
X = FOREACH A GENERATE $2;
As we know, this will result in X =
<3> <1> <4> <3> <5> <3>
Now, if we say
Y = DISTINCT X;
The output is Y =
<1> <3> <5>
Notes:
- Note that original order is not preserved (another illustration of the fact that Pig relations are unordered). In fact, to eliminate duplicates, the input will be first sorted.
You can not request for distinct on a subset of the columns. This can be done by projection followed by the DISTINCT statement as in the above example.
STREAM: Using Custom Code with Pig
This is a recent addition to the language. It allows to add custom processing into Pig's execution pipeline. The details can be found in PigStreamingFunctionalSpec.
CROSS: Computing the cross product of multiple relations
To compute the cross product (also known as "cartesian product") of two or more relations, use:
X = CROSS A, B;
Based on the values of A and B given earlier in the document, the result is X =
<1, 2, 3, 2, 4> <1, 2, 3, 8, 9> <1, 2, 3, 1, 3> <1, 2, 3, 2, 7> <1, 2, 3, 2, 9> <1, 2, 3, 4, 6> <1, 2, 3, 4, 9> <4, 2, 1, 2, 4> <4, 2, 1, 8, 9> ...
Notes:
- This is an expensive operation and should not be usually necessary.
UNION: Computing the union of multiple relations
We can vertically glue together contents of multiple aliases into a single alias by the UNION command. For example,
X = UNION A, B;
The result is X =
<1, 2, 3> <4, 2, 1> <8, 3, 4> <4, 3, 3> <7, 2, 5> <8, 4, 3> <2, 4> <8, 9> <1, 3> <2, 7> <2, 9> <4, 6> <4, 9>
Notes:
- UNION is not order-preserving. The inputs are interpreted as unordered bag of tuples and the output union is also an unordered bag.
- UNION does not ensure (like in databases) that the tuples all adhere to the same schema, or even that they have the same number of fields, as in the above example. However, in the typical case, it should be so, and it is the user's responsibility to
- either ensure the same kind of tuples in all aliases being unioned, or
- be able to handle the different kinds of tuples while processing the result of the union.
- UNION does not eliminate duplicate tuples.
SPLIT: Separating data into different relations
The SPLIT statement, in some sense, is the converse of the UNION statement. It is used to partition the contents of a relation into multiple relations based on desired conditions.
An example of a SPLIT statement is the following,
SPLIT A INTO X IF $0 < 7, Y IF ($0 > 2 AND $0<> 7);
The output is
X = <1, 2, 3> <4, 2, 1> <4, 3, 3> and Y = <4, 2, 1> <8, 3, 4> <4, 3, 3> <8, 4, 3>
Notes:
- This construct is useful if you want to logically output multiple things from your function. You can then attach a field to the output of your function, and later split on that field to get the multiple outputs.
One tuple can go to multiple partitions, e.g., the <4, 2, 1> tuple above.
A tuple might also go to none of the partitions, if it doesn't satisfy any of the conditions, e.g., the <7, 2, 5> tuple above.
Conditions can be specified as mentioned in the Filter statement.
Nested Operations in FOREACH...GENERATE
If one of the fields in the input relation is a data bag, the nested data bag can be treated as an inner or a nested relation. Consequently, in a FOREACH...GENERATE statement, we can perform many of the operations on this nested relation that we can on a regular relation.
The specific operations that we can do on the nested relations are FILTER, ORDER, and DISTINCT. Note that we do not allow FOREACH...GENERATE on the nested relation, since that leads to the possibility of arbitrary number of nesting levels.
The syntax for doing the nested operations is very similar to the regular syntax and is demonstrated by the following example:
W = LOAD '...' AS (url, outlink);
G = GROUP W by url;
R = FOREACH G {
FW = FILTER W BY outlink eq 'www.apache.org';
PW = FW.outlink;
DW = DISTINCT PW;
GENERATE group, COUNT(DW);
}Notes:
- Note the nested block within the FOREACH...GENERATE statement. The syntax is the same as regular Pig Latin syntax.
- The last statement in the nested block must be a GENERATE.
- Within the nested block, one can do nested filering, projection, sorting, and duplicate elimination.
Increasing the parallelism
To increase the parallelism of a job, include the PARALLEL clause in any of your Pig latin statements.
For example, J = JOIN A by url, B by url PARALLEL 50
Couple of notes:
- PARALLEL keyword only effects the number of reduce tasks. Map parallelism is determined by the input file, one map for each HDFS block.
- Degree of parallelism depends on the size of your cluster. At most 2 map or reduce tasks can run on a machine simultaneously. So if you ask for 40 machines, you might ask for a 1000 reduces, but they will still run 80 at a time. The example above would generate 50 reduce jobs if your cluster has at least 25 machines.
- When you don’t specify parallel, you still get the same map parallelism but only 1 reduce task.
Retrieving Results
There are several convenient ways to retrieve the contents in a particular alias:
If you are issuing PigLatin through Grunt
The command dump <alias> will dump the contents of the alias on your screen. This is typically useful only as a sanity check to see if the correct results are being produced.
The command store <alias> into <filename> [ using <store function spec>] will store the contents of the alias into the requested filename using the requested storage function (or the default function PigStorage if the storage function is not specified. )
If you are issuing PigLatin through your java program
The call PigServer.openIterator(String alias) will give you an iterator over the contents of the alias. You may find it useful to know the PigDataTypeApis to process these contents.
The call PigServer.store(String alias, String fileName, String storeFunc) will (like the store command in grunt) store the contents of the alias into the requested filename using the requested storage function (or the default function PigStorage if the storage function is not specified).
Note
- In the current (1.2) and earlier releases, storage functions are case sensitive. This will get changes in the future releases.
PigStorage can only store flat tuples, i.e., tuples having atomic fields. If you want to store nested data, use BinStorage instead.
Debugging Your Scripts
Pig provides several ways to assist in building and validating your script.
describe command allows to review schema of a particular relation.
explain command shows logical and physical execution plan to compute a particular relation.
illustrate command shows step-by-step execution of your script on a small data sample.
All three commands are described in Grunt Manual.
Working with Compressed Files
Compressed Input
Compressed files are difficult to process in parallel, since they cannot, in general, be split into fragments and independently decompressed. However, if the compression is block-oriented (e.g. bz2), the splitting and parallel processing is easy to do.
Pig has inbuilt support for processing .bz2 files in parallel (.gz support is coming soon). If the input file name extension is .bz2, Pig decompresses the file on the fly and passes the decompressed input stream to your load function. For example,
A = LOAD 'input.bz2' USING myLoad();
Multiple instances of myLoad() (as dictated by the degree of parallelism) will be created and each will be given a fragment of the *decompressed* version of input.bz2 to process.
Compressed Output
Pig currently supports output compression in the .bz2 format (so that the output can subsequently be loaded in parallel). All you have to do is include a .bz2 extension in the name of your output file. Your store function (if any) should simply write uncompressed data, and Pig will compress it on the fly.
For example,
STORE A into 'output.bz2' USING myStore();
Experimenting with Pig Latin syntax
To experiment with the Pig Latin syntax, you can use the StandAloneParser. Invoke it by the following command:
java -cp pig.jar org.apache.pig.StandAloneParser
Example usage:
$ java -cp pig.jar org.apache.pig.StandAloneParser
> A = LOAD 'myfile.txt';
---- Query parsed successfully ---
> B = FOREACH A GENERATE $1, $2;
---- Query parsed successfully ---
> C = COGROUP A BY $0, B BY $0;
---- Query parsed successfully ---
Current aliases: A->null,
> D = FOREACH C blah blah blah;
Parse error: org.apache.pig.impl.logicalLayer.parser.ParseException: Encountered "blah" at line 1, column 15.
Was expecting one of:
"generate" ...
"{" ...
> D = FOREACH C GENERATE 'hello world';
---- Query parsed successfully ---
> quit
$
Outer Join
[pi] We should add this join rewrite logic in the parser.
Outer join by example:
A = load 'test1';
grunt> dump A;
(k1, vq)
(k1, v2)
(k2, v3)
(k2, v4)
(k3, v5)
(k4, v6)
B = load 'test2';
grunt> dump B;
(k1, w1)
(k2, w2)
(k2, w3)
(k3, w4)
(k8, w8)
CG = COGROUP A by $0, B by $0;
grunt> dump CG;
(k1, {(k1, vq), (k1, v2)}, {(k1, w1)})
(k2, {(k2, v3), (k2, v4)}, {(k2, w2), (k2, w3)})
(k3, {(k3, v5)}, {(k3, w4)})
(k4, {(k4, v6)}, {})
(k8, {}, {(k8, w8)})
A_ONLY_FILTERED = FILTER CG by (COUNT(B) == '0');
A_ONLY_FLAT = FOREACH A_ONLY_FILTERED GENERATE FLATTEN(A);
dump A_ONLY_FLAT;
(k4, v6)
B_ONLY_FILTERED = FILTER CG by (COUNT(A) == '0');
B_ONLY_FLAT = FOREACH B_ONLY_FILTERED GENERATE FLATTEN(B);
dump B_ONLY_FLAT;
(k8, w8)
B_AND_A_FILTERED = FILTER CG by ((COUNT(A) != '0') and (COUNT(B) != '0'));
B_AND_A_FLAT = FOREACH B_AND_A_FILTERED GENERATE FLATTEN(B);
dump B_AND_A_FLAT;
(k1, w1)
(k2, w2)
(k2, w3)
(k3, w4)Another way is following:
A = load 'test1'; B = load 'test2'; CG_I_O = COGROUP A by $0 inner, B by $0 outer; F = FOREACH CG_I_O GENERATE A, ((COUNT(B) == '0')? '' : B) as MODIFIED_B; G = FOREACH F GENERATE FLATTEN(A), FLATTEN(MODIFIED_B); -- You can do it in one pass: Y = FOREACH CG_I_O GENERATE FLATTEN(A), FLATTEN(((COUNT(B) == '0')? '' : B)); dump G; (k1, vq, k1, w1) (k1, v2, k1, w1) (k2, v3, k2, w2) (k2, v4, k2, w2) (k2, v3, k2, w3) (k2, v4, k2, w3) (k3, v5, k3, w4) (k4, v6, )
Embedded Pig Latin
Pig Latin can be embedded into a Java program in a manner similar to JDBC. See EmbeddedPig for details.