You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ol...@apache.org on 2009/06/04 22:58:28 UTC
svn commit: r781853 [2/3] - in /hadoop/pig/trunk: ./
src/docs/src/documentation/content/xdocs/
Modified: hadoop/pig/trunk/src/docs/src/documentation/content/xdocs/piglatin.xml
URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/docs/src/documentation/content/xdocs/piglatin.xml?rev=781853&r1=781852&r2=781853&view=diff
==============================================================================
--- hadoop/pig/trunk/src/docs/src/documentation/content/xdocs/piglatin.xml (original)
+++ hadoop/pig/trunk/src/docs/src/documentation/content/xdocs/piglatin.xml Thu Jun 4 20:58:28 2009
@@ -22,122 +22,22 @@
<article>
<title>Pig Latin Manual</title>
- <!-- OVERVIEW -->
-<section><title>Conventions</title>
- <para>Conventions for the syntax and code examples included in the Pig Latin Reference Manual are described here.</para>
- <informaltable frame="all">
- <tgroup cols="3"><tbody><row>
- <entry>
- <para>Convention</para>
- </entry>
- <entry>
- <para>Description</para>
- </entry>
- <entry>
- <para>Example</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>( )</para>
- </entry>
- <entry>
- <para>Parentheses enclose one or more items.</para>
- <para>Parentheses are also used to indicate the tuple data type.</para>
- </entry>
- <entry>
- <para>Multiple items:</para>
- <para>(1, abc, (2,4,6) )</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>[ ]</para>
- </entry>
- <entry>
- <para>Straight brackets enclose one or more optional items.</para>
- <para>Straight brackets are also used to indicate the map data type. In this case <> is used to indicate optional items.</para>
- </entry>
- <entry>
- <para>Optional items:</para>
- <para>[INNER | OUTER]</para>
- <para/>
- </entry>
- </row>
- <row>
- <entry>
- <para>{ }</para>
- </entry>
- <entry>
- <para>Curly brackets enclose two or more items, one of which is required. </para>
- <para>Curly brackets also used to indicate the bag data type. In this case <> is used to indicate required items.</para>
- </entry>
- <entry>
- <para>Two items, one required:</para>
- <para>{ gen_blk | nested_gen_blk }</para>
- <para/>
- </entry>
- </row>
- <row>
- <entry>
- <para>â¦</para>
- </entry>
- <entry>
- <para>Horizontal ellipsis points indicate that you can repeat a portion of the code.</para>
- </entry>
- <entry>
- <para>Pig Latin syntax statement:</para>
- <para>cat path [path â¦]</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>UPPERCASE</para>
- <para/>
- <para>lowercase</para>
- </entry>
- <entry>
- <para>In general, uppercase type indicates elements the system supplies.</para>
- <para>In general, lowercase type indicates elements that you supply.</para>
- <para>Note: The names (aliases) of relations and fields are case sensitive. The names of Pig Latin functions are case sensitive. All other Pig Latin keywords are case insensitive.</para>
- </entry>
- <entry>
- <para>Pig Latin statement:</para>
- <para>A = LOAD 'data' AS (f1:int);</para>
- <para/>
- <orderedlist>
- <listitem>
- <para>LOAD, AS supplied BY system</para>
- </listitem>
- <listitem>
- <para>A, f1 are names (aliases)</para>
- </listitem>
- <listitem>
- <para>data supplied by you</para>
- </listitem>
- </orderedlist>
- </entry>
- </row>
- <row>
- <entry>
- <para>italics</para>
- </entry>
- <entry>
- <para>Italic type indicates placeholders or variables for which you must supply values.</para>
- </entry>
- <entry>
- <para>Pig Latin syntax:</para>
- <para>alias = LIMIT alias  n;</para>
- <para/>
- <para>You supply the values for placeholder alias and variable n.</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
+
+
+
+
+
+
+ <!-- ABOUT PIG LATIN -->
+ <section>
+ <title>About Pig Latin </title>
<section>
- <title>Pig Latin Statements</title>
- <para>A Pig Latin statement is an operator that takes a relation as input and produces another relation as output. (This definition applies to all Pig Latin operators except LOAD and STORE which read data from and write data to the file system.) Pig Latin statements can span multiple lines and must end with a semi-colon ( ; ). Pig Latin statements are generally organized in the following manner. </para>
- <orderedlist>
+ <title>Pig Latin Statements</title>
+ <para>A Pig Latin statement is an operator that takes a relation as input and produces another relation as output.
+ (This definition applies to all Pig Latin operators except LOAD and STORE which read data from and write data to the file system.)
+ Pig Latin statements can span multiple lines and must end with a semi-colon ( ; ). Pig Latin statements are generally organized in the following manner: </para>
+ <itemizedlist>
<listitem>
<para>A LOAD statement reads data from the file system. </para>
</listitem>
@@ -147,261 +47,146 @@
<listitem>
<para>A STORE statement writes output to the file system; or, a DUMP statement displays output to the screen.</para>
</listitem>
- </orderedlist>
-
+ </itemizedlist>
+
<section>
- <title>Processing Pig Latin Statements</title>
- <para>You can execute Pig Latin statements interactively using the Grunt shell or you can place Pig Latin statements in a script and run the script. Either way, Pig processes Pig Latin statements as follows:</para>
- <orderedlist>
+ <title>Running Pig Latin </title>
+ <para>You can execute Pig Latin statements interactively or in batch mode using Pig scripts (see the EXEC and RUN operators).</para>
+
+ <para>Grunt Shell, Interactive or Batch Mode</para>
+ <programlisting>
+$ pig
+... - Connecting to ...
+grunt> A = load 'data';
+grunt> B = ... ;
+or
+grunt> exec myscript.pig;
+or
+grunt> run myscript.pig;
+</programlisting>
+
+<para>Command Line, Batch Mode</para>
+ <programlisting>
+$ pig myscript.pig
+</programlisting>
+
+<para></para>
+ <para><emphasis>In general</emphasis>, Pig processes Pig Latin statements as follows:</para>
+ <itemizedlist>
<listitem>
- <para>First, Pig validates the syntax and semantics of all statements. </para>
+ <para>First, Pig validates the syntax and semantics of all statements.</para>
</listitem>
<listitem>
- <para>Next, if Pig has encountered a DUMP or STORE, Pig will execute all statements connected to the specified DUMP or STORE.</para>
+ <para>Next, if Pig encounters a DUMP or STORE, Pig will execute the statements.</para>
</listitem>
- </orderedlist>
+ </itemizedlist>
+ <para></para>
<para>In this example Pig will validate, but not execute, the LOAD and FOREACH statements.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);</para>
- <para>B = FOREACH A GENERATE name;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>In this example, Pig will validate the LOAD, FOREACH, and DUMP statements. Then, if there are no errors, Pig will execute these statements.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);</para>
- <para>B = FOREACH A GENERATE name;</para>
- <para>DUMP B;</para>
- <para>(John)</para>
- <para>(Mary)</para>
- <para>(Bill)</para>
- <para>(Joe)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
+
+<programlisting>
+A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);
+B = FOREACH A GENERATE name;
+</programlisting>
+
+ <para>In this example, Pig will validate and then execute the LOAD, FOREACH, and DUMP statements.</para>
+
+<programlisting>
+A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);
+B = FOREACH A GENERATE name;
+DUMP B;
+(John)
+(Mary)
+(Bill)
+(Joe)
+</programlisting>
+
+ <para> </para>
+ <para>Note: See Multi-Query Optimization for more information on how Pig Latin statements are processed.</para>
+ </section>
<section>
- <title>Using Comments in Scripts</title>
- <para>If you place Pig Latin statements in a script, the script can include comments. </para>
- <orderedlist>
+ <title>Retrieving Pig Latin Results</title>
+ <para>Pig Latin includes operators you can use to retrieve the results of your Pig Latin statements: </para>
+ <itemizedlist>
<listitem>
- <para>For multi-line comments use /* â¦. */</para>
+ <para>Use the DUMP operator to display results to a screen. </para>
</listitem>
<listitem>
- <para>For single line comments use --</para>
+ <para>Use the STORE operator to write results to a file on the file system.</para>
</listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>/* myscript.pig</para>
- <para>My script includes three simple Pig Latin Statements.</para>
- <para>*/</para>
- <para>A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float); -- load statement</para>
- <para>B = FOREACH A GENERATE name; Â -- foreach statement</para>
- <para>DUMP B; Â --dump statement</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
+ </itemizedlist>
+ </section>
+
<section>
- <title>Relations, Bags, Tuples, and Fields</title>
- <para>As noted, Pig Latin statements work with relations. A relation can be defined as follows:</para>
- <orderedlist>
+ <title>Debugging Pig Latin</title>
+ <para>Pig Latin includes operators that can help you debug your Pig Latin statements:</para>
+ <itemizedlist>
<listitem>
- <para>A relation is a bag (more specifically, an outer bag).</para>
+ <para>Use the DESCRIBE operator to review the schema of a relation.</para>
</listitem>
<listitem>
- <para>A bag is a collection of tuples. </para>
+ <para>Use the EXPLAIN operator to view the logical, physical, or map reduce execution plans to compute a relation.</para>
</listitem>
<listitem>
- <para>A tuple is an ordered set of fields.</para>
+ <para>Use the ILLUSTRATE operator to view the step-by-step execution of a series of statements.</para>
+ </listitem>
+ </itemizedlist>
+ </section>
+
+
+ <section>
+ <title>Working with Data</title>
+ <para>Pig Latin allows you to work with data in many ways. In general, and as a starting point:</para>
+ <itemizedlist>
+ <listitem>
+ <para>Use the FILTER operator to work with tuples or rows of data. Use the FOREACH operator to work with columns of data.</para>
</listitem>
<listitem>
- <para>A field is a piece of data.</para>
+ <para>Use the GROUP operator to group data in a single relation. Use the COGROUP and JOIN operators to group or join data in two or more relations.</para>
</listitem>
- </orderedlist>
- <para/>
- <para>A Pig relation is a bag of tuples. A Pig relation is similar to a table in a relational database, where the tuples in the bag correspond to the rows in a table. Unlike a relational table, however, Pig relations don't require that every tuple contain the same number of fields or that the fields in the same position (column) have the same type.</para>
- <para>Also note that relations are unordered which means there is no guarantee that tuples are processed in any particular order. Furthermore, processing may be parallelized in which case tuples are not processed according to any total ordering.</para>
+ <listitem>
+ <para>Use the UNION operator to merge the contents of two or more relations. Use the SPLIT operator to partition the contents of a relation into multiple relations.</para>
+ </listitem>
+ </itemizedlist>
+ </section>
- <section><title>Referencing Relations</title>
- <para>Relations are referred to by name (or alias). Names are assigned by you as part of the Pig Latin statement. In this example the name (alias) of the relation is A.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);</para>
- <para>DUMP A;</para>
- <para>(John,18,4.0F)</para>
- <para>(Mary,19,3.8F)</para>
- <para>(Bill,20,3.9F)</para>
- <para>(Joe,18,3.8F)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
+ <section>
+ <title>Increasing Parallelism</title>
+ <para>To increase the parallelism of a job, include the PARALLEL clause with the COGROUP, CROSS, DISTINCT, GROUP, JOIN and ORDER operators. PARALLEL controls the number of reducers only; the number of maps is determined by the input data (see the <ulink url="http://wiki.apache.org/pig/PigUserCookbook">Pig User Cookbook</ulink>).</para>
+ </section>
+
+ <section><title>Increasing Performance</title>
+ <para>You can increase or optimize the performance of your Pig Latin scripts by following a few simple rules (see the <ulink url="http://wiki.apache.org/pig/PigUserCookbook">Pig User Cookbook</ulink>).</para>
+ </section>
<section>
- <title>Referencing Fields</title>
- <para>Fields are referred to by positional notation or by name (or alias). </para>
- <orderedlist>
+ <title>Using Comments in Scripts</title>
+ <para>If you place Pig Latin statements in a script, the script can include comments. </para>
+ <itemizedlist>
<listitem>
- <para>Positional notation is generated by the system. Positional notation is indicated with the dollar sign ($) and begins with zero (0); for example, $0, $1, $2. </para>
+ <para>For multi-line comments use /* â¦. */</para>
</listitem>
<listitem>
- <para>Names are assigned by you using schemas (or, in the case of the GROUP operator and some functions, by the system). You can use any name that is not a Pig keyword; for example, f1, f2, f3 or a, b, c or name, age, gpa.</para>
+ <para>For single line comments use --</para>
</listitem>
- </orderedlist>
- <para>Given relation A above, the three fields are separated out in this table. </para>
- <informaltable frame="all">
- <tgroup cols="4"><tbody><row>
- <entry>
- <para/>
- </entry>
- <entry>
- <para>First Field</para>
- </entry>
- <entry>
- <para>Second Field</para>
- </entry>
- <entry>
- <para>Third Field </para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Data type</para>
- </entry>
- <entry>
- <para>chararray</para>
- </entry>
- <entry>
- <para>int</para>
- </entry>
- <entry>
- <para>float</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Positional notation (generated by system) </para>
- </entry>
- <entry>
- <para>$0</para>
- </entry>
- <entry>
- <para>$1</para>
- </entry>
- <entry>
- <para>$2</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Possible name (assigned by you using a schema)</para>
- </entry>
- <entry>
- <para>name</para>
- </entry>
- <entry>
- <para>age</para>
- </entry>
- <entry>
- <para>gpa</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Field value (for the first tuple)</para>
- </entry>
- <entry>
- <para>John</para>
- </entry>
- <entry>
- <para>18</para>
- </entry>
- <entry>
- <para>4.0</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>As shown in this example when you assign names to fields you can still refer to the fields using positional notation. However, for debugging purposes and ease of comprehension, it is better to use names.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);</para>
- <para>X = FOREACH A GENERATE name,$2;</para>
- <para>DUMP X;</para>
- <para>(John,4.0F)</para>
- <para>(Mary,3.8F)</para>
- <para>(Bill,3.9F)</para>
- <para>(Joe,3.8F)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>In this example an error is generated because the requested column ($3) is outside of the declared schema (positional notation begins with $0). Note that the error is caught before the statements are executed.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (f1:int,f2:int,f3:int);</para>
- <para>B = FOREACH A GENERATE $3;</para>
- <para>DUMP B;</para>
- <para/>
- <para>2009-01-21 23:03:46,715 [main] ERROR org.apache.pig.tools.grunt.GruntParser - java.io.IOException: Out of bound access. Trying to access non-existent  : 3. Schema {f1: bytearray,f2: bytearray,f3: bytearray} has 3 column(s). etc â¦</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section><section><title>Referencing Fields that are Complex Data Types</title>
- <para>As noted, the fields in a tuple can be any data type, including the complex data types: bags, tuples, and maps. </para>
- <orderedlist>
- <listitem>
- <para>Use the schemas for complex data types to name fields that are complex data types. </para>
- </listitem>
- <listitem>
- <para>Use the dereference operators to reference and work with fields that are complex data types.</para>
- </listitem>
- </orderedlist>
- <para>In this example the data file contains tuples. A schema for complex data types (in this case, tuples) is used to load the data. Then, dereference operators (the dot in t1.t1a and t2.$0) are used to access the fields in the tuples. Note that when you assign names to fields you can still refer to these fields using positional notation.</para>
- <orderedlist>
- <listitem>
- <para/>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>cat data;</para>
- <para>(3,8,9) (4,5,6)</para>
- <para>(1,4,7) (3,7,5)</para>
- <para>(2,5,8) (9,5,8)</para>
- <para/>
- <para>A = LOAD 'data' AS (t1:tuple(t1a:int, t1b:int,t1c:int),t2:tuple(t2a:int,t2b:int,t2c:int));</para>
- <para>DUMP A;</para>
- <para>((3,8,9),(4,5,6))</para>
- <para>((1,4,7),(3,7,5))</para>
- <para>((2,5,8),(9,5,8))</para>
- <para/>
- <para>X = FOREACH A GENERATE t1.t1a,t2.$0;</para>
- <para>DUMP X;</para>
- <para>(3,4)</para>
- <para>(1,3)</para>
- <para>(2,9)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <orderedlist>
- <listitem>
- <para/>
- </listitem>
- </orderedlist></section></section>
-
- <section>
+ </itemizedlist>
+<programlisting>
+/* myscript.pig
+My script includes three simple Pig Latin Statements.
+*/
+
+A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float); -- load statement
+B = FOREACH A GENERATE name; -- foreach statement
+DUMP B; --dump statement
+</programlisting>
+</section>
+
+ <section>
<title>Case Sensitivity</title>
<para>The names (aliases) of relations and fields are case sensitive. The names of Pig Latin functions are case sensitive. The names of parameters (see Parameter Substitution) and all other Pig Latin keywords are case insensitive.</para>
<para>In the example below, note the following:</para>
- <orderedlist>
+ <itemizedlist>
<listitem>
<para>The names (aliases) of relations A, B, and C are case sensitive.</para>
</listitem>
@@ -417,828 +202,532 @@
<listitem>
<para>In the FOREACH statement, the field in relation B is referred to by positional notation ($0).</para>
</listitem>
- </orderedlist>
+ </itemizedlist>
<para/>
+
+<programlisting>
+grunt> A = LOAD 'data' USING PigStorage() AS (f1:int, f2:int, f3:int);
+grunt> B = GROUP A BY f1;
+grunt> C = FOREACH B GENERATE COUNT ($0);
+grunt> DUMP C;
+</programlisting>
+</section>
+</section>
+
+<!-- MULTI-QUERY OPTIMIZATION-->
+<section>
+ <title>Multi-Query Optimization</title>
+<para>With multi-query optimization Pig processes an entire script or a batch of statements at once
+(as opposed to processing statements when a DUMP or STORE is encountered). Multi-query optimization introduces some changes:</para>
+
+<itemizedlist>
+<listitem>
+<para>For batch mode execution, the entire script is first parsed to determine if intermediate tasks
+can be combined to reduce the overall amount of work that needs to be done; execution starts only after the parsing is completed
+(see the EXPLAIN operator and the EXEC and RUN commands). </para>
+</listitem>
+<listitem>
+<para>Two run scenarios are optimized, as explained below: explicit and implicit splits, and storing intermediate results.</para>
+</listitem>
+</itemizedlist>
+
+<section>
+ <title> </title>
+<section>
+ <title>Explicit and Implicit Splits</title>
+<para>There might be cases in which you want different processing on separate parts of the same data stream.</para>
+<para>Example 1:</para>
+<programlisting>
+A = LOAD ...
+...
+SPLIT A' INTO B IF ..., C IF ...
+...
+STORE B' ...
+STORE C' ...
+</programlisting>
+<para>Example 2:</para>
+<programlisting>
+A = LOAD ...
+...
+B = FILTER A' ...
+C = FILTER A' ...
+...
+STORE B' ...
+STORE C' ...
+</programlisting>
+<para>In prior Pig releases, Example 1 will dump A' to disk and then start jobs for B' and C'.
+Example 2 will execute all the dependencies of B' and store it and then execute all the dependencies of C' and store it.
+Both are equivalent, but the performance will be different. </para>
+<para>Here's what the multi-query optimization does to increase the performance: </para>
+ <itemizedlist>
+ <listitem><para>For Example 2, adds an implicit split to transform the query to Example 1. This eliminates the processing of A' multiple times.</para></listitem>
+ <listitem><para>Makes the split non-blocking and allows processing to continue. This helps reduce the amount of data that has to be stored right at the split. </para></listitem>
+ <listitem><para>Allows multiple outputs from a job. This way some results can be stored as a side-effect of the main job. This is also necessary to make the previous item work. </para></listitem>
+ <listitem><para>Allows multiple split branches to be carried on to the combiner/reducer. This reduces the amount of IO again in the case where multiple branches in the split can benefit from a combiner run. </para></listitem>
+ </itemizedlist>
+</section>
+
+<section>
+ <title>Storing Intermediate Results</title>
+<para>Sometimes it is necessary to store intermediate results. </para>
+
+<programlisting>
+A = LOAD ...
+...
+STORE A'
+...
+STORE A''
+</programlisting>
+
+<para>If the script doesn't re-load A' for the processing of A the steps above A' will be duplicated.
+This is a special case of Example 2 above, so the same steps are recommended.
+With multi-query optimization, the script will process A and dump A' as a side-effect.</para>
+</section>
+</section>
+
+
+<section>
+ <title>Turning Multi-Query Optimization On or Off</title>
+ <para>By default multi-query optimization is enabled.
+ To turn off the optimization and revert to "execute-on-store" behavior, use the "-M" or "-no_multiquery" options. </para>
+ <para>To run script "myscript.pig" without the optimization, execute Pig as follows: </para>
+
+<programlisting>
+$ pig -M myscript.pig
+or
+$ pig -no_multiquery myscript.pig
+</programlisting>
+
+</section>
+
+<section>
+ <title>Error Handling</title>
+ <para>With multi-query optimization Pig processes an entire script or a batch of statements at once.
+ By default Pig tries to run all the jobs that result from that, regardless of whether some jobs fail during execution.
+ To check which jobs have succeeded or failed use one of these options. </para>
+
+ <para>First, Pig logs all successful and failed store commands. Store commands are identified by output path.
+ At the end of execution a summary line indicates success, partial failure or failure of all store commands. </para>
+
+ <para>Second, Pig returns different code upon completion for these scenarios:</para>
+ <itemizedlist>
+ <listitem><para>Return code 0: All jobs succeeded</para></listitem>
+ <listitem><para>Return code 1: <emphasis>Used for retrievable errors</emphasis> </para></listitem>
+ <listitem><para>Return code 2: All jobs have failed </para></listitem>
+ <listitem><para>Return code 3: Some jobs have failed </para></listitem>
+ </itemizedlist>
+ <para></para>
+ <para>In some cases it might be desirable to fail the entire script upon detecting the first failed job.
+ This can be achieved with the "-F" or "-stop_on_failure" command line flag.
+ If used, Pig will stop execution when the first failed job is detected and discontinue further processing.
+ This also means that file commands that come after a failed store in the script will not be executed (this can be used to create "done" files). </para>
+
+ <para>This is how the flag is used: </para>
+<programlisting>
+$ pig -F myscript.pig
+or
+$ pig -stop_on_failure myscript.pig
+</programlisting>
+
+</section>
+
+<section>
+ <title>Backward Compatibility</title>
+
+ <para>Most existing Pig scripts will produce the same result with or without the multi-query optimization.
+ There are cases though were this is not true. Path names and schemes are discussed here.</para>
+
+ <para>Any script is parsed in it's entirety before it is sent to execution. Since the current directory can change
+ throughout the script any path used in load or store is translated to a fully qualified and absolute path.</para>
+
+ <para>In map-reduce mode, the following script will load from "hdfs://<host>:<port>/data1" and store into "hdfs://<host>:<port>/tmp/out1". </para>
+<programlisting>
+cd /;
+A = LOAD 'data1';
+cd tmp;
+STORE A INTO 'out1';
+</programlisting>
+
+ <para>These expanded paths will be passed to any LoadFunc or Slicer implementation.
+ In some cases this can cause problems, especially when a LoadFunc/Slicer is not used to read from a dfs file or path
+ (for example, loading from an SQL database). </para>
+
+ <para>Solutions are to either: </para>
+ <itemizedlist>
+ <listitem><para>Specify "-M" or "-no_multiquery" to revert to the old names</para></listitem>
+ <listitem><para>Specify a custom scheme for the LoadFunc/Slicer </para></listitem>
+ </itemizedlist>
+
+ <para>Arguments used in a load statement that have a scheme other than "hdfs" or "file" will not be expanded and passed to the LoadFunc/Slicer unchanged.</para>
+ <para>In the SQL case, the SQLLoader function is invoked with "sql://mytable". </para>
+
+<programlisting>
+A = LOAD "sql://mytable" USING SQLLoader();
+</programlisting>
+</section>
+
+<section>
+ <title>Implicit Dependencies</title>
+<para>If a script has dependencies on the execution order outside of what Pig knows about, execution may fail. For instance, in this script
+MYUDF might try to read from out1, a file that A was just stored into.
+However, Pig does not know that MYUDF depends on the out1 file and might submit the jobs
+producing the out2 and out1 files at the same time.
+</para>
+<programlisting>
+...
+STORE A INTO 'out1';
+B = LOAD 'data2';
+C = FOREACH B GENERATE MYUDF($0,'out1');
+STORE C INTO 'out2';
+</programlisting>
+
+<para>To make the script work (to ensure that the right execution order is enforced) add the exec statement.
+The exec statement will trigger the execution of the statements that produce the out1 file. </para>
+
+<programlisting>
+...
+STORE A INTO 'out1';
+EXEC;
+B = LOAD 'data2';
+C = FOREACH B GENERATE MYUDF($0,'out1');
+STORE C INTO 'out2';
+</programlisting>
+
+</section>
+
+</section>
+ <!-- KEYWORDS -->
+ <section>
+ <title>Keywords</title>
+
<informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>grunt> A = LOAD 'data' USING PigStorage() AS (f1:int, f2:int, f3:int);</para>
- <para>grunt> B = GROUP A BY f1;</para>
- <para>grunt> C = FOREACH B GENERATE COUNT ($0);</para>
- <para>grunt> DUMP C;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
+ <tgroup cols="2"><tbody>
+
+ <row>
+ <entry> <para>-- A </para> </entry>
+ <entry> <para>and, any, all, arrange, as, asc, AVG</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- B </para> </entry>
+ <entry> <para>bag, BinaryDeserializer, BinarySerializer, BinStorage, by, bytearray </para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- C </para> </entry>
+ <entry> <para>cache, cat, cd, chararray, cogroup, CONCAT, copyFromLocal, copyToLocal, COUNT, cp, cross</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- D </para> </entry>
+ <entry> <para>%declare, %default, define, desc, describe, DIFF, distinct, double, du, dump</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- E </para> </entry>
+ <entry> <para>e, E, eval, exec, explain</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- F </para> </entry>
+ <entry> <para>f, F, filter, flatten, float, foreach</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- G </para> </entry>
+ <entry> <para>generate, group</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- H </para> </entry>
+ <entry> <para>help</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- I </para> </entry>
+ <entry> <para>if, illustrate, inner, input, int, into, is</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- J </para> </entry>
+ <entry> <para>join</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- K </para> </entry>
+ <entry> <para>kill</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- L </para> </entry>
+ <entry> <para>l, L, limit, load, long, ls</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- M </para> </entry>
+ <entry> <para>map, matches, MAX, MIN, mkdir, mv </para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- N </para> </entry>
+ <entry> <para>not, null</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- O </para> </entry>
+ <entry> <para>or, order, outer, output</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- P </para> </entry>
+ <entry> <para>parallel, pig, PigDump, PigStorage, pwd</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- Q </para> </entry>
+ <entry> <para>quit</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- R </para> </entry>
+ <entry> <para>register, rm, rmf, run</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- S </para> </entry>
+ <entry> <para>sample, set, ship, SIZE, split, stderr, stdin, stdout, store, stream, SUM</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- T </para> </entry>
+ <entry> <para>TextLoader, TOKENIZE, through, tuple</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- U </para> </entry>
+ <entry> <para>union, using</para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- V, W, X, Y, Z </para> </entry>
+ <entry> <para> </para> </entry>
+ </row>
+
+ <row>
+ <entry> <para>-- Symbols</para> </entry>
+ <entry> <para>= = != < > Â <= Â >= + - * / % ? $ . # ( ) [ ] { } </para> </entry>
+ </row>
+
+ </tbody></tgroup>
+ </informaltable>
+ </section>
+</section>
+
+<!-- DATA TYPES AND MORE-->
+
+<section>
+ <title>Data Types and More</title>
+
+<!-- RELATIONS, BAGS, TUPLES, FIELDS-->
<section>
- <title>Working with Data</title>
- <para>Pig Latin allows you to work with data in many ways. In general, and as a starting point:</para>
- <orderedlist>
+ <title>Relations, Bags, Tuples, Fields</title>
+ <para>Pig Latin statements work with relations. A relation can be defined as follows:</para>
+ <itemizedlist>
<listitem>
- <para>Use the FILTER operator to work with tuples or rows of data. Use the FOREACH operator to work with columns of data.</para>
+ <para>A relation is a bag (more specifically, an outer bag).</para>
</listitem>
<listitem>
- <para>Use the GROUP operator to group data in a single relation. Use the COGROUP and JOIN operators to group or join data in two or more relations.</para>
+ <para>A bag is a collection of tuples. </para>
</listitem>
<listitem>
- <para>Use the UNION operator to merge the contents of two or more relations. Use the SPLIT operator to partition the contents of a relation into multiple relations.</para>
+ <para>A tuple is an ordered set of fields.</para>
+ </listitem>
+ <listitem>
+ <para>A field is a piece of data.</para>
</listitem>
- </orderedlist></section>
+ </itemizedlist>
+ <para/>
+ <para>A Pig relation is a bag of tuples. A Pig relation is similar to a table in a relational database, where the tuples in the bag correspond to the rows in a table. Unlike a relational table, however, Pig relations don't require that every tuple contain the same number of fields or that the fields in the same position (column) have the same type.</para>
+ <para>Also note that relations are unordered which means there is no guarantee that tuples are processed in any particular order. Furthermore, processing may be parallelized in which case tuples are not processed according to any total ordering.</para>
- <section>
- <title>Increasing Parallelism</title>
- <para>To increase the parallelism of a job, include the PARALLEL clause with the COGROUP, CROSS, DISTINCT, GROUP, JOIN and ORDER operators. PARALLEL controls the number of reducers only; the number of maps is determined by the input data (see the <ulink url="http://wiki.apache.org/pig/PigUserCookbook">Pig User Cookbook</ulink>).</para></section><section><title>Increasing Performance</title>
- <para>You can increase or optimize the performance of your Pig Latin scripts by following a few simple rules (see the Pig User Cookbook).</para>
+ <section><title>Referencing Relations</title>
+ <para>Relations are referred to by name (or alias). Names are assigned by you as part of the Pig Latin statement. In this example the name (alias) of the relation is A.</para>
+
+ <programlisting>
+A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);
+DUMP A;
+(John,18,4.0F)
+(Mary,19,3.8F)
+(Bill,20,3.9F)
+(Joe,18,3.8F)
+</programlisting>
</section>
<section>
- <title>Retrieving Results</title>
- <para>Pig Latin includes operators you can use to retrieve the results of your Pig Latin statements: </para>
- <orderedlist>
- <listitem>
- <para>Use the DUMP operator to display results to a screen. </para>
- </listitem>
- <listitem>
- <para>Use the STORE operator to write results to a file on the file system.</para>
- </listitem>
- </orderedlist></section><section><title>Debugging Pig Latin Scripts</title>
- <para>Pig Latin includes operators that can help you debug your Pig Latin statements:</para>
- <orderedlist>
- <listitem>
- <para>Use the DESCRIBE operator to review the schema of a relation.</para>
- </listitem>
+ <title>Referencing Fields</title>
+ <para>Fields are referred to by positional notation or by name (alias). </para>
+ <itemizedlist>
<listitem>
- <para>Use the EXPLAIN operator to view the logical, physical, or map reduce execution plans to compute a relation.</para>
+ <para>Positional notation is generated by the system. Positional notation is indicated with the dollar sign ($) and begins with zero (0); for example, $0, $1, $2. </para>
</listitem>
<listitem>
- <para>Use the ILLUSTRATE operator to view the step-by-step execution of a series of statements.</para>
+ <para>Names are assigned by you using schemas (or, in the case of the GROUP operator and some functions, by the system). You can use any name that is not a Pig keyword; for example, f1, f2, f3 or a, b, c or name, age, gpa.</para>
</listitem>
- </orderedlist>
-
- </section>
-
-
-<!-- DATA TYPES -->
-
-<section>
-<title>Data Types</title>
+ </itemizedlist>
+ <para>Given relation A above, the three fields are separated out in this table. </para>
<informaltable frame="all">
- <tgroup cols="3"><tbody><row>
- <entry>
- <para>Simple Data Types</para>
- </entry>
- <entry>
- <para>Description</para>
- </entry>
- <entry>
- <para>Example </para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Scalars</para>
- </entry>
- <entry>
- <para/>
- </entry>
- <entry>
- <para/>
- </entry>
- </row>
- <row>
- <entry>
- <para>int</para>
- </entry>
- <entry>
- <para>Signed 32-bit integer</para>
- </entry>
- <entry>
- <para>10</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>long</para>
- </entry>
- <entry>
- <para>Signed 64-bit integer</para>
- </entry>
- <entry>
- <para>Data: Â Â 10L or 10l </para>
- <para>Display: 10L </para>
- </entry>
- </row>
- <row>
- <entry>
- <para>float</para>
- </entry>
- <entry>
- <para>32-bit floating point</para>
- </entry>
- <entry>
- <para>Data: Â Â 10.5F or 10.5f or 10.5e2f or 10.5E2F</para>
- <para>Display: 10.5F or 1050.0F</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>double</para>
- </entry>
- <entry>
- <para>64-bit floating point</para>
- </entry>
- <entry>
- <para>Data: Â Â 10.5 or 10.5e2 or 10.5E2</para>
- <para>Display: 10.5 or 1050.0</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Arrays</para>
- </entry>
- <entry>
- <para/>
- </entry>
+ <tgroup cols="4"><tbody><row>
<entry>
<para/>
</entry>
- </row>
- <row>
<entry>
- <para>chararray</para>
- </entry>
- <entry>
- <para>Character array (string) in Unicode UTF-8 format</para>
- </entry>
- <entry>
- <para>hello world</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>bytearray</para>
- </entry>
- <entry>
- <para>Byte array (blob)</para>
- </entry>
- <entry>
- <para/>
- </entry>
- </row>
- <row>
- <entry>
- <para>Complex Data Types</para>
- </entry>
- <entry>
- <para/>
- </entry>
- <entry>
- <para/>
- </entry>
- </row>
- <row>
- <entry>
- <para>tuple</para>
- </entry>
- <entry>
- <para>An ordered set of fields.</para>
- </entry>
- <entry>
- <para>(19,2)</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>bag</para>
- </entry>
- <entry>
- <para>An collection of tuples.</para>
- </entry>
- <entry>
- <para>{(19,2), (18,1)}</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>map</para>
- </entry>
- <entry>
- <para>A set of key value pairs.</para>
- </entry>
- <entry>
- <para>[open#apache]</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>Note the following general observations about data types:</para>
- <orderedlist>
- <listitem>
- <para>Use schemas to assign types to fields. Â If you don't assign types, fields default to type bytearray and implicit conversions are applied to the data depending on the context in which that data is used. For example, in relation B, f1 is converted to integer because 5 is integer. In relation C, f1 and f2 are converted to double because we don't know the type of either f1 or f2.</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (f1,f2,f3);</para>
- <para>B = FOREACH A GENERATE f1 + 5;</para>
- <para>C = FOREACH A generate f1 + f2;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <orderedlist>
- <listitem>
- <para>If a schema is defined as part of a load statement, the load function will attempt to enforce the schema. If the data does not conform to the schema, the loader will generate a null value or an error.</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (name:chararray, age:int, gpa:float);</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para/>
- <orderedlist>
- <listitem>
- <para>If an explicit cast is not supported, an error will occur. For example, you cannot cast a chararray to int.</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (name:chararray, age:int, gpa:float);</para>
- <para>B = FOREACH A GENERATE (int)name;</para>
- <para>This will cause an error â¦</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para/>
- <orderedlist>
- <listitem>
- <para>If Pig cannot resolve incompatible types through implicit casts, an error will occur. For example, you cannot add chararray and float (see the Types Table for addition and subtraction).</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (name:chararray, age:int, gpa:float);</para>
- <para>B = FOREACH A GENERATE name + gpa;</para>
- <para>This will cause an error â¦</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
-
- <section>
- <title>Tuple</title>
- <para>A tuple is an ordered set of fields.</para>
-
- <section>
- <title>Syntax </title>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>( field [, field â¦] )  </para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Terms</title>
- <informaltable frame="all">
- <tgroup cols="2"><tbody><row>
- <entry>
- <para>( Â )</para>
- </entry>
- <entry>
- <para>A tuple is enclosed in parentheses ( ).</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>field</para>
- </entry>
- <entry>
- <para>A piece of data. A field can be any data type (including tuple and bag).</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Usage</title>
- <para>You can think of a tuple as a row with one or more fields, where each field can be any data type and any field may or may not have data. If a field has no data, then the following happens:</para>
- <orderedlist>
- <listitem>
- <para>In a load statement, the loader will inject null into the tuple. The actual value that is substituted for null is loader specific; for example, PigStorage substitutes an empty field for null.</para>
- </listitem>
- <listitem>
- <para>In a non-load statement, if a requested field is missing from a tuple, Pig will inject null.</para>
- </listitem>
- </orderedlist></section>
-
- <section>
- <title>Examples</title>
- <para>In this example the tuple contains three fields.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>(John,18,4.0F)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
-
- <section>
- <title>Bag</title>
- <para>A bag is a collection of tuples.</para>
-
- <section>
- <title>Syntax: Inner bag</title><informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>{ tuple [, tuple â¦] }</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Terms</title>
- <informaltable frame="all">
- <tgroup cols="2"><tbody><row>
- <entry>
- <para>{ Â }</para>
- </entry>
- <entry>
- <para>An inner bag is enclosed in curly brackets { }.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>tuple</para>
- </entry>
- <entry>
- <para>A tuple.</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Usage </title>
- <para>Note the following about bags:</para>
- <orderedlist>
- <listitem>
- <para>A bag can have duplicate tuples.</para>
- </listitem>
- <listitem>
- <para>A bag can have tuples with differing numbers of fields. However, if Pig tries to access a field that does not exist, a null value is substituted.</para>
- </listitem>
- <listitem>
- <para>A bag can have tuples with fields that have different data types. However, for Pig to effectively process bags, the schemas of the tuples within those bags should be the same. For example, if half of the tuples include chararray fields and while the other half include float fields, only half of the tuples will participate in any kind of computation because the chararray fields will be converted to null.</para>
- <para/>
- <para>Bags have two forms: outer bag (or relation) and inner bag.</para>
- </listitem>
- </orderedlist></section>
-
- <section>
- <title>Example: Outer Bag</title>
- <para>In this example A is a relation or bag of tuples. You can think of this bag as an outer bag.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' as (f1:int, f2:int, f3;int);</para>
- <para>DUMP A;</para>
- <para>(1,2,3)</para>
- <para>(4,2,1)</para>
- <para>(8,3,4)</para>
- <para>(4,3,3)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Example: Inner Bag</title>
- <para>Now, suppose we group relation A by the first field to form relation X. </para>
- <para>In this example X is a relation or bag of tuples. The tuples in relation X have two fields. The first field is type int. The second field is type bag; you can think of this bag as an inner bag.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>X = GROUP A BY f1;</para>
- <para>DUMP X;</para>
- <para>(1,{(1,2,3)})</para>
- <para>(4,{(4,2,1),(4,3,3)})</para>
- <para>(8,{(8,3,4)})</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
-
- <section>
- <title>Map</title>
- <para>A map is a set of key value pairs.</para>
-
- <section>
- <title>Syntax (<> denotes optional)</title>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>[ key#value <, key#value â¦> ]</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Terms</title>
- <informaltable frame="all">
- <tgroup cols="2"><tbody><row>
- <entry>
- <para>[ ]</para>
- </entry>
- <entry>
- <para>Maps are enclosed in straight brackets [ ].</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>#</para>
- </entry>
- <entry>
- <para>Key value pairs are separated by the pound sign #.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>key</para>
- </entry>
- <entry>
- <para>Must be a scalar data type. Must be a unique value.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>value</para>
- </entry>
- <entry>
- <para>Any data type.</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Usage</title>
- <para>Key values within a relation must be unique.</para></section>
-
- <section>
- <title>Example</title>
- <para>In this example the map includes two key value pairs.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>[name#John,phone#5551212]</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section></section>
-
- <section>
- <title>Nulls</title>
- <para>In Pig Latin, nulls are implemented using the SQL definition of null as unknown or non-existent. Nulls can occur naturally in data or can be the result of an operation. </para>
-
- <section>
- <title>Nulls and Operators</title>
- <para>Pig Latin operators interact with nulls as shown in this table.</para>
- <informaltable frame="all">
- <tgroup cols="2"><tbody><row>
- <entry>
- <para>Operator </para>
- </entry>
- <entry>
- <para>Interaction </para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Comparison operators:</para>
- <para>==, !=</para>
- <para>>, <</para>
- <para>>=, <=</para>
- </entry>
- <entry>
- <para>If either sub-expression is null, the result is null.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Comparison operator:</para>
- <para>matches </para>
- </entry>
- <entry>
- <para>If either the string being matched against or the string defining the match is null, the result is null.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Arithmetic operators:</para>
- <para>Â + , -, *, /</para>
- <para>% modulo</para>
- <para>? bincond</para>
- </entry>
- <entry>
- <para>If either sub-expression is null, the resulting expression is null.</para>
- <para/>
- </entry>
- </row>
- <row>
- <entry>
- <para>Null operator:</para>
- <para>is null </para>
- </entry>
- <entry>
- <para>If the tested value is null, returns true; otherwise, returns false.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Null operator:</para>
- <para>is not null</para>
- </entry>
- <entry>
- <para>If the tested value is not null, returns true; otherwise, returns false.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Dereference operators:</para>
- <para>tuple (.) or map (#)</para>
- </entry>
- <entry>
- <para>If the de-referenced tuple or map is null, returns null.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Cast operator</para>
- </entry>
- <entry>
- <para>Casting a null from one type to another type results in a null.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Functions:</para>
- <para>AVG, MIN, MAX, SUM</para>
- </entry>
- <entry>
- <para>These functions ignore nulls. </para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Function:</para>
- <para>COUNT</para>
- </entry>
- <entry>
- <para>This function counts all values, including nulls.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Function:</para>
- <para>CONCAT</para>
- </entry>
- <entry>
- <para>If either sub-expression is null, the resulting expression is null.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Function:</para>
- <para>SIZE</para>
- </entry>
- <entry>
- <para>If the tested object is null, returns null.</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>For Boolean sub-expressions, note the results when nulls are used with these operators:</para>
- <orderedlist>
- <listitem>
- <para>FILTER operator â If a filter expression results in null value, the filter does not pass them through (if X is null, !X is also null, and the filter will reject both).</para>
- </listitem>
- <listitem>
- <para>Bincond operator â If a Boolean sub-expression results in null value, the resulting expression is null (see the interactions above for Arithmetic operators)</para>
- </listitem>
- </orderedlist>
-
- <section>
- <title>Example: COUNT function</title>
- <para>As noted, the COUNT function counts all values, including nulls. If you don't want the function to count null values, you can use one of the methods shown here.</para>
- <para>In this example the is not null operator is used to filter (remove) all null values before subsequent operations, including the COUNT function, are applied.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data';</para>
- <para>B = FILTER A BY $1 is not null;</para>
- <para>C = GROUP A BY $0;</para>
- <para>D = FOREACH B GENERATE GROUP, COUNT(B.$1);</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>Suppose you have written a function, RemoveNulls, to filter null values. In this example RemoveNulls is used to filter nulls values for the COUNT function only. </para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data';</para>
- <para>B = GROUP A BY $0;</para>
- <para>D = FOREACH B GENERATE GROUP, COUNT(RemoveNulls($1));</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
-
- <section>
- <title>Nulls and Constants</title>
- <para>Nulls can be used as constant expressions in place of expressions of any type.</para>
- <para>In this example a and null are projected.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (a, b, c).</para>
- <para>B = FOREACH A GENERATE a, null;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>In this example of an outer join, if the join key is missing from a table it is replaced by null.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'student' AS (name: chararray, age: int, gpa: float);</para>
- <para>B = LOAD 'votertab10k' AS (name: chararray, age: int, registration: chararray, donation: float);</para>
- <para>C = COGROUP A BY name, B BY name;</para>
- <para>D = FOREACH C GENERATE FLATTEN((IsEmpty(A) ? null : A)), FLATTEN((IsEmpty(B) ? null : B));</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>Like any other expression, null constants can be implicitly or explicitly cast. </para>
- <para>In this example both a and null will be implicitly cast to double.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (a, b, c).</para>
- <para>B = FOREACH A GENERATE a + null;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>In this example  both a and null will be cast to int, a implicitly, and null explicitly.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'data' AS (a, b, c).</para>
- <para>B = FOREACH A GENERATE a + (int)null;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Operations That Produce Nulls</title>
- <para>As noted, nulls can be the result of an operation. These operations can produce null values: </para>
- <orderedlist>
- <listitem>
- <para>Division by zero</para>
- </listitem>
- <listitem>
- <para>Returns from user defined functions (UDFs) </para>
- </listitem>
- <listitem>
- <para>Dereferencing a field that does not exist.</para>
- </listitem>
- <listitem>
- <para>Dereferencing a key that does not exist in a map. For example, given a map, info, containing [name#john, phone#5551212] if a user tries to use info#address a null is returned.</para>
- </listitem>
- <listitem>
- <para>Accessing a field that does not exist in a tuple. As a further explanation, see the examples below.</para>
- </listitem>
- </orderedlist>
-
- <section>
- <title>Example: Accessing a field that does not exist in a tuple</title>
- <para>In this example nulls are injected if fields do not have data.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>cat data;</para>
- <para>Â Â 2 Â 3</para>
- <para>4 Â </para>
- <para>7 Â 8 Â 9</para>
- <para/>
- <para>A = LOAD 'data' AS (f1:int,f2:int,f3:int)</para>
- <para>DUMP A;</para>
- <para>(,2,3)</para>
- <para>(4,,)</para>
- <para>(7,8,9)</para>
- <para/>
- <para>B = FOREACH A GENERATE f1,f2;</para>
- <para>DUMP B;</para>
- <para>(,2)</para>
- <para>(4,)</para>
- <para>(7,8)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
-
- <section>
- <title>Nulls and Load Functions</title>
- <para>
-As noted, nulls can occur naturally in the data. If nulls are part of the data, it is the responsibility of the load function to handle them correctly. Keep in mind that what is considered a null value is loader-specific; however, the load function should always communicate null values to Pig by producing Java nulls.</para>
- <para>The Pig Latin load functions (for example, PigStorage and TextLoader) produce null values wherever data is missing. For example, empty strings (chararrays) are not loaded; instead, they are replaced by nulls.</para>
- <para>PigStorage is the default load function for the LOAD operator. In this example the is not null operator is used to filter names with null values.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>A = LOAD 'student' AS (name, age, gpa); </para>
- <para>B = FILTER A BY name is not null;</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
-
- <section>
- <title>Constants</title>
- <para>Pig provides constant representations for all data types except bytearrays.</para>
- <informaltable frame="all">
- <tgroup cols="3"><tbody><row>
- <entry>
- <para/>
- </entry>
- <entry>
- <para>Constant Example</para>
- </entry>
- <entry>
- <para>Notes</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>Simple Data Types</para>
+ <para>First Field</para>
</entry>
<entry>
- <para/>
+ <para>Second Field</para>
</entry>
<entry>
- <para/>
+ <para>Third Field </para>
</entry>
</row>
<row>
<entry>
- <para>Scalars</para>
+ <para>Data type</para>
</entry>
<entry>
- <para/>
+ <para>chararray</para>
</entry>
<entry>
- <para/>
+ <para>int</para>
+ </entry>
+ <entry>
+ <para>float</para>
</entry>
</row>
<row>
<entry>
- <para>int</para>
+ <para>Positional notation (generated by system) </para>
</entry>
<entry>
- <para>19</para>
+ <para>$0</para>
</entry>
<entry>
- <para/>
+ <para>$1</para>
+ </entry>
+ <entry>
+ <para>$2</para>
</entry>
</row>
<row>
<entry>
- <para>long</para>
+ <para>Possible name (assigned by you using a schema)</para>
</entry>
<entry>
- <para>19L</para>
+ <para>name</para>
</entry>
<entry>
- <para/>
+ <para>age</para>
+ </entry>
+ <entry>
+ <para>gpa</para>
</entry>
</row>
<row>
<entry>
- <para>float</para>
+ <para>Field value (for the first tuple)</para>
</entry>
<entry>
- <para>19.2F or 1.92e2f</para>
+ <para>John</para>
</entry>
<entry>
- <para/>
+ <para>18</para>
</entry>
- </row>
- <row>
<entry>
- <para>double</para>
+ <para>4.0</para>
+ </entry>
+ </row></tbody></tgroup>
+ </informaltable>
+ <para>As shown in this example when you assign names to fields you can still refer to the fields using positional notation. However, for debugging purposes and ease of comprehension, it is better to use names.</para>
+<programlisting>
+A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float);
+X = FOREACH A GENERATE name,$2;
+DUMP X;
+(John,4.0F)
+(Mary,3.8F)
+(Bill,3.9F)
+(Joe,3.8F)
+</programlisting>
+
+ <para>In this example an error is generated because the requested column ($3) is outside of the declared schema (positional notation begins with $0). Note that the error is caught before the statements are executed.</para>
+
+ <programlisting>
+A = LOAD 'data' AS (f1:int,f2:int,f3:int);
+B = FOREACH A GENERATE $3;
+DUMP B;
+2009-01-21 23:03:46,715 [main] ERROR org.apache.pig.tools.grunt.GruntParser - java.io.IOException:
+Out of bound access. Trying to access non-existent : 3. Schema {f1: bytearray,f2: bytearray,f3: bytearray} has 3 column(s).
+<emphasis>etc ...</emphasis></programlisting>
+ </section>
+
+ <section><title>Referencing Fields that are Complex Data Types</title>
+ <para>As noted, the fields in a tuple can be any data type, including the complex data types: bags, tuples, and maps. </para>
+ <itemizedlist>
+ <listitem>
+ <para>Use the schemas for complex data types to name fields that are complex data types. </para>
+ </listitem>
+ <listitem>
+ <para>Use the dereference operators to reference and work with fields that are complex data types.</para>
+ </listitem>
+ </itemizedlist>
+ <para>In this example the data file contains tuples. A schema for complex data types (in this case, tuples) is used to load the data. Then, dereference operators (the dot in t1.t1a and t2.$0) are used to access the fields in the tuples. Note that when you assign names to fields you can still refer to these fields using positional notation.</para>
+
+
+ <programlisting>
+cat data;
+(3,8,9) (4,5,6)
+(1,4,7) (3,7,5)
+(2,5,8) (9,5,8)
+
+A = LOAD 'data' AS (t1:tuple(t1a:int, t1b:int,t1c:int),t2:tuple(t2a:int,t2b:int,t2c:int));
+
+DUMP A;
+((3,8,9),(4,5,6))
+((1,4,7),(3,7,5))
+((2,5,8),(9,5,8))
+
+X = FOREACH A GENERATE t1.t1a,t2.$0;
+
+DUMP X;
+(3,4)
+(1,3)
+(2,9)
+</programlisting>
+</section>
+</section>
+
+<section>
+<title>Data Types</title>
+
+<section>
+<title>Simple and Complex</title>
+<para></para>
+
+ <informaltable frame="all">
+ <tgroup cols="3"><tbody><row>
+ <entry>
+ <para>Simple Data Types</para>
</entry>
<entry>
- <para>19.2 or 1.92e2</para>
+ <para>Description</para>
</entry>
<entry>
- <para/>
+ <para>Example </para>
</entry>
</row>
<row>
<entry>
- <para>Arrays </para>
+ <para>Scalars</para>
</entry>
<entry>
<para/>
@@ -1249,319 +738,184 @@
</row>
<row>
<entry>
- <para>chararray</para>
+ <para>int</para>
</entry>
<entry>
- <para>'hello world'</para>
+ <para>Signed 32-bit integer</para>
</entry>
<entry>
- <para/>
+ <para>10</para>
</entry>
</row>
<row>
<entry>
- <para>bytearray</para>
+ <para>long</para>
</entry>
<entry>
- <para/>
+ <para>Signed 64-bit integer</para>
</entry>
<entry>
- <para>Not applicable.</para>
+ <para>Data: Â Â 10L or 10l </para>
+ <para>Display: 10L </para>
</entry>
</row>
<row>
<entry>
- <para>Complex Data Types</para>
+ <para>float</para>
</entry>
<entry>
- <para/>
+ <para>32-bit floating point</para>
</entry>
<entry>
- <para/>
+ <para>Data: Â Â 10.5F or 10.5f or 10.5e2f or 10.5E2F</para>
+ <para>Display: 10.5F or 1050.0F</para>
</entry>
</row>
<row>
<entry>
- <para>tuple</para>
+ <para>double</para>
</entry>
<entry>
- <para>(19, 2, 1)</para>
+ <para>64-bit floating point</para>
</entry>
<entry>
- <para>A constant in this form creates a tuple.</para>
+ <para>Data: Â Â 10.5 or 10.5e2 or 10.5E2</para>
+ <para>Display: 10.5 or 1050.0</para>
</entry>
</row>
<row>
<entry>
- <para>bag</para>
+ <para>Arrays</para>
</entry>
<entry>
- <para>{ (19, 2), (1, 2) }</para>
+ <para/>
</entry>
<entry>
- <para>A constant in this form creates a bag.</para>
+ <para/>
</entry>
</row>
<row>
<entry>
- <para>map</para>
- </entry>
- <entry>
- <para>[ 'name' # 'John', 'ext' # 5555 ]</para>
+ <para>chararray</para>
</entry>
<entry>
- <para>A constant in this form creates a map.</para>
+ <para>Character array (string) in Unicode UTF-8 format</para>
</entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>Please note the following:</para>
- <orderedlist>
- <listitem>
- <para>On UTF-8 systems you can specify string constants consisting of printable ASCII characters such as 'abc'; you can specify control characters such as '\t'; and, you can specify a character in Unicode by starting it with '\u', for instance, '\u0001' represents Ctrl-A in hexadecimal (see Wikipedia <ulink url="http://en.wikipedia.org/wiki/ASCII">ASCII</ulink>, <ulink url="http://en.wikipedia.org/wiki/Unicode">Unicode</ulink>, and <ulink url="http://en.wikipedia.org/wiki/UTF-8">UTF-8</ulink>). In theory, you should be able to specify non-UTF-8 constants on non-UTF-8 systems but as far as we know this has not been tested.</para>
- </listitem>
- <listitem>
- <para>To specify a long constant, l or L must be appended to the number (for example, 12345678L). If the l or L is not specified, but the number is too large to fit into an int, the problem will be detected at parse time and the processing is terminated. </para>
- </listitem>
- <listitem>
- <para>Any numeric constant with decimal point (for example, 1.5) and/or exponent (for example, 5e+1) is treated as double unless it ends with f or F in which case it is assigned type float (for example, Â 1.5f). </para>
- </listitem>
- </orderedlist>
- <para>The data type definitions for tuples, bags, and maps apply to constants:</para>
- <orderedlist>
- <listitem>
- <para>A tuple can contain fields of any data type</para>
- </listitem>
- <listitem>
- <para>A bag is a collection of tuples</para>
- </listitem>
- <listitem>
- <para>A map key must be a scalar; a map value can be any data type</para>
- </listitem>
- </orderedlist>
- <para>Complex constants can be used in the same places scalar constants can be used, that is, in FILTER and GENERATE statements.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
<entry>
- <para>A = LOAD 'data' USING MyStorage() AS (T: tuple(name:chararray, age: int));</para>
- <para>B = FILTER A BY T == ('john', 25);</para>
- <para>D = FOREACH B GENERATE T.name, [25#5.6], {(1, 5, 18)};</para>
+ <para>hello world</para>
</entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Expressions</title>
- <para>In Pig Latin, expressions are language constructs used with the FILTER, FOREACH, GROUP, and SPLIT operators as well as the eval functions.</para>
- <para>Expressions are written in conventional mathematical infix notation and are adapted to the UTF-8 character set. Depending on the context, expressions can include:</para>
- <orderedlist>
- <listitem>
- <para>Any Pig data type (simple data types, complex data types)</para>
- </listitem>
- <listitem>
- <para>Any Pig operator (arithmetic, comparison, null, boolean, dereference, sign, and cast)</para>
- </listitem>
- <listitem>
- <para>Any Pig built-in function.</para>
- </listitem>
- <listitem>
- <para>Any user-defined function (UDF) written in Java. </para>
- <para/>
- <para>In a Pig Latin statement, an arithmetic expression could look like this:</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
+ </row>
+ <row>
<entry>
- <para>X = GROUP A BY f2*f3;</para>
+ <para>bytearray</para>
</entry>
- </row></tbody></tgroup>
- </informaltable>
- <orderedlist>
- <listitem>
- <para/>
- <para>A string expression could look like this, where a and b are both chararrays:</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
<entry>
- <para>X = FOREACH A GENERATE CONCAT(a,b);</para>
+ <para>Byte array (blob)</para>
</entry>
- </row></tbody></tgroup>
- </informaltable>
- <orderedlist>
- <listitem>
- <para/>
- <para>A boolean expression could look like this:</para>
- </listitem>
- </orderedlist>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
<entry>
- <para>X = FILTER A BY (f1==8) OR (NOT (f2+f3 > f1));</para>
+ <para/>
</entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Schemas</title>
- <para>Schemas enable you to assign names to and declare types for fields. Schemas are optional but we encourage you to use them whenever possible; type declarations result in better parse-time error checking and more efficient code execution. </para>
- <para>Schemas are defined using the AS keyword with the LOAD, STREAM, and FOREACH operators. If you define a schema using the LOAD operator, then it is the load function that enforces the schema (see the LOAD operator and the <ulink url="http://wiki.apache.org/pig/UDFManual">User-Defined Function Manual</ulink> for more information).</para>
- <para>Note the following:</para>
- <orderedlist>
- <listitem>
- <para>You can define a schema that includes both the field name and field type.</para>
- </listitem>
- <listitem>
- <para>You can define a schema that includes the field name only; in this case, the field type defaults to bytearray.</para>
- </listitem>
- <listitem>
- <para>You can choose not to define a schema; in this case, the field is un-named and the field type defaults to bytearray.</para>
- </listitem>
- </orderedlist>
- <para>If you assign a name to a field, you can refer to that field using the name or by positional notation. If you don't assign a name to a field (the field is un-named) you can only refer to the field using positional notation.</para>
- <para>If you assign a type to a field, you can subsequently change the type using the cast operators. If you don't assign a type to a field, the field defaults to bytearray; you can change the default type using the cast operators.</para>
-
- <section>
- <title>Schemas with LOAD and STREAM Statements</title>
- <para>With LOAD and STREAM statements, the schema following the AS keyword must be enclosed in parentheses.</para>
- <para>In this example the LOAD statement includes a schema definition for simple data types.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
+ </row>
+ <row>
<entry>
- <para>A = LOAD 'data' AS (f1:int, f2:int);</para>
+ <para>Complex Data Types</para>
</entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Schemas with FOREACH Statements</title>
- <para>With FOREACH statements, the schema following the AS keyword must be enclosed in parentheses when the FLATTEN keyword is used. Otherwise, the schema should not be enclosed in parentheses.</para>
- <para>In this example the FOREACH statement includes the FLATTEN keyword and a schema for simple data types.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
<entry>
- <para>X = FOREACH C GENERATE FLATTEN(B) AS (f1:int, f2:int, f3:int);</para>
+ <para/>
</entry>
- </row></tbody></tgroup>
- </informaltable>
- <para>In this example the FOREACH statement includes a schema for simple data types.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
<entry>
- <para>X = FOREACH A GENERATE f1+f2 AS x1:int;</para>
+ <para/>
</entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Schemas for Simple Data Types</title>
- <para>Simple data types include int, long, float, double, chararray, and bytearray.</para>
-
- <section>
- <title>Syntax</title>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
+ </row>
+ <row>
<entry>
- <para>(alias[:type]) [, (alias[:type]) â¦] )</para>
+ <para>tuple</para>
</entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Terms</title>
- <informaltable frame="all">
- <tgroup cols="2"><tbody><row>
<entry>
- <para>alias</para>
+ <para>An ordered set of fields.</para>
</entry>
<entry>
- <para>The name assigned to the field.</para>
+ <para>(19,2)</para>
</entry>
</row>
<row>
<entry>
- <para>type</para>
+ <para>bag</para>
</entry>
<entry>
- <para>(Optional) The simple data type assigned to the field.</para>
- <para>The alias and type are separated by a colon ( : ).</para>
- <para>If the type is omitted, the field defaults to type bytearray.</para>
+ <para>An collection of tuples.</para>
+ </entry>
+ <entry>
+ <para>{(19,2), (18,1)}</para>
</entry>
</row>
<row>
<entry>
- <para>( , )</para>
+ <para>map</para>
</entry>
<entry>
- <para>Multiple fields are enclosed in parentheses and separated by commas.</para>
+ <para>A set of key value pairs.</para>
</entry>
- </row></tbody></tgroup>
- </informaltable></section>
-
- <section>
- <title>Examples</title>
- <para>In this example the schema defines multiple types.</para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
<entry>
- <para>cat student</para>
- <para>John   18    4.0</para>
- <para>Mary   19    3.8</para>
- <para>Bill   20    3.9</para>
- <para>Joe   18    3.8</para>
- <para/>
- <para>A = LOAD 'student' AS (name:chararray, age:int, gpa:float);</para>
- <para/>
- <para>DESCRIBE A;</para>
- <para>A: {name: chararray,age: int,gpa: float}</para>
- <para/>
- <para>DUMP A:</para>
- <para>(John,18,4.0F)</para>
- <para>(Mary,19,3.8F)</para>
- <para>(Bill,20,3.9F)</para>
- <para>(Joe,18,3.8F)</para>
+ <para>[open#apache]</para>
</entry>
</row></tbody></tgroup>
</informaltable>
- <para>In this example field "gpa" will default to bytearray because no type is declared. </para>
- <informaltable frame="all">
- <tgroup cols="1"><tbody><row>
- <entry>
- <para>cat student</para>
- <para>John   18    4.0</para>
- <para>Mary   19    3.8</para>
- <para>Bill   20    3.9</para>
- <para>Joe   18    3.8</para>
- <para/>
- <para>A = LOAD 'data' AS (name:chararray, age:int, gpa)</para>
- <para/>
- <para>DESCRIBE A;</para>
- <para>A: {name: chararray,age: int,gpa: bytearray}</para>
- <para/>
- <para>DUMP A;</para>
- <para>(John,18,4.0)</para>
- <para>(Mary,19,3.8)</para>
- <para>(Bill,20,3.9)</para>
- <para>(Joe,18,3.8)</para>
- </entry>
- </row></tbody></tgroup>
- </informaltable></section></section>
-
- <section>
- <title>Schemas for Complex Data Types</title>
- <para>Complex data types include tuples, bags, and maps.</para></section>
+ <para>Note the following general observations about data types:</para>
+ <itemizedlist>
+ <listitem>
+ <para>Use schemas to assign types to fields. Â If you don't assign types, fields default to type bytearray and implicit conversions are applied to the data depending on the context in which that data is used. For example, in relation B, f1 is converted to integer because 5 is integer. In relation C, f1 and f2 are converted to double because we don't know the type of either f1 or f2.</para>
+ <programlisting>
+A = LOAD 'data' AS (f1,f2,f3);
+B = FOREACH A GENERATE f1 + 5;
+C = FOREACH A generate f1 + f2;
+</programlisting>
+ </listitem>
+ </itemizedlist>
+
+ <itemizedlist>
+ <listitem>
+ <para>If a schema is defined as part of a load statement, the load function will attempt to enforce the schema. If the data does not conform to the schema, the loader will generate a null value or an error.</para>
+ <programlisting>
+A = LOAD 'data' AS (name:chararray, age:int, gpa:float);
+</programlisting>
+ </listitem>
+ </itemizedlist>
+ <para/>
+ <itemizedlist>
+ <listitem>
+ <para>If an explicit cast is not supported, an error will occur. For example, you cannot cast a chararray to int.</para>
+ <programlisting>
+A = LOAD 'data' AS (name:chararray, age:int, gpa:float);
+B = FOREACH A GENERATE (int)name;
+
+<emphasis>This will cause an error â¦</emphasis></programlisting>
+ </listitem>
+ </itemizedlist>
+
+ <para/>
+ <itemizedlist>
+ <listitem>
+ <para>If Pig cannot resolve incompatible types through implicit casts, an error will occur. For example, you cannot add chararray and float (see the Types Table for addition and subtraction).</para>
+ <programlisting>
+A = LOAD 'data' AS (name:chararray, age:int, gpa:float);
+B = FOREACH A GENERATE name + gpa;
+
+<emphasis>This will cause an error â¦</emphasis></programlisting>
+ </listitem>
+ </itemizedlist>
+ </section>
<section>
- <title>Tuple Schema</title>
+ <title>Tuple</title>
<para>A tuple is an ordered set of fields.</para>
<section>
- <title>Syntax</title>
+ <title>Syntax </title>
<informaltable frame="all">
<tgroup cols="1"><tbody><row>
<entry>
- <para>alias[:tuple] (alias[:type]) [, (alias[:type]) â¦] )</para>
+ <para>( field [, field â¦] )  </para>
</entry>
</row></tbody></tgroup>
</informaltable></section>
@@ -1571,101 +925,49 @@
<informaltable frame="all">
<tgroup cols="2"><tbody><row>
<entry>
- <para>alias</para>
- </entry>
- <entry>
- <para>The name assigned to the tuple.</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>:tuple</para>
- </entry>
- <entry>
- <para>(Optional) The data type, tuple (case insensitive).</para>
- </entry>
- </row>
- <row>
- <entry>
- <para>( )</para>
+ <para>( Â )</para>
</entry>
<entry>
- <para>The designation for a tuple, a set of parentheses.</para>
+ <para>A tuple is enclosed in parentheses ( ).</para>
</entry>
</row>
<row>
<entry>
- <para>alias[:type]</para>
+ <para>field</para>
</entry>
<entry>
- <para>The constituents of the tuple, where the schema definition rules for the corresponding type applies to the constituents of the tuple:</para>
- <orderedlist>
- <listitem>
- <para>alias â the name assigned to the field</para>
- </listitem>
- <listitem>
[... 3381 lines stripped ...]