1 / 37

Pig Data flow language (abstraction for MR jobs)

Learn about Pig, a data flow language that simplifies the design and execution of MapReduce jobs for big data analysis. Explore how Pig translates scripts into MR jobs and enables parallel execution of data analysis tasks. Discover the key features and use cases of Pig, as well as its advantages over SQL and other data processing tools.

rwheatley
Download Presentation

Pig Data flow language (abstraction for MR jobs)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pig Data flow language (abstraction for MR jobs) B. Ramamurthy

  2. Abstraction layer for MR • Raw MR is difficult to install and program (Do we know about this? Then why did I ask you do this?) • There are many models that simplify designing MR applications: • MR made easy from Google App Engine • MRJob for python developers • Elastic Map Reduce (EMR) from amazon aws • Pig from Apache via Yahoo • Hive from Apache via Facebook • And others • We will look at Pig in detail.. It is data flow language, so conceptually closer to the way we solve problems…

  3. What is Pig?: example • Pig is a scripting language that helps in designing big data solutions using high level primitives. • Pig script can be executed locally; it is typically translated into MR job/task workflow and executed on Hadoop • Pig itself is a MR job on Hadoop • You can access local file system using pig –x local (eg. file:/…) • Other file system accessible are hdfs:// and s3:// from grunt> of non-local pig • You can transfer data into local file system from s3: hadoop dfs –copyToLocal s3n://cse487/pig1/ps5.pig /home/hadoop/pig1/ps5.pig hadoop dfs –copyToLocal s3n://cse487/pig1/data2 /home/hadoop/pig1/data2 Then run ps5.pig in the local mode pig –x local run ps5.pig

  4. Simple pig scripts: wordcount A = load 'data2' as (line); words = foreach A generate flatten(TOKENIZE(line)) as word; grpd = group words by word; cntd = foreachgrpd generate group, COUNT(words); store cntd into 'pig1out';

  5. Sample Pig script: simple data analysis 2 4 5 -2 3 4 3 5 6 -4 5 7 -7 4 6 • 45 A = LOAD 'data3' AS (x,y,z); B = FILTER A by x> 0; C = GROUP B BY x; D = FOREACH C GENERATE group,COUNT(B); STORE D INTO 'p6out';

  6. See the pattern? • LOAD • FILTER • GROUP • GENERATE (apply some function from piggybank) • STORE (DUMP for interactive debugging)

  7. Pig Latin • Is the language pig script is written in. • Is a parallel data flow language • Mathematically pig latin describes a directed acyclic graph (DAG) where edges are data flow and the nodes are operators that process data • It is data flow not control flow language: no if statements and for loops! (traditional OO programming describes control flow not data flow.)

  8. Pig and query language • How about Pig and SQL? • SQL describes “what” or what is the user’s question and it does NOT describes how it is to be solved. • SQL is built around answering one question: lots of subqueries and temporary tables resulting in one thing: inverted process • remember from our earlier discussions if these temp table are NOT in-memory their random access is expensive • Pig describes the data pipeline from first step to final step. • HDFS vs RDBMS Tables • Pig vs Hive • Yahoo vs Facebook

  9. SQL (vs. Pig) CREATE TEMP TABLE t1 AS SELECT customer, sum(purchase) AS total_purchases FROM transactions GROUP BY customer; SELECT customer, total_purchases,zipcode FROM t1, customer_profile WHERE t1.customer = customer_profile.customer;

  10. (SQL vs.) Pig txns = load ‘transactions’ as (customer, purchase) grouped = group txns customer; total = foreach grouped generate group, SUM(txns.purchase) as tp; profile = load ‘customer_profile’ as (customer, zipcode); answer = join total by group, profile by customer; dump answer;

  11. Pig and HDFS and MR • Pig does not require HDFS. • Pig can run on any file system as long as you transfer the data flow and the data appropriately. • This is great since you can use not just file:// or hdfs:// but also other systems to be developed in the future. • Similarly Pig Latin has several advantages over MR (see chapter 1 Programming Pig book) during the conceptual phase.. For later execution on MR

  12. Uses of Pig • Traditional Extract, Transform, Load (ETL) data pipelines • Research on raw data • Iterative processing • Prototyping (debugging) on small data and local system before launching a big data, multi-node MR jobs • Good for EDA!! • Largest use case: data pipelines: raw data , cleanse, load into data warehouse • Ad-hoc queries from data where the scheme is unknown • What it is not good for? For workloads that will update a few records, the will look up data in some random order, Pig is not a good choice. • In 2009, 50% yahoo! Jobs executed were using Pig. • Lets execute some Pig scripts on local installation and then on amazon installation.

  13. Apache Pig • Apache Pig is a platform for analyzing large data sets that consists of a high-level language for expressing data analysis programs, coupled with infrastructure for evaluating these programs. • Pig's infrastructure layer consists of • a compiler that produces sequences of Map-Reduce programs, • Pig's language layer currently consists of a textual language called Pig Latin, which has the following key properties: • Ease of programming. It is trivial to achieve parallel execution of simple, "embarrassingly parallel" data analysis tasks. Complex tasks comprised of multiple interrelated data transformations are explicitly encoded as data flow sequences, making them easy to write, understand, and maintain. • Optimization opportunities. The way in which tasks are encoded permits the system to optimize their execution automatically, allowing the user to focus on semantics rather than efficiency. • Extensibility. Users can create their own functions to do special-purpose processing.

  14. Running Pig • You can execute Pig Latin statements: • Using grunt shell or command line $ pig ... - Connecting to ... grunt> A = load 'data'; grunt> B = ... ; • In local mode or hadoop mapreduce mode $ pig myscript.pig Command Line - batch, local mode mode $ pig -x local myscript.pig • Either interactively or in batch

  15. Program/flow organization • A LOAD statement reads data from the file system. • A series of "transformation" statements process the data. • A STORE statement writes output to the file system; or, a DUMP statement displays output to the screen.

  16. Interpretation • In general, Pig processes Pig Latin statements as follows: • First, Pig validates the syntax and semantics of all statements. • Next, if Pig encounters a DUMP or STORE, Pig will execute the statements. A = LOAD 'student' USING PigStorage() AS (name:chararray, age:int, gpa:float); B = FOREACH A GENERATE name; DUMP B; (John) (Mary) (Bill) (Joe) • Store operator will store it in a file

  17. Simple Examples A = LOAD 'input' AS (x, y, z); B = FILTER A BY x > 5; DUMP B; C = FOREACH B GENERATE y, z; STORE C INTO 'output'; ----------------------------------------------------------------------------- A = LOAD 'input' AS (x, y, z); B = FILTER A BY x > 5; STORE B INTO 'output1'; C = FOREACH B GENERATE y, z; STORE C INTO 'output2'

  18. Pig Script register file:/home/hadoop/lib/pig/piggybank.jar DEFINE EXTRACT org.apache.pig.piggybank.evaluation.string.EXTRACT(); RAW_LOGS = LOAD '$INPUT' USING TextLoader as (line:chararray); LOGS_BASE = foreach RAW_LOGS generate FLATTEN ( EXTRACT (line, '^(\\S+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] "(.+?)" (\\S+) (\\S+) "([^"]*)" "([^"]*)"') ) as ( remoteAddr:chararray, remoteLogname:chararray, user:chararray, time:chararray, request:chararray, status:int, bytes_string:chararray, referrer:chararray, browser:chararray ) ; REFERRER_ONLY = FOREACH LOGS_BASE GENERATE referrer; FILTERED = FILTER REFERRER_ONLY BY referrer matches '.*bing.*' OR referrer matches '.*google.*'; SEARCH_TERMS = FOREACH FILTERED GENERATE FLATTEN(EXTRACT(referrer, '.*[&\\?]q=([^&]+).*')) as terms:chararray; SEARCH_TERMS_FILTERED = FILTER SEARCH_TERMS BY NOT $0 IS NULL; SEARCH_TERMS_COUNT = FOREACH (GROUP SEARCH_TERMS_FILTERED BY $0) GENERATE $0, COUNT($1) as num; SEARCH_TERMS_COUNT_SORTED = LIMIT(ORDER SEARCH_TERMS_COUNT BY num DESC) 50; STORE SEARCH_TERMS_COUNT_SORTED into '$OUTPUT';

  19. More examples from Cloudera • http://www.cloudera.com/wp-content/uploads/2010/01/IntroToPig.pdf A very nice presentation from Cloudera… • Also see Apache’s pig page: • http://pig.apache.org/docs/r0.9.1/index.html

  20. Pig’s data model • Scalar types: int, long, float (early versions, recently float has been dropped), double, chararray, bytearray • Complex types: Map, Tuple, Bag • Map: chararray to any pig element; in fact , this <key> to <value> mapping; map constants [‘name’#’bob’, ‘age’#55] will create a map with two keys name and age, first value is chararray and the second value is an integer. • Tuple: is a fixed length ordered collection of Pig data elements. Equivalent to a row in SQL. Order, can refer to elements by field position. (‘bob’, 55) is a tuple with two fields. • Bag: unordered collection of tuples. Cannot reference tuple by position. Eg. {(‘bob’,55), (‘sally’,52), (‘john’, 25)} is a bog with 3 tuples; bags may become large and may spill into disk from “in-memory” • Null: unknown, data missing; any data element can be null; (In Java it is Null pointers… the meaning is different in Pig)

  21. Pig schema • Very relaxed wrt schema. • Scheme is defined at the time you load the data • Runtime declaration of schemas is really nice. • You can operate without meta data. • On the other hand, meta data can be stored in a repository Hcatalog and used. For example JSON format… etc. • Gently typed: between Java and Perl at two extremes

  22. Schema Definition divs = load ‘NYSE_dividends’ as (exchange:chararray, symbol:chararray, date:chararray, dividend:double); Or if you are lazy divs = load ‘NYSE_dividends’ as (exchange, symbol, date, dividend); But what if the data input is really complex? Eg. JSON objects? One can keep a scheme in the HCatalog (apache incubation), a meta data repository for facilitating reading/loading input data in other formats. divs = load ‘mydata’ using HCatLoader();

  23. Pig Latin • Basics: keywords, relation names, field names; • Keywords are not case sensitive but relation and fields names are! User defined functions are also case sensitive • Comments /* */ or single line comment – • Each processing step results in data • Relation name = data operation • Field names start with alphabet

  24. More examples • No pig-schema daily = load ‘NYSE_daily’; calcs = foreach daily generate $7/100.0, SUBSTRING($0,0,1), $6-$3); Here – is only numeric on Pig) • No-schema filter daily = load ‘NYSE_daily’; fltrd = filter daily by $6 > $3; Here > is allowed for numeric, bytearray or chararray.. Pig is going to guess the type! • Math (float cast) daily = load ‘NYSE_daily’ as (exchange, symbol, date, open, high:float,low:float, close, volume:int, adj_close); rough = foreach daily generate volume * close; -- will convert to float Thus the free “typing” may result in unintended consequences.. Be aware. Pig is sometimes stupid. For a more in-depth view look at also how “casts” are done in Pig.

  25. Load (input method) • Can easily interface to hbase: read from hbase • using clause • divs = load ‘NYSE_dividends’ using HBaseStorage(); • divs = load ‘NYSE_dividends’ using PigStorage(); • divs = load ‘NYSE_dividends’ using PigStorage(,); • as clause • daily = load ‘NYSE_daily’ as (exchange, symbol, date, open, high,low,close, volume);

  26. Store & dump • Default is PigStorage (it writes as tab separated) • store processed into ‘/data/example/processed’; • For comma separated use: • store processed into ‘/data/example/processed’ using PigStorage(,); • Can write into hbase using HBaseStorage(): • store ‘processed’ using into HBaseStorage(); • Dump for interactive debugging, and prototyping

  27. Relational operations • Allow you to transform by sorting, grouping, joining, projecting and filtering • foreach supports as array of expressions: simplest is constants and field references. rough = foreach daily generate volume * close; calcs = foreach daily generate $7/100.0, SUBSTRING($0,0,1), $6-$3); • UDF (User Defined Functions) can also be used in expressions • Filter operation CMsyms = filter divs by symbol matches ‘CM*’;

  28. Operations (cntd) • Group operation collects together records with the same key. • grpd = group daily by stock; -- output is <key, bag> • counts = foreachgrpd generate group, COUNT(daily); • Can also group by multiple keys • grpd = group daily by (stock, exchange); • Group forces the “reduce” phase of MR • Pig offers mechanism for addressing data skew and unbalanced use of reducers (we will not worry about this now) • Order by: strict ordering • Maps, tuples, bags

  29. Pig from Alan Gates’ book(In preparation for exam2)

  30. Introduction • Download pig from pig.apache.org (into timberlake or your local computer/laptop) • Unzip and untar it. You are set to go. • You can execute in local mode for learning purposes. Later on you can test it on your hadoop installation. • Navigate to the director where pig is installed. ./bin/pig –x local • Will put you in grunt mode or local mode

  31. Data and pig Script • Create a data (called data) directory in the directory where bin is located. • Download from github all the data files related to pig book and store in the data directory • NYSE_divdidends • NYSE_daily • Etc. • Now go thru’ the examples in chapters 1-4, either by typing them in line by line or by creating script files. • Mystockanalysis.pig can be executed by • ./bin/pig –x local Mystockanalysis.pig or line by line on grunt

  32. Chapter 1 • Hello world of pig. • Mary had little lamb example. • Go through the example in page.3 • Create “mary” file in your data directory • Type in the commands line by line as in p.3 • Now create a ch1.pig file out of the commands • Run the script file using the pig command • Try some other commands not listed there. • Understand the examples discussed in p.5,6

  33. Chapter 2 • Discusses installing and running pig • Go through the example in p.14. • That’s all.

  34. Chapter 3 • Discuss the grunt shell that is the prompt for the local mode • pig –x local • Results in grunt grunt> • See the example in page 20

  35. Chapter 4 • Pig data model • Scalars like: int, long, float, double, etc. • Complex types: Map, chararray to element mapping, sort of like key, value pair • Tuple ordered collection of Pig elements (‘bob, 55) • Bag is an unordered collection of tuples • Nulls • Schemas: Pig has lax attitude towards schemas • Explicit: • dividends = load ‘NYSE_dividends’ as (exchange:chararray, symbol:chararray, date: chararray, dividend:float); • Or you could say • divs = load ‘NYSE_dividends’ as (exchange, symbol, date, dividend); • See the table on page 28 • See the example p.28,29,30.

  36. Chapter 5 • Pig Latin • Look at the examples p.33-50 • Commands discussed are: • Load, store, dump • Relational operations: foreach, filter, group, order ..by, distinct, join • Data operation: limit, sample, parallel.

More Related