Slide 1

Slide 1 text

Map, Reduce, AWK! @markwunsch

Slide 2

Slide 2 text

$ ls -l s3log.txt -rw-r--r-- 1 mwunsch RTRHQ\Domain Users 13553855 May 16 14:08 s3log.txt

Slide 3

Slide 3 text

13553855

Slide 4

Slide 4 text

13M

Slide 5

Slide 5 text

That’s BIG DATA right?

Slide 6

Slide 6 text

I’m going to need an Hadoop.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

What about small data?

Slide 9

Slide 9 text

What about small data?

Slide 10

Slide 10 text

small data

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Alfred Aho Peter Weinberger Brian Kernighan

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Alfred V. Aho "AWK is a language for processing text files. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.”

Slide 15

Slide 15 text

AWK - a language for processing text files - each line is a record - each line is broken up into a sequence of fields - pattern-action statements - for each pattern that matches, the associated action is executed

Slide 16

Slide 16 text

condition { action } An AWK program is of a sequence of pattern-action statements.

Slide 17

Slide 17 text

awk

Slide 18

Slide 18 text

awk nawk gawk mawk jawk

Slide 19

Slide 19 text

YOU ALREADY HAVE IT!

Slide 20

Slide 20 text

docs.aws.amazon.com/AmazonS3/latest/dev/LogFormat.html Field Name Example Bucket Owner 79a59df900b949e55d96a1e6… Bucket mybucket Time [06/Feb/2014:00:00:38 +0000] Remote IP 192.0.2.3 Requester 79a59df900b949e55d96a1e698f… Request ID 3E57427F33A59F07 Operation REST.PUT.OBJECT Key /photos/2014/08/puppy.jpg Request-URI "GET /mybucket/photos/2014/08/puppy.jpg?x-foo=bar" HTTP status 200 Error Code NoSuchBucket Bytes Sent 2662992 Object Size 3462992 Total Time 70 Turn-Around Time 10 Referrer "http://www.amazon.com/webservices" User-Agent "curl/7.15.1" Version Id 3HL4kqtJvjVBH40Nrjfkd

Slide 21

Slide 21 text

httpd.apache.org/docs/trunk/logs.html#accesslog "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" Apache Combined Log Format

Slide 22

Slide 22 text

Field Name Example Apache Format String Bucket Owner 79a59df900b949e55d96a1e6… Bucket mybucket Time [06/Feb/2014:00:00:38 +0000] %t Remote IP 192.0.2.3 %h Requester 79a59df900b949e55d96a1e698f… %u Request ID 3E57427F33A59F07 Operation REST.PUT.OBJECT Key /photos/2014/08/puppy.jpg Request-URI "GET /mybucket/photos/2014/08…” \”%r\” HTTP status 200 %>s Error Code NoSuchBucket Bytes Sent 2662992 %b Object Size 3462992 Total Time 70 Turn-Around Time 10 Referrer “http://www.amazon.com/…” \”%{Referer}i\” User-Agent "curl/7.15.1" \”%{User-agent}i\” Version Id 3HL4kqtJvjVBH40Nrjfkd

Slide 23

Slide 23 text

Map()

Slide 24

Slide 24 text

{ action } condition

Slide 25

Slide 25 text

{ action }

Slide 26

Slide 26 text

{ print }

Slide 27

Slide 27 text

Each line is broken up into a sequence of fields… 3252c3… www.abstractfactory.tv [09/May/2

Slide 28

Slide 28 text

Each line is broken up into a sequence of fields… FS 0897 10897 18 17 "-" "Podcasts/2.0.2" -

Slide 29

Slide 29 text

$1 3252c3… $2 www.abstractfactory.tv $3 [09/May/2014:15:58:01 $4 +0000] $5 64.124.28.146 $6 - $7 6EB0563A2F14BD4A $8 WEBSITE.GET.OBJECT $9 feed.xml $10 "GET $11 /feed.xml $12 HTTP/1.1" $13 200 $14 - $15 10897 $16 10897 $17 18 $18 17 $19 "-" $20 "Podcasts/2.0.2" $21 -

Slide 30

Slide 30 text

{ print $5,“-”,$6,$3,$4,$10,$11,$12,$13,$15,$19,$20; } Apache Combined Log Format

Slide 31

Slide 31 text

{ print $5,“-”,$6,$3,$4,$10,$11,$12,$13,$15,$19,$20; } Apache Combined Log Format

Slide 32

Slide 32 text

"Castro/39 (iPhone; iOS 7.1.1; Scale/2.00)" Apache Combined Log Format User Agent FS FS FS FS

Slide 33

Slide 33 text

{ ua=$20 ! ! ! print $5,“-”,$6,$3,$4,$10,$11,$12,$13,$15,$19,ua; } Apache Combined Log Format for (i=21; i

Slide 34

Slide 34 text

{ ua=$20 for (i=21; i

Slide 35

Slide 35 text

BEGIN { FS=“\””; } ($2 ~ /feed\.xml/) { print $6; }

Slide 36

Slide 36 text

awk -F\” ‘($2 ~ /feed\.xml/) {print $6}’

Slide 37

Slide 37 text

awk -F\” ‘($2 ~ /feed\.xml/) {print $6}’ | sort | uniq -c | sort -fr

Slide 38

Slide 38 text

16 Castro/39 (iPhone; iOS 7.1.1; Scale/2.00) 9 Feedbin - 2 subscribers 7 iTunes/10.7 Downcast/2.8.18.1003 3 Podcasts/2.0.2 3 Instacast/4.5.2 (like iTunes/10.1.2) 2 StitcherBot (MP3 Search Bot for Stitcher Personalized Radio Service) 2 Overcast/1.0 Podcast Sync (http://overcast.fm/) 2 Mozilla/5.0 (compatible; Linux x86_64; Mail.RU_Bot/2.0; +http:// go.mail.ru/help/robots) 2 iTunes/11.1.5 (Macintosh; OS X 10.9.2) AppleWebKit/537.75.14 1 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0 1 iTunes/11.1.5 (Macintosh; OS X 10.7.5) AppleWebKit/534.57.7 1 iTunes/10.7 Downcast/2.8.17.1003 1 Apache-HttpClient/UNAVAILABLE (java 1.4) tail -50 s3logs.txt | awk -F\” ‘($2 ~ /feed\.xml/) {print $6}’ | sort | uniq -c | sort -fr

Slide 39

Slide 39 text

Reduce()

Slide 40

Slide 40 text

($9 == 206) { addresses[$1] += $10; } END { for (x in addresses) { print x, addresses[x]; } } $9 => Status Code 206 => HTTP Partial Content $1 => Ip Address $10 => Bytes

Slide 41

Slide 41 text

awk ‘{ sum += $2 } END { print sum }’

Slide 42

Slide 42 text

sort | uniq | wc -l

Slide 43

Slide 43 text

Map Reduce AWK

Slide 44

Slide 44 text

AWK(1) AWK(1) awk NAME awk - pattern-directed scanning and processing language SYNOPSIS awk [ -F fs ] [ -v var=value ] [ ‘prog’ | -f progfile ] [ file … ]

Slide 45

Slide 45 text

Great Auks John James Audobon, The Birds of America

Slide 46

Slide 46 text

^D