77
#DevoxxFR #IntroToGraph @Lauren_Schaefer #IntroToGraph a Devoxx France hands-on lab @Lauren_Schaefer 1

Intro to Graph Databases

Embed Size (px)

Citation preview

Page 1: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

#IntroToGrapha Devoxx France hands-on lab

@Lauren_Schaefer

1

Page 2: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

The plan• What are graph databases and why should you care?

• Schema diagrams

• Create

• Read

• Update

• Delete

• Implement a new feature

• Recommendation engines

Page 3: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

All about the workbook• Electronic copy: http://ibm.biz/devoxxfr_workbook

• Learn this!

• Try this!

• Tweet this!

• Get creative

• Additional resources

Page 4: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

What are graph databases?

Page 5: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

What are graph databases?

• NoSQL• Focus on relationships• Nodes and edges instead of tables

Page 6: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Page 7: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Creative Commons: https://www.flickr.com/photos/hjl/4094315135/

Page 8: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you make a graph?

Page 9: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you make a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Page 10: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you make a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Edge (Verb)

Edge (Verb)

Edge (Verb)Edge (Verb)

Page 11: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you make a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Edge (Verb)

Edge (Verb)

Edge (Verb)Edge (Verb)

PropertiesKey: value

PropertiesKey: value

PropertiesKey: value

PropertiesKey: value

PropertiesKey: value Propertie

sKey: value

PropertiesKey: value

PropertiesKey: value

Page 12: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you traverse a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Edge (Verb)

Edge (Verb)

Edge (Verb)Edge (Verb)

Page 13: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you traverse a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Edge (Verb)

Edge (Verb)

Edge (Verb)Edge (Verb)

Page 14: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you traverse a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Edge (Verb)

Edge (Verb)

Edge (Verb)Edge (Verb)

Page 15: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you traverse a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

Edge (Verb)

Edge (Verb)

Edge (Verb)Edge (Verb)

Page 16: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes

Page 17: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes

Page 18: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes

Page 19: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Page 20: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Page 21: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaskalabel: printname: Alaskadescription: Lauren loves this photo even though she wasn't present when the photo was taken. Her husband took this photo on a guy's weekend in Alaska. price: 75imgPath: alaska.jpg

Page 22: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Page 23: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Page 24: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Page 25: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jasonlabel: userfirstName: JasonlastName: Schaeferusername: jasonemail: [email protected]

Page 26: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Page 27: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 28: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 29: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

label: buysdatetime: 2016-10-15 13:13:17firstName: JasonlastName: Schaeferaddress1: 123 Sweet Laneaddress2: Apt #5city: Valentinestate: NEzip: 69201paymentMethod: Paypass

Page 30: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 31: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 32: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 33: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a relational database

ID Name Description Price imgPath1 Alaska Lauren loves this

photo even though she wasn't present

when the photo was taken. Her husband took this photo on a

guy's weekend in Alaska.

75 alaska.jpg

2 Las Vegas What happens in Vegas, stays in

Vegas... unless you take a picture.

90 vegas.jpg

ID FirstNam

e

LastName

Username

Email

1 Jason Schaefer

jason [email protected]

2 Joy Haywood

Joy [email protected]

User Print Datetime FirstName

LastName

Address1

Address2

City State

Zip PaymentMethod

1 1 2016-10-15 13:13:17

Jason Schaefer

123 Sweet Lane

Apt #5 Valentine

NE 69201

paypass

2 2 2016-04-22 14:48:30

Joy Haywood

423 Purple St

Honolulu

HI 96818

Credit card

Page 34: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Should you care about graph databases?

Page 35: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Graph databases are massively increasing in popularity

http://db-engines.com/en/ranking_categories

Page 36: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

PeoplePeople who viewed this item ultimately bought…

Blue pool float Red wagon

Page 37: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Places

Page 38: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Things

Page 39: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Fraud

$$$

Page 40: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Many more use cases• Modeling social networks

• Diagnosing psychosis with word analysis

• Analyzing the spread of epidemics

• Modeling a bio network

• Visualizing a social/economic/political network

Page 41: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Let’s try a graph database

Page 42: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Register at Bluemix: ibm.biz/devoxxfr• Deploy the app: ibm.biz/devoxxfr_deploy• Try the app

Page 43: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you create a schema diagram for a graph?

Page 44: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

1. Model the nouns and verbs

printuser buys

Page 45: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

2. Define vertices and edges

label: printlabel: user label: buys

Page 46: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

3. Define the multiplicitylabel: printlabel: user label: buys

MULTI

Page 47: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

4. Add propertieslabel: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Page 48: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

5. Create indexeslabel: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Vertex Indexes:userByName: [firstName, lastName], mixed, not unique, indexOnly (user)userByUsername: [username], composite, unique, indexOnly (user)printByName: [name], composite, unique, indexOnly (print)vByPrice: [price], mixed, not unique

Page 49: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you convert a schema diagram to code?

Page 50: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Convert the diagram to code!

• Show how the code is creating the schema in graph.py

Page 51: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Convert this diagram to code

label: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Vertex Indexes:userByName: [firstName, lastName], mixed, not unique, indexOnly (user)userByUsername: [username], composite, unique, indexOnly (user)printByName: [name], composite, unique, indexOnly (print)vByPrice: [price], mixed, not unique

Page 52: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Add a new print to the app’s home page

Page 53: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you create a node or edge in a graph database?

Page 54: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Create a node for a new print

label: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Page 55: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Write a Gremlin query that creates a new

user• Explore the code that creates a new user

Page 56: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you read a node or edge in a graph database?

Page 57: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Read a node for the Alaska print

label: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Page 58: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Write a Gremlin query that gets a user’s information• Explore the code that displays a user’s profile

Page 59: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you update a node or edge in a graph database?

Page 60: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Update a node for an existing print

label: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Page 61: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Write a Gremlin query that updates a user’s

information• Explore the code that updates a user’s profile

Page 62: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you delete a node or edge in a graph database?

Page 63: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Delete a node for a user

label: printtype: print

name: stringdescription:

stringprice: float

imgPath: string

label: usertype: userfirstName:

stringlastName:

stringusername:

stringemail: string

label: buysdatetime: stringaddress1: stringaddress2: string

city: stringstate: stringzip: integer

paymentMethod: stringMULTI

Page 64: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Write a Gremlin query that deletes all of the edges and

nodes• Explore the code that deletes all of the data from the

graph

Page 65: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you implement a feature that requires a new

graph query?

Page 66: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Implement a feature to display orders on a user’s profile

page - Observe the schema diagram - Write and test a query - Write the code that contains the query - Write the view code

Page 67: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you create a recommendation engine?

Page 68: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Generating recommendations

Page 69: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

TinkerPop recipe

Page 70: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 71: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 72: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 73: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 74: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

Page 75: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Write Gremlin queries to generate recommendations

Page 76: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Try this!• Write a query for generating recommendations on a product

page• Implement the feature to display recommendations on a

product page

Page 77: Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

That’s all for now…• To access the resources associated with this presentation…

- visit http://ibm.biz/devoxxfr_workbook - visit http://ibm.biz/devoxxfr_lab_slides

• To continue to learn more about Lauren, IBM Graph, and Bluemix, follow @Lauren_Schaefer @IBMGraph @IBMBluemix