100 likes | 308 Vues
(Hadoop) Pig Dataflow Language. B. Ramamurthy Based on Cloudera’s tutorials and Apache’s Pig Manual. Apache Pig.
E N D
(Hadoop) Pig Dataflow Language B. Ramamurthy Based on Cloudera’s tutorials and Apache’s Pig Manual
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.
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
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.
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
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'
Lets run Pig Script on AWS • See tutorial at http://aws.amazon.com/articles/2729 • This is about parsing web log for frequent external referrers, IPs, frequent terms etc. • You place the data and pig script on s3 • Then start the pig workflow on aws • The output also goes back into the s3 space
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';
More examples from Cloudera • http://www.cloudera.com/wp-content/uploads/2010/01/IntroToPig.pdf • Also see Apache’s pig page: • http://pig.apache.org/docs/r0.9.1/index.html