Neo4j Graph Database & Cypher

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm gonna show you how to install and set up a neo4j graph database in Windows now I'm gonna talk a little bit about what graph databases are but this isn't by any means a front to that course on graph databases I do have a full neo4j course available at edge' wanax comm that goes into graph database structure in detail so I'll put that link in the description now before we go ahead and install they just want to take a look at a simple graphic that I created that kind of shows you how a graph database works now graph databases are much different than your traditional relational database like MySQL or Postgres instead of using tables with rows and columns we use a graph with nodes and relationships alright now both of these types of databases have their place a relational database is great for tabular data that's not really closely related all right if you have any experience with relational databases then you know if you have a lot of nested relationships it can get really complicated with join tables and join queries and you need all kinds of primary and foreign keys and it just can be a real mess and even worse than that it can be really taxing on the system so graph databases were built to kind of fix that problem and work with data that's much more closely related and more dynamic alright so if we take a look at this image here and you see these three orange dots these these represent what's called a node and a node represents some kind of data entity all right now nodes can have labels in this case we have a user label for these nodes and each one represents a single user all right now nodes can also have what's called properties which are basically just attributes in this case each one has a name and an age now if we look in between the nodes we have these lines which represent the relationship between each node alright so for instance John and Alice have a friend's relationship now what's nice about this with relationships they can also have properties just like nodes which is something that's that's much different than what you would see in MySQL all right so this friends relationship has a property of sense which is the year that these two became friends all right you can see that John and Steve also have a friends relationship and then Steve and Alice have a married relationship which also has a sense property all right so this is essentially how graph databases work obviously the more nodes and the more data relationships you have the more complicated it gets all right but when you have thousands of nodes and you look at a graph database it's a lot more telling than looking at thousands of users going to call them and a table of columns you can actually start to see patterns and it's just it's really helpful because it's a little more it's a little closer to what we actually think when we think of data we don't think in columns and rows unless you're a robot all right so I just wanted to kind of get that out of the way just in case you do we have no clue what the graph database is all right so we're gonna go ahead and go to neo4j comm and just click on the download button up here and the right all right now there's a couple different versions there is the enterprise version which has better scaling and has additional features we're not going to get into that that's more for company and you know that plans on having a lot of data we're going to go ahead and just get the Community Edition okay so we're gonna click that button and that should start to download its ok so that file is 96.1 megabytes which isn't that bad alright so once that's done we can go ahead and open it all right so you should get a box like this it's gonna ask you where you want to install it I'm just gonna keep it in Program Files that's fine click Next accept the agreement next all right so that's it now we can choose to run it I'll click finish all right so when you first try and run u4j you're gonna get this it says choose a graph database directory and then stock the server so the default is going to be in your Documents folder all right you can choose a different location if you want but I'm just going to keep that and just click start all right so now when it turns green it's telling us to browse to local host port 74 74 so I'm going to click that and you should see a screen like this it's going to ask you for a login and by default the username and password of both going to be neo4j ok we'll click connect and then it'll ask you to set your password or change your password and now we're connected all right so you should see an interface like this now each of these boxes this one here and this one these are called frames and this is basically like a welcome frame ok so it gives you a couple of different options to to kind of go through little tutorials for instance this learn about neo4j if I click start learning it's going to tell us about nodes and relationships and properties things that we've actually just went over alright if I click the arrow you can see it shows us a node with some fields or some properties and kind of just goes through what we've already talked about all right now you'll notice up in the corner here we have this colon play concepts this one has play start these are commands that we can actually run up here in the editor alright so if we were to get rid of this close ease out and : play start you'll see that it'll open that up so you can actually run different commands in here all right now if you want help with the commands you can actually type in : help commands and it'll tell you all the different help commands here so for instance the server connection we could say help server and that would open that up we could look at the status by saying server status all right so show us what we're connected as and so on all right so you can look at a list of the commands in the documentation so you can do different things but we're not gonna really do any more with that all right so let's take a look over here on the side and if I click this icon you see that it opens this the sidebar with some database information so here is wheel where you can see all the labels in your database all of the relationships and all the property keys which obviously we don't have any yet and then you have some database information down here like the version the name the size alright and then this next icon is favorites you can actually save certain queries in your favorites you see we have basic queries here if I was just to click on one of these it'll just pop up up here now I'm gonna get rid of that because I want to type our own query but just in just so you can know you can save favorites and store them over here this tab here is just documentation so if you want to learn a bit more after this video you can check that out down here we have an option for neo4j browser sync this is a service that allows you to basically push your local database to a cloud version of it you have to sign up and sign in to be able to do that alright then we have some browser settings user interface we can change the theme the color of it the initial command which by default is just play start but you can set it up to for instance grab all of your nodes and display them whatever you'd like it to be when you first enter the interface and then down here we just have some connection information things like that and then this last tab is just about it's about the system the version license things like that all right so what I want to do now is just start to create a couple nodes all right so let's say create and remember a node is represent represented with parentheses and we can give it a identifier so we can use n that's what I usually use if we're working with relationships I'll use ABC but if we're working with a single node I tend to just use n but you can use whatever you'd like all right so that's our identifier now we also want to give this node a label I'm gonna give it a label of person all right now if we want to add properties we want to just open up some curly braces and the syntax is pretty much identical to Jason syntax we want let's say name and we're gonna set that to start it - Brad okay so that's how we can add properties if we want we could just put a comma and add more but I'm just gonna do a name for now alright so that will create a node a person node with the name of Brad now you don't have to return anything on a create but you can so let's just say return n alright so we're gonna go ahead and execute that with this play button up here and there we go we have our first node okay if I hover over it and you look down in this area you'll see that it has an ID of zero okay the ID is auto-generated and it also has a name of Brad which is a property that we gave to it all right now every time you run a query it's going to show your result in a new frame all right which you can pin to the top if you want you can also just get rid of them with the X all right let's create a couple more person nodes so if we click the up arrow you can see that it gives us the last query I'm just gonna go ahead and just so these will say Melanie and you can see every return that's gonna open up a new frame now you don't see Brad in here because it's only returning what we set up here which is just this one node I'll show you how to take how to look at all of our nodes in a second okay so say Mike Jill and say Jack okay so if we want to look at all of our nodes we can say match and and then we'll just return and all right so essentially these statements are made up of a match and then a return all right so there we go there's all the nodes in our database now we can also create nodes with other labels and before I do that let's click here and you can see that we have a list of our labels and we have a person label so let's create a different kind of node so we'll create say and and let's give this one a label of movie okay and then we'll have a property called title and let's say Caddyshack I don't know why that just popped in my head but it did alright and then we'll just return and now notice that this node is a different color neo4j browser will automatically set different colors to different labels all right so let's add another movie let's say Scarface okay let's do gremlins alright don't know why I'm thinking of all 80s movies but hey alright so now what I want to do is look at the whole thing together so we're gonna say match n or turn n and that gives us all the nodes if we wanted to get let's say just the movie nodes we could say match n movie and that gives us just the movies alright so that's pretty much all there is to creating node now let's say we want to add a property to a node alright so we'll say Jack we won't let's say we want to add an age all right so what we can do is first match I want to match I'll say N and this is a person node that we want to match and then we'll say we want the one with name two that equals Jack and then we can simply go to the end here and say set n dot age and we'll set that to 34 okay so that just says that one property now if we go and a shortcut if we want to see the whole graph is to click this this asterisk over here alright and then if I hover over Jack and you look down in this area you'll see that he now has an age of 34 alright so that's how we can add properties and that's also how we can update properties let's say we want to set it to 35 we can go ahead and run that same query and now if we look now he's 35 all right same thing if we want to remove okay so let's say so match in person with the name of Jack and then we'll say remove n dot age okay so now if we go ahead and look now his age is gone all right so it's that simple to add and remove properties all right so now I want to get into relationships all right so let's say we want to add a relationship Jack and Jill so what we're gonna do is we're gonna say match and we're gonna use a as an identifier but this could just as well be anything and we want to say it's a person and the name Jack okay so now we're gonna go here and put a comma open up another set of parentheses and this is gonna be the one that we want to add as a relationship so we'll use B as an identifier still a person and we want the person with the name of Jill and then what we're gonna do is say merge and we're going to take the a node and we want to set a relationship now we could go like this that represents a relationship a to B but we want to name this relationship so we're gonna go right in the middle here and open up some brackets and we're gonna say R as an identifier which again this could be anything and let's add married as a relationship all right so let's go ahead and run that and then we'll go ahead and look at the graph and now you can see that Jack and Jill have a married relationship all right and you can also see that you can move these around any way you'd like so let's go ahead and add another relationship I'm just gonna click up twice and let's change the name here actually we'll leave Jack and let's make let's give them a friends relationship with Brad so over here we're going to change married to friends now you can also add properties into a relationship just like you can with nodes let's put a space right here open up some curly braces and it'll say since I'll say since 1998 okay we'll take a look at the graph and now you can see that Jack is friends with Brad and if I hover over friends you'll see we have that it's property of 1998 all right now we can also add and add relationships between nodes that don't have the same label so for instance maybe Jill maybe her favorite movie is Caddyshack so what we can do is let's change person to Jill and then the relationship is going to be to a movie and the name or not the name the title is Caddyshack and then we'll change the relationship to favorite and we don't need a let's not do a property here so we'll just get rid of that all right so we're matching the person Jill with the movie Caddyshack and the relationship is gonna be favorite so let's go ahead and run that okay take a look at the graph and now you can see that jill has has a favorite movie of Caddyshack all right so you can see how easy this is it's it's really not difficult at all once you learn the patterns with Seifer and how to add relationships it's really pretty easy and it's it's pretty cool I think this is a lot more interesting than looking at a bunch of rows of data now what I'm going to do is I'm going to pause the video and I'm just going to add a bunch of more relationships here and then we can start to look at some other queries fetching certain data all right so I will be right back all right so I just went ahead and added a couple more relationships now I want to get into searching or a query ongdalsem Jill okay we want the person note of Jill so all we have to do is say match and and we think we can specify person but we don't have to and then we'll say we're okay just like SQL we have where ozs where n dot name is equal to Jill all right and then we just want to return and and there we go gives us Jill now if we want to just return a property we could do that as well by just tacking on to the end dot name all right and now you can see that when we ask for fields it gives us this row layout all right now we can also use operators like + + or so let's say that we want to search nodes where n dot name equals Jill or n dot name equals Mike okay so now you can see that it returns us a row with the name Mike and Jill all right and of course if I get rid of the dot name it'll give us the whole node in graph format now we can also get specific knowledge without using where all right so if we were to say match and person and then we can just put the property in here like named Brad and then just return and ok so you can see that that gives us a single node as well without having to use where we just plug the properties in all right so what if we want to search data based on a relationship let's get all the friends of Jack so we'll say match and we'll say hey this is going to be a person with the name of Jack sorry about that and then we want the relationship so I'm going to put brackets here and then : friends another - and then we'll use a greater than and then we need the node that we want it to relate to as friends which will just be the person all right and then we'll say return return a.b okay so what this gives us is it gives us jack and anybody that he's friends with okay so he's friends with Mike Brad and Melanie and it also shows if if any of his results have a relationship with each other it'll show that as well all right now if we don't want to return Jack we can just get rid of the a and just return b okay and that gives us as friends okay at the same time we can also just return a which would be just Jack alright now I want to try this the other way around and let's take the name property out of a and put it into B okay and then we'll just return a.b all right now when I do that it gives us no Rose the reason for that is because we have this this little this greater than here which is basically pointing a certain direction so people that are friends with Jack not that Jack is friends with because if we look down here you'll see that the relationships they do have a direction okay we have the arrow pointing down here all right and if we look at this jack is friends with other people but people are not friends with Jack now when we write our queries it has to be in this direction but we don't have to stick with that direction when we actually do a search or a query so what we can do up here is we can get rid of this as greater than okay so just leave it like that so if we go ahead and run that that's going to give us everything in every direction all right and at the same time we could put a less than right here to go that way okay and that gives us obviously that gives us anybody that Jack is friends with all right so just to let you know that you can do different directions when you're querying the data or the relationship all right so we're kind of running out of time here the last thing I want to look at is deleting notes now it's important to know that just take a look at the whole graph when you delete a node you can't delete a node unless you delete all its relationships if it has relationships you can't delete it all right so let's give it a shot and just see what happens so let's say match a person and then we're going to let's see put our property in here so person whoops person name and let's do Jill and then over here on this side we're going to say delete a all right so we get this error and it says cannot delete node because it still has relationships all right so we have to delete the relationship so if we look at Jill she has a favorite movie and she also has married she's married to Jack so we have to delete those relationships so let's do match by the way this doesn't these commands don't have to be case sensitive I just like to use upper case all right so let's see a person named Jill all right now we have to delete the relationships so let's do go like that and I'm just gonna use an identifier in here because I want to delete all relationships and let's point to B which can be I'm not going to give it a label because I want to delete any relationships between movies or other people all right and then we simply can say delete a B and R ok so that didn't work let's see what am I missing here maybe we have to just delete our first just the relationships okay there we go delete in relationships now if we look at the graph ok now Jill is by herself she has no relationship so now we should be able to delete the node there we go all right so now if we look now there's no Jill now if we want to delete everything there's a command we can do for that so let's say match and and then we need to say optional match and and we want any relationship it has so put our to any node and then delete and in our okay if we look at our graph now we have no rows all right if I reload and look over here the labels are gone the relationships are gone but it holds the keys for some reason those don't get deleted now let's say you want every you want a brand new database what you could do is you want to go to your database file which is in by default documents neo4j and then default graph DB alright now what we can do is just delete actually we have to stop the server first okay and then we can just delete everything in here okay and then we can go back in start it go back to the browser and it's gonna be just like it was the first time you came and you have to have to log in with neo4j and then it'll ask you for a password to change it to alright and now if we look over here even the keys are gone and we're good as new now there are ways you can have multiple instances of neo4j running on the same system so you can have multiple databases but I'm not real I'm not going to get into that I just kind of wanted to scratch the surface and introduce you to graph databases if you didn't know what they were or if you just heard about them and he he wanted to get into it a little more all right now if you're interested and you do like this stuff I would suggest going to edge wanax comm and getting the neo4j course that I did I believe it's six chapters it has a project where we'll actually work with nodejs and create a node application that uses neo4j and it's it's it's pretty neat so hopefully guys liked this video if you did please give it a like leave a comment whatever you can do was great and thanks for watching
Info
Channel: Traversy Media
Views: 58,141
Rating: 4.9612904 out of 5
Keywords: neo4j, graph database, cypher, cypher query language, nosql, neo4j database
Id: 1kyPUqU-MkE
Channel Id: undefined
Length: 29min 3sec (1743 seconds)
Published: Wed Jul 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.