Apache Pig Explained

Apache Pig
Developer:Apache Software Foundation, Yahoo Research
Latest Release Version:0.17.0
Operating System:Microsoft Windows, OS X, Linux
Genre:Data analytics
License:Apache License 2.0

Apache Pig[1] is a high-level platform for creating programs that run on Apache Hadoop. The language for this platform is called Pig Latin. Pig can execute its Hadoop jobs in MapReduce, Apache Tez, or Apache Spark.[2] Pig Latin abstracts the programming from the Java MapReduce idiom into a notation which makes MapReduce programming high level, similar to that of SQL for relational database management systems. Pig Latin can be extended using user-defined functions (UDFs) which the user can write in Java, Python, JavaScript, Ruby or Groovy[3] and then call directly from the language.

History

Apache Pig was originally[4] developed at Yahoo Research around 2006 for researchers to have an ad hoc way of creating and executing MapReduce jobs on very large data sets. In 2007,[5] it was moved into the Apache Software Foundation.

VersionOriginal release dateLatest versionRelease date[6]
2008-09-110.1.12008-12-05
2009-04-080.2.02009-04-08
2009-06-250.3.02009-06-25
2009-08-290.4.02009-08-29
2009-09-290.5.02009-09-29
2010-03-010.6.02010-03-01
2010-05-130.7.02010-05-13
2010-12-170.8.12011-04-24
2011-07-290.9.22012-01-22
2012-01-220.10.12012-04-25
2013-02-210.11.12013-04-01
2013-10-140.12.12014-04-14
2014-07-040.13.02014-07-04
2014-11-200.14.02014-11-20
2015-06-060.15.02015-06-06
2016-06-080.16.02016-06-08
2017-06-190.17.02017-06-19

Naming

Regarding the naming of the Pig programming language, the name was chosen arbitrarily and stuck because it was memorable, easy to spell, and for novelty.[7] [8] [9]

Example

Below is an example of a "Word Count" program in Pig Latin:

input_lines = LOAD '/tmp/my-copy-of-all-pages-on-internet' AS (line:chararray); -- Extract words from each line and put them into a pig bag -- datatype, then flatten the bag to get one word on each row words = FOREACH input_lines GENERATE FLATTEN(TOKENIZE(line)) AS word; -- filter out any words that are just white spaces filtered_words = FILTER words BY word MATCHES '\\w+'; -- create a group for each word word_groups = GROUP filtered_words BY word; -- count the entries in each group word_count = FOREACH word_groups GENERATE COUNT(filtered_words) AS count, group AS word; -- order the records by count ordered_word_count = ORDER word_count BY count DESC; STORE ordered_word_count INTO '/tmp/number-of-words-on-internet';

The above program will generate parallel executable tasks which can be distributed across multiple machines in a Hadoop cluster to count the number of words in a dataset such as all the webpages on the internet.

Pig vs SQL

In comparison to SQL, Pig

  1. has a nested relational model,
  2. uses lazy evaluation,
  3. uses extract, transform, load (ETL),
  4. is able to store data at any point during a pipeline,
  5. declares execution plans,
  6. supports pipeline splits, thus allowing workflows to proceed along DAGs instead of strictly sequential pipelines.

On the other hand, it has been argued DBMSs are substantially faster than the MapReduce system once the data is loaded, but that loading the data takes considerably longer in the database systems. It has also been argued RDBMSs offer out of the box support for column-storage, working with compressed data, indexes for efficient random data access, and transaction-level fault tolerance.[10]

Pig Latin is procedural and fits very naturally in the pipeline paradigm while SQL is instead declarative. In SQL users can specify that data from two tables must be joined, but not what join implementation to use (You can specify the implementation of JOIN in SQL, thus "... for many SQL applications the query writer may not have enough knowledge of the data or enough expertise to specify an appropriate join algorithm."). Pig Latin allows users to specify an implementation or aspects of an implementation to be used in executing a script in several ways. In effect, Pig Latin programming is similar to specifying a query execution plan, making it easier for programmers to explicitly control the flow of their data processing task.[11]

SQL is oriented around queries that produce a single result. SQL handles trees naturally, but has no built in mechanism for splitting a data processing stream and applying different operators to each sub-stream. Pig Latin script describes a directed acyclic graph (DAG) rather than a pipeline.

Pig Latin's ability to include user code at any point in the pipeline is useful for pipeline development. If SQL is used, data must first be imported into the database, and then the cleansing and transformation process can begin.[12]

See also

Notes and References

  1. Web site: Hadoop: Apache Pig. Sep 2, 2011.
  2. Web site: [PIG-4167] Initial implementation of Pig on Spark - ASF JIRA]. issues.apache.org. 2018-12-29.
  3. Web site: Pig user defined functions. May 3, 2013.
  4. Web site: Yahoo Blog:Pig – The Road to an Efficient High-level language for Hadoop. May 23, 2015. dead. https://web.archive.org/web/20160203181220/https://developer.yahoo.com/blogs/hadoop/pig-road-efficient-high-level-language-hadoop-413.html. February 3, 2016.
  5. Web site: Pig into Incubation at the Apache Software Foundation. May 23, 2015. dead. https://web.archive.org/web/20160203162733/https://developer.yahoo.com/blogs/hadoop/pig-incubation-apache-software-foundation-393.html. February 3, 2016.
  6. Web site: Apache Pig Releases. Apache. 2019-03-13.
  7. Web site: 1. What Is Pig? - Programming Pig, 2nd Edition [Book]]. 2021-08-01. www.oreilly.com. en.
  8. Book: Gates, Alan. Programming Pig. 2016. Daniel Dai. 978-1-4919-3706-8. Second. Sebastopol, CA. 964523786.
  9. Pig mascot questions . 2021-07-27 . 1 August 2021 . Pig User Mailing List . Gates . Alan . en . https://web.archive.org/web/20210801002756/https://www.mail-archive.com/user%40pig.apache.org/msg10825.html . 1 August 2021.
  10. Web site: Communications of the ACM: MapReduce and Parallel DBMSs: Friends or Foes?. May 23, 2015. dead. https://web.archive.org/web/20150701205317/http://database.cs.brown.edu/papers/stonebraker-cacm2010.pdf. July 1, 2015.
  11. Web site: ACM SigMod 08: Pig Latin: A Not-So-Foreign Language for Data Processing. May 23, 2015.
  12. Web site: Yahoo Pig Development Team: Comparing Pig Latin and SQL for Constructing Data Processing Pipelines. May 23, 2015. dead. https://web.archive.org/web/20150530103839/https://developer.yahoo.com/blogs/hadoop/comparing-pig-latin-sql-constructing-data-processing-pipelines-444.html. May 30, 2015.