Docsity

Programming Map - Clouds and Power Aware Computing - Lecture Notes, Study notes of Computer Science

Partial preview of the text

Download Programming Map - Clouds and Power Aware Computing - Lecture Notes and more Study notes Computer Science in PDF only on Docsity!

In Java/Python In Pig+Java Today, we'll start with Pig Two kinds of Map/Reduce programming Programming Map/Reduce Wednesday, February 23, 2011 3:45 PM Docsity.com

We have class tomorrow (Monday's schedule)!

A Map phase in which we do something to each record. A Reduce phase where we combine results. Map/Reduce consists of The Map/Reduce framework does the rest. We only write what to do to each element, and how to combine a list of things. Recall from last time Recall from last time Wednesday, February 23, 2011 3:46 PM Docsity.com

Filter: map selects, reduce combines. Sort: map selects, reduce merge-sorts. Construct: map creates new data, reduce reports what was done. Common Map/Reduce algorithms Common Map/Reduce algorithms Wednesday, February 23, 2011 3:54 PM Docsity.com

No choice on reducer (combine and eliminate duplicates). Very limited map capabilities. You might have noticed that there are severe limits on what you can do with MapReduce directly inside AppEngine: If you write a mapper, it has to be propogated to each map node. This is moving code, not data. This is relatively expensive and somewhat dangerous. Otherwise, you cannot inject code into the cloud. This has security and other implications. Why? Understanding limits of AppEngine MapReduce Tuesday, February 22, 2011 11:46 AM Docsity.com

Map and Reduce are pre-coded. There is no need to propogate code. Google MR is fast because Must propogate jarfile for Map, Combine, Reduce classes to each Hadoop node. Up to 30 seconds for a large array. So, basically no one uses it for real time queries. Hadoop MR has a very slow startup time because Myths and Realities of MR Myths and Realities of MR Tuesday, February 22, 2011 12:26 PM Docsity.com

Often, can compute and pre-store results of commonly needed queries. These run much faster on Hadoop than serially. Then we query the results normally. So, what is Hadoop MR good for? So, what is Hadoop MR good for? Tuesday, February 22, 2011 12:29 PM Docsity.com

A data restructuring subsystem of Hadoop Input is a database. Output is a table. SQL is intended for queries. Input is a set of distributed files. Output is another distributed file. Pig Latin is intended for restructuring data. With its own language Pig Latin with SQL-like syntax. But Pig Pig Tuesday, February 22, 2011 12:30 PM Docsity.com

In local mode , filenames refer to local files. In MapReduce mode , flenames refer to distributed (HDFS) files. Pig runs in two modes: We'll run in local mode for now, but We'll imagine running in MapReduce mode, and Our programs will be identical in either case. Local and MapReduce mode Tuesday, February 22, 2011 3:23 PM Docsity.com

Register the tutorial JAR file so that the included user-defined functions can be called in the script.

  • Use the PigStorage function to load the excite log file (excite.log or excite-small.log) into the โ€œrawโ€ bag as an array of records with the fields user , time , and query.

REGISTER ./tutorial.jar; Use the FILTER-BY construction to call the NonURLDetector user-defined function (Java class in tutorial.jar) to remove records if the query field is empty or a URL.

raw = LOAD 'excite.log' USING PigStorage('\t') AS (user, time, query);

  • Call the ToLower user-defined function to change the query field to lowercase. clean1 = FILTER raw BY org.apache.pig.tutorial.NonURLDetector(query); The excite query log timestamp format is YYMMDDHHMMSS. Call the ExtractHour user-defined function to extract the hour (HH) from the time field.

clean2 = FOREACH clean1 GENERATE user, time, org.apache.pig.tutorial.ToLower(query) as query; Call the NGramGenerator user-defined function to compose the n-grams of the query.

houred = FOREACH clean2 GENERATE user, org.apache.pig.tutorial.ExtractHour(time) as hour, query;

  • Use the DISTINCT command to get the unique n-grams for all records. ngramed1 = FOREACH houred GENERATE user, hour, flatten(org.apache.pig.tutorial.NGramGenerator(query)) as ngram;
  • Use the GROUP command to group records by n-gram and hour. ngramed2 = DISTINCT ngramed1;
  • Use the COUNT function to get the count (occurrences) of each n-gram. hour_frequency1 = GROUP ngramed2 BY (ngram, hour); Use the GROUP command to group records by n-gram only. Each group now corresponds to a distinct n-gram and has the count for each hour.

hour_frequency2 = FOREACH hour_frequency1 GENERATE flatten($0), COUNT($1) as count; For each group, identify the hour in which this n-gram is used with a particularly high frequency. Call the ScoreGenerator user-defined function to calculate a "popularity" score for the n-gram.

uniq_frequency1 = GROUP hour_frequency2 BY group::ngram; uniq_frequency2 = FOREACH uniq_frequency1 GENERATE flatten($0), flatten(org.apache.pig.tutorial.ScoreGenerator($1)); Pig example 1: determine frequency of common search phrases from a webserver logfile. Tuesday, February 22, 2011 12:36 PM Docsity.com

  • Use the FOREACH-GENERATE command to assign names to the fields. flatten(org.apache.pig.tutorial.ScoreGenerator($1)); Use the FILTER-BY command to remove all records with a score less than or equal to 2.0.

uniq_frequency3 = FOREACH uniq_frequency2 GENERATE $1 as hour, $0 as ngram, $ as score, $3 as count, $4 as mean;

  • Use the ORDER-BY command to sort the remaining records by hour and score. filtered_uniq_frequency = FILTER uniq_frequency3 BY score > 2.0; Use the PigStorage function to store the results. The output file contains a list of n- grams with the following fields: hour , ngram , score , count , mean.

ordered_uniq_frequency = ORDER filtered_uniq_frequency BY (hour, score); STORE ordered_uniq_frequency INTO '/tmp/tutorial-results' USING PigStorage(); Pasted from Docsity.com

Make every class in the jarfile available on the command line. (Note: only static functions are available, not methods.) (Try doing this in SQL!) REGISTER ./something.jar The REGISTER command The REGISTER command Tuesday, February 22, 2011 12:56 PM Docsity.com

A field is a piece of data. e.g. (f1,f2,f3). A tuple is a sequence of 1 or more fields in parentheses, A bag is a set of tuples, e.g., {(1,2),(4,5,6)} An outer bag can be referred to by a top-level alias. An inner bag doesn't have an alias. A relation is an outer bag. Data types in Pig: Data types in Pig Tuesday, February 22, 2011 3:45 PM Docsity.com

What Pig does "could be" done by SQL. But it would be incredibly complex SQL. Aliases allow us to write a Pig command in pieces. The result is still one huge (unreadable) command that gets executed, in the end. Aliases and SQL Aliases and SQL Wednesday, February 23, 2011 11:08 AM Docsity.com

prints a relation's contents as text, after computing it. DUMP __ ; prints its schema (metadata). DESCRIBE __ ; prints an explanation of what Pig will do to get it. EXPLAIN __ ; prints a sample run with a subset of data. ILLUSTRATE __ ; Debugging in pig Debugging in pig Tuesday, February 22, 2011 6:01 PM Docsity.com