a short intro to go

25
A Short Intro to Go CS 240

Upload: others

Post on 11-Apr-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Short Intro to Go

A Short Intro to GoCS 240

Page 2: A Short Intro to Go

“Go is acompiled,

concurrent,statically typed,

garbage-collectedlanguage developed at Google”

- Rob Pike, 2012

What’s this Weird Language I’ve Never Heard of?

Rob Pike is the Jeff Dean of distributed systems. Here’s the article the quote is from: https://talks.golang.org/2012/splash.article2

Page 3: A Short Intro to Go

compiled Like C, C++concurrent Like Erlangstatically typed Like C, C++, Javagarbage-collected Like Java and Python

What’s this Weird Language I’ve Never Heard of?

3

Page 4: A Short Intro to Go

Why Not Use Python, Java, C++, etc?

Built for Systems.Go preserves efficiency but has good abstractions.

Easy multi threading and IO communication.

Develop quicklyDo many things efficiently and at the same time

4

Page 5: A Short Intro to Go

Official Self Reported List of Companies that use Go: https://github.com/golang/go/wiki/GoUsers

Seems Google Specific. Who Else Actually Uses it?

5

Page 6: A Short Intro to Go

Why did they Choose Go?

“We built everything in Python because it was easy, but now it’s slow. So we switched to Go.”

- Most companies using Go

6

Page 7: A Short Intro to Go

But How do I Use Go?Start here:

https://tour.golang.org/list

Didn’t install Go? Use the web IDE:https://play.golang.org/

Other Resources:Go for Pythonists

https://talks.golang.org/2013/go4python.slide#1

Go for Distributed Systemshttps://talks.golang.org/2013/distsys.slide#1

Official Go Talkshttps://github.com/golang/go/wiki/GoTalks

7

Page 8: A Short Intro to Go

But How do I Use Go?

DEMO: go tour

8

Page 9: A Short Intro to Go

Build Software for Any System

go build file.goCompile an executable for your machine

env GOOS=windows GOARCH=amd64 go build file.goCompile an executable for Windows with 64 bit processor

9

Page 10: A Short Intro to Go

Format your Code

COMMAND WHAT IT DOESgofmt file.go Format the file.go properly

DEMO: gofmt

10

Page 11: A Short Intro to Go

Wait, I Have Questions!

Go’s official“Frequently Asked Questions (FAQ)”

https://golang.org/doc/faq

11

Page 12: A Short Intro to Go

MapReduceCS 240

12

Page 13: A Short Intro to Go

Map Reduce

Wikipedia:“MapReduce is a programming model and an associated implementation for processing and generating big data sets with a parallel, distributed algorithm on a cluster.”

In other words, a general and scalable solution to deal with big data computation on multiple machines.

13

Page 14: A Short Intro to Go

Abstract Map Reduce

map(key, value) -> list(<k’, v’>) • Apply function to (key, value) pair • Outputs set of intermediate pairs

reduce(key, list<value>) -> <k’, v’> • Applies aggregation function to values • Outputs result

Split and distribute data

Aggregate and compute

results

14

Page 15: A Short Intro to Go

Word Count – The Hello World of Map Reduce

Bus Car TrainTrain Plane CarBus Bus Plane

Bus Car Train

Train Plane Car

Bus Bus Plane

Bus 1Car 1

Train 1

Train 1Plane 1Car 1

Bus 2Plane 1

Bus 2Bus 1

Car 1Car 1

Train 1Train 1

Plane 1Plane 1

Bus 3

Car 2

Train 2

Plane 2

Bus 3Car 2

Train 2Plane 2

Splitting Mapping IntermediateSplitting Reducing Combining

doMap() doReduce()

15

Page 16: A Short Intro to Go

A Motivating Problem for Map Reduce

“Find me the closest Starbucks to KAUST.Actually, I’ll give you a place and something to look for,

and you find me the closest one.Here’s a 1 TB text file … good luck”

GPS Coordinates Site Name[22.3, 39.1] Tim Hortons[22.2, 39.1] KAUST Library[35.7, 139.7] Starbucks... ...

In KAUSTIn Tokyo, Japan

16

Page 17: A Short Intro to Go

A Motivating Problem for Map Reduce

(0,0) (0,1) (0,2) (0,3) (0,4)

(1,0) (1,1) (1,2) (1,3) (1,4)

(2,0) (2,1) (2,2) (2,3) (2,4)

(3,0) (3,1) (3,2) (3,3) (3,4)

0 1 2 3 4

0

3

2

1

GPS Coordinates Site Name[22.3, 39.1] Tim Hortons[22.2, 39.1] KAUST Library[35.7, 139.7] Starbucks... ...

Map to grids

Reduce to single files

17

Page 18: A Short Intro to Go

Split the File and Map Each Chunk Independently (1/2)

GPS Coordinates Site Name[22.3, 39.1] Tim Hortons[22.2, 39.1] KAUST Library[35.7, 139.7] Starbucks... ...[42.0, 69.0] Chanak Train Stop[22.2, 39.2] Burger King... ...... ...... ...... ...

Mapper

Mapper

Mapping Nodes

18

Page 19: A Short Intro to Go

Split the File and Map Each Chunk Independently (2/2)

GPS Coordinates Site Name[22.3, 39.1] Tim Hortons[22.2, 39.1] KAUST Library[35.7, 139.7] Starbucks... ...[42.0, 69.0] Chanak Train[22.2, 39.2] Burger King... ...... ...... ...... ...

Mapper

Mapper

(1,2): [22.3, 39.1] Tim Hortons(1,2): [22.2, 39.1] KAUST Library(1,2): ...(2,4): [35.7, 139.7] Starbucks(2,4): ...

(1,3): [42.0, 69.0] Chanak Train(1,3): ...(1,2): [22.2, 39.2] Burger King(1,2): ...

KEY <grid>: VALUE <locations and name>...

Mapping Nodes

(KEY) can appear in multiple mappers19

Page 20: A Short Intro to Go

Collect the Mapper Results and Reduce to Single Files (1/2)

Reducer(1,2)

Reducer(1,3), (2,4)

(1,2): [22.3, 39.1] Tim Hortons(1,2): [22.2, 39.1] KAUST Library(1,2): ...(2,4): [35.7, 139.7] Starbucks(2,4): ...

(1,3): [42.0, 69.0] Chanak Train(1,3): ...(1,2): [22.2, 39.2] Burger King(1,2): ...

Reducing Nodes

20

Page 21: A Short Intro to Go

Collect the Mapper Results and Reduce to Single Files (2/2)

Reducer(1,2)

Reducer(1,3), (2,4)

(1,2): [22.3, 39.1] Tim Hortons(1,2): [22.2, 39.1] KAUST Library(1,2): ...(2,4): [35.7, 139.7] Starbucks(2,4): ...

(1,3): [42.0, 69.0] Chanak Train(1,3): ...(1,2): [22.2, 39.2] Burger King(1,2): ...

KEY <grid>: [VALUES <locations and names>,...]

Reducing Nodes (1,2): [[22.3, 39.1] Tim Hortons,[22.2, 39.1] KAUST Library,[22.2, 39.2] Burger King...]

]

(2,4): [[35.7, 139.7] Starbucks,...]

(1,3): [[42.0, 69.0] Chanak Train Stop,...],

21

Page 22: A Short Intro to Go

Word Count – The Hello World of Map Reduce

Bus Car TrainTrain Plane CarBus Bus Plane

Bus Car Train

Train Plane Car

Bus Bus Plane

Bus 1Car 1

Train 1

Train 1Plane 1Car 1

Bus 2Plane 1

Bus 2Bus 1

Car 1Car 1

Train 1Train 1

Plane 1Plane 1

Bus 3

Car 2

Train 2

Plane 2

Bus 3Car 2

Train 2Plane 2

Splitting Mapping IntermediateSplitting Reducing Combining

Mapper nodes

Reducer nodes

Task is automatically distributed across five

different nodes22

Page 23: A Short Intro to Go

Hadoop: An open-source implementation

Apache Hadoop is the most popular open-source implementation of MapReduce

Runs on top of a distributed filesystem (HDFS)

Try their MapReduce tutorial:https://hadoop.apache.org/docs/r3.3.1/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html

23

Page 24: A Short Intro to Go

How Hadoop Does it

MapperMapping Nodes

Mapper

ReducerReducing Nodes

Reducer

24

Page 25: A Short Intro to Go

Some Advice for the Assignments

• Write modular code• Use comments (even to yourself)• Don’t forget gofmt (graded)• The clearer your code is, the more

we can help with bugs

25