Intro to Graph Databases

Post on 05-Apr-2017

64 views 4 download

Transcript of Intro to Graph Databases

#DevoxxFR #IntroToGraph @Lauren_Schaefer

#IntroToGrapha Devoxx France hands-on lab

@Lauren_Schaefer

1

#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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

What are graph databases?

#DevoxxFR #IntroToGraph @Lauren_Schaefer

What are graph databases?

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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you make a graph?

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you make a graph?

Node(noun

)

Node(noun

)

Node(noun

)

Node(noun

)

#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)

#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

#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)

#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)

#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)

#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)

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jasonlabel: userfirstName: JasonlastName: Schaeferusername: jasonemail: jason@example.com

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Lauren’s Lovely Landscapes as a graph

Alaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#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 jason@example.com

2 Joy Haywood

Joy joy@example.com

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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Should you care about graph databases?

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Graph databases are massively increasing in popularity

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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

PeoplePeople who viewed this item ultimately bought…

Blue pool float Red wagon

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Places

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Things

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Fraud

$$$

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Let’s try a graph database

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you create a schema diagram for a graph?

#DevoxxFR #IntroToGraph @Lauren_Schaefer

1. Model the nouns and verbs

printuser buys

#DevoxxFR #IntroToGraph @Lauren_Schaefer

2. Define vertices and edges

label: printlabel: user label: buys

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

MULTI

#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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you convert a schema diagram to code?

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Convert the diagram to code!

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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

user• Explore the code that creates a new user

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

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

#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

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you implement a feature that requires a new

graph query?

#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

#DevoxxFR #IntroToGraph @Lauren_Schaefer

How do you create a recommendation engine?

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Generating recommendations

#DevoxxFR #IntroToGraph @Lauren_Schaefer

TinkerPop recipe

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Recommendations for DaleAlaska

Antarctica

Las Vegas

Australia

Japan

Jason

Joy

Deanna

Dale

#DevoxxFR #IntroToGraph @Lauren_Schaefer

Live demo!• Write Gremlin queries to generate recommendations

#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

#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