TuGraph installation and simple use

TuGraph installation and simple use

TuGraph

TuGraph is a large-scale graph computing system independently developed by Ant Group, which provides a graph database engine and a graph analysis engine. Its main features are large data volume storage and calculation, high throughput rate, and flexible API, while supporting efficient online transaction processing (OLTP) and online analytical processing (OLAP). LightGraph, GeaGraph are former names of TuGraph.

1. Installation

1. Install docker

install docker

2. Pull the TuGraph image

https://hub.docker.com/search?q=tugraph
insert image description here

docker pull tugraph/tugraph-db-centos7

3. Start docker

docker run -d -p 7070:7070 -p 9090:9090 --name tugraph_demo tugraph/tugraph-db-centos7 lgraph_server
# 7070是默认的http端口,web访问使用。
# 9090是默认的rpc端口,rpc client访问使用。

4. Access the web terminal

Enter in the browser, the default username adminand password are 73@TuGraph

localhost:7070

insert image description here

After logging in to the console, you can click on the left 帮助to view related documents

insert image description here

2. Data type

TuGraph supports a variety of data types that can be used as attributes. The specific supported data types are as follows:

type of data minimum value maximum value describe
BOOL false true Boolean value
INT8 -128 127 8-bit integer
INT16 -32768 32767 16-bit integer
INT32 - 2^31 2^31 - 1 32-bit integer
INT64 - 2^63 2^63 - 1 64-bit integer
DATE 0000-00-00 9999-12-31 Date in "YYYY-MM-DD" format
DATETIME 0000-00-00 00:00:00 9999-12-31 23:59:59 Date and time in "YYYY-MM-DD hh:mm:ss" format
FLOAT 32-bit floating point number
DOUBLE 64-bit floating point number
STRING string of variable length
BLOB binary data

3. Graphic basis

TuGraph adopts a property graph model. The graph model is the modeling of things and their relationships, which is represented by

  • Vertices (vertices/nodes)
  • 边(edges/relationships)
  • property

​ Composition, let's take a look at the examples in the document yago:

insert image description here

yagoThe data contains 5 types of vertices and 8 types of edges, which jointly describe the basic relationship between movies and actors, as well as users' ratings for movies.

Label type illustrate
movie entity Indicates a specific movie, such as "Forrest Gump".
person entity Indicates a person, which in the case of a film may be an actor, director, or screenwriter.
genre entity Indicates the genre of the movie, such as drama, horror.
keyword entity Represents some keywords related to the movie, such as "save the world", "virtual reality", "subway".
user entity Indicates the user watching the movie.
produce relation Indicates the producer relationship of the video.
acted_in relation Indicates which movies the actor appeared in.
direct relation Indicates who the director of the video is.
write relation Indicates the writer relationship of the film.
has_genre relation Indicates the genre category of the video.
has_keyword relation Represents some keywords of the video, that is, tags of more detailed categories.
rate relation Represents the user's rating for a video.

4. Modeling

On the modeling page, the blue part on the left represents the model of the point, and the green part on the right represents the model of the edge. The first-level child node is the name of the model, the second-level child node is the attribute of the model, and the third-level child node is the information of this attribute: whether it can be empty, and its data type.
In the upper right corner, you can search for the name or attribute of the model, which will be highlighted in red on the page.
Create a vertex model, click Add, select the corresponding data type, whether it is empty or not by default, set the primary key, and it will be displayed on the page after the creation is successful.
To create an edge model, you don't need to set a primary key, and you don't need to add any attributes.

4.1 Create a new subgraph

insert image description here

4.2 Vertex labels

person

insert image description here

movie

insert image description here

4.3 Relationship labels

produce

insert image description here

5. Data import

On the import page, we can upload the csv file to import data online, click Select File to select the csv file from the computer, and then we need to perform the mapping data operation, first select the corresponding model (select movie), click Mapping Data, the above table It is the attribute of the data model, the data type, and whether it is optional.

TuGraph supports importing data from CSV files and JSON data sources.

movie.csv

LABEL=movie
id:INT32:ID,title:STRING,tagline:STRING,summary:STRING:OPTIONAL,poster_image:STRING:OPTIONAL,duration:INT32,rated:STRING:OPTIONAL
1,The Matrix,Welcome to the Real World.,Thomas A. Anderson is a man living two lives. By day he is an average computer programmer and by night a malevolent hacker known as Neo who finds himself targeted by the police when he is contacted by Morpheus a legendary computer hacker who reveals the shocking truth about our reality.,http://image.tmdb.org/t/p/w185/gynBNzwyaHKtXqlEKKLioNkjKgN.jpg,136,R
28,The Matrix Reloaded,Free your mind.,Six months after the events depicted in The Matrix Neo has proved to be a good omen for the free humans as more and more humans are being freed from the matrix and brought to Zion the one and only stronghold of the Resistance. Neo himself has discovered his superpowers including super speed ability to see the codes of the things inside the matrix and a certain degree of pre-cognition. But a nasty piece of news hits the human resistance: 250000 machine sentinels are digging to Zion and would reach them in 72 hours. As Zion prepares for the ultimate war Neo Morpheus and Trinity are advised by the Oracle to find the Keymaker who would help them reach the Source. Meanwhile Neo's recurrent dreams depicting Trinity's death have got him worried and as if it was not enough Agent Smith has somehow escaped deletion has become more powerful than before and has fixed Neo as his next target.,http://image.tmdb.org/t/p/w185/ezIurBz2fdUc68d98Fp9dRf5ihv.jpg,138,R
68,The Matrix Revolutions,Everything that has a beginning has an end.,The human city of Zion defends itself against the massive invasion of the machines as Neo fights to end the war at another front while also opposing the rogue Agent Smith.,http://image.tmdb.org/t/p/w185/sKogjhfs5q3azmpW7DFKKAeLEG8.jpg,129,R
82,Pulp Fiction,Just because you are a character doesn't mean you have character.,placeholder text,http://image.tmdb.org/t/p/w185/dM2w364MScsjFf8pfMbaWUcWrR.jpg,154,R
130,Cloud Atlas,Everything is Connected,placeholder text,http://image.tmdb.org/t/p/w185/k9gWDjfXM80iXQLuMvPlZgSFJgR.jpg,172,R
457,The Shawshank Redemption,Fear can hold you prisoner. Hope can set you free.,placeholder text,http://image.tmdb.org/t/p/w185/9O7gLzmreU0nGkIB6K3BsJbzvNv.jpg,142,R
471,The Godfather,An offer you can't refuse.,placeholder text,http://image.tmdb.org/t/p/w185/d4KNaTrltq6bpkFS01pYtyXa09m.jpg,175,R
496,The Godfather: Part II,I don't feel I have to wipe everybody out\ Tom. Just my enemies.,placeholder text,http://image.tmdb.org/t/p/w185/tHbMIIF51rguMNSastqoQwR0sBs.jpg,200,R
517,The Good\ the Bad and the Ugly,For three men the Civil War wasn't hell. It was practice.,placeholder text,http://image.tmdb.org/t/p/w185/8PD1dgf0kQHtRawoSxp1jFemI1q.jpg,161,R
532,The Dark Knight,Why So Serious?,placeholder text,http://image.tmdb.org/t/p/w185/1hRoyzDtpgMU7Dz4JF22RANzQO7.jpg,152,PG-13
564,The Dark Knight Rises,The Legend Ends,placeholder text,http://image.tmdb.org/t/p/w185/dEYnvnUfXrqvqeRSqvIEtmzhoA8.jpg,165,PG-13
598,Schindler's List,Whoever saves one life\ saves the world entire.,placeholder text,http://image.tmdb.org/t/p/w185/tvOvW7Qjj63zbQW5TZ8CjPThAUd.jpg,195,R
615,12 Angry Men,Life is in their hands. Death is on their minds.,placeholder text,http://image.tmdb.org/t/p/w185/wvlFtIwh0GIqHRAz9F5cCch2IJD.jpg,96,NR
641,The Lord of the Rings: The Return of the King,The eye of the enemy is moving.,Aragorn is revealed as the heir to the ancient kings as he Gandalf and the other members of the broken fellowship struggle to save Gondor from Sauron's forces. Meanwhile Frodo and Sam bring the ring closer to the heart of Mordor the dark lord's realm.,http://image.tmdb.org/t/p/w185/50LoR9gJhbWZ5PpoHgi8MNTYgzd.jpg,201,PG-13
681,Fight Club,How much can you know about yourself if you've never been in a fight?,placeholder text,http://image.tmdb.org/t/p/w185/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg,139,R
698,The Lord of the Rings: The Fellowship of the Ring,One ring to rule them all,Young hobbit Frodo Baggins after inheriting a mysterious ring from his uncle Bilbo must leave his home in order to keep it from falling into the hands of its evil creator. Along the way a fellowship is formed to protect the ringbearer and make sure that the ring arrives at its final destination: Mt. Doom the only place where it can be destroyed.,http://image.tmdb.org/t/p/w185/9HG6pINW1KoFTAKY3LdybkoOKAm.jpg,178,PG-13
710,Star Wars: Episode V - The Empire Strikes Back,The Adventure Continues...,placeholder text,http://image.tmdb.org/t/p/w185/6u1fYtxG5eqjhtCPDx04pJphQRW.jpg,124,PG
737,Inception,Your mind is the scene of the crime.,placeholder text,http://image.tmdb.org/t/p/w185/tAXARVreJnWfoANIHASmgYk4SB0.jpg,148,PG-13
750,One Flew Over the Cuckoo's Nest,If he's crazy\ what does that make you?,placeholder text,http://image.tmdb.org/t/p/w185/srr59GKJdDXPwnWlew9NoYfOvYV.jpg,133,R
770,Forrest Gump,The world will never be the same\ once you've seen it through the eyes of Forrest Gump.,A man with a low IQ has accomplished great things in his life and been present during significant historic events - in each case far exceeding what anyone imagined he could do. Yet despite all the things he has attained his one true love eludes him. 'Forrest Gump' is the story of a man who rose above his challenges and who proved that determination courage and love are more important than ability.,http://image.tmdb.org/t/p/w185/z4ROnCrL77ZMzT0MsNXY5j25wS2.jpg,142,PG-13
781,Goodfellas,Three Decades of Life in the Mafia.,placeholder text,http://image.tmdb.org/t/p/w185/pwpGfTImTGifEGgLb3s6LRPd4I6.jpg,146,R
797,The Lord of the Rings: The Two Towers,A New Power Is Rising.,Frodo and Sam are trekking to Mordor to destroy the One Ring of Power while Gimli Legolas and Aragorn search for the orc-captured Merry and Pippin. All along nefarious wizard Saruman awaits the Fellowship members at the Orthanc Tower in Isengard.,http://image.tmdb.org/t/p/w185/9mBjBuUmBBgnGjV1JZ2uCIYbaph.jpg,179,PG-13
805,Star Wars: Episode IV - A New Hope,A long time ago in a galaxy far\ far away...,Princess Leia is captured and held hostage by the evil Imperial forces in their effort to take over the galactic Empire. Venturesome Luke Skywalker and dashing captain Han Solo team together with the loveable robot duo R2-D2 and C-3PO to rescue the beautiful princess and restore peace and justice in the Empire.,http://image.tmdb.org/t/p/w185/tvSlBzAdRE29bZe5yYWrJ2ds137.jpg,121,PG
1096,3 Days to Kill,amazing killing time,placeholder text,http://image.tmdb.org/t/p/w185/53pam1GAosXwOjefCEVNjYTvy6j.jpg,113,PG-13
1097,300: Rise of an Empire,Amazing killing time,placeholder text,http://image.tmdb.org/t/p/w185/hrOMW22FV86te7uLyRnp3Z6Nv6e.jpg,0,R
1118,American Hustle,Everyone Hustles To Survive,placeholder text,http://image.tmdb.org/t/p/w185/mhB7C62lSMpGO2HYNaW6d7W3TVH.jpg,138,R
1241,Frozen,Amazing killing time,placeholder text,http://image.tmdb.org/t/p/w185/qb4ls8KzTJMFPEwNOfM7FotfC4Y.jpg,102,PG
1293,Gravity,Don't Let Go,placeholder text,http://image.tmdb.org/t/p/w185/2gPjLWIyrWlAn2DgKMOKTBnZYyO.jpg,91,PG-13
1356,The Monuments Men,It was the greatest art heist in history,placeholder text,http://image.tmdb.org/t/p/w185/lhCUqVEPA3MMxnV8QTeVBTmRrP4.jpg,118,PG-13
1366,The LEGO Movie,Assembling in 2014,placeholder text,http://image.tmdb.org/t/p/w185/lMHbadNmznKs5vgBAkHxKGHulOa.jpg,100,PG
1412,RoboCop,We've got the future under control.,placeholder text,http://image.tmdb.org/t/p/w185/hFQcC5eSSHqJfk70RdgrMMxGuYt.jpg,102,PG-13
1550,Her,Amazing killing time,placeholder text,http://image.tmdb.org/t/p/w185/8f8Hisd36IN8jN1OpKxsxRu8i57.jpg,126,R
1596,Dallas Buyers Club,Dare to Live,placeholder text,http://image.tmdb.org/t/p/w185/ecb5z7VGKjfSQLPMwt1FOiPE5V0.jpg,117,R
1693,12 Years a Slave,The extraordinary true story of Solomon Northup,placeholder text,http://image.tmdb.org/t/p/w185/kb3X943WMIJYVg4SOAyK0pmWL5D.jpg,134,R
1768,Elysium,Amazing killing time,placeholder text,http://image.tmdb.org/t/p/w185/tHkjoAxmhp3Eau1h0Ir7maKMwUz.jpg,109,R
1856,Epic,Discover a world beyond your imagination,placeholder text,http://image.tmdb.org/t/p/w185/fdXA81BOdZ3pEyBlY8UUvuJj136.jpg,102,PG
1935,The Croods,Meet the first modern family.,placeholder text,http://image.tmdb.org/t/p/w185/mo4WQxBxs6IU7fmkIOyMOLyG3qq.jpg,98,PG
2008,Mr. Peabody & Sherman,Amazing killing time,placeholder text,http://image.tmdb.org/t/p/w185/qnmYfaopf7xJwLgVjeTdgZ4nI0v.jpg,92,PG
2055,The Avengers,Some assembly required.,placeholder text,http://image.tmdb.org/t/p/w185/cezWGskPY5x7GaglTTRN4Fugfb8.jpg,143,PG-13
2232,Hansel & Gretel: Witch Hunters,Classic tale. New twist.,placeholder text,http://image.tmdb.org/t/p/w185/yDIVWFJqFLIeS8E1R6GG9uwPMS3.jpg,88,R
2398,The Lorax,Meet The Original Force Of Nature,placeholder text,http://image.tmdb.org/t/p/w185/6FuYtjegxaUv7n7C733hUEOQytR.jpg,86,PG
2422,Mulan,This time\ the princess saves the prince.,placeholder text,http://image.tmdb.org/t/p/w185/ztD0iLfeyESQZV95GwPeSE3Uyqn.jpg,88,G
2498,Beowulf,Evil breeds pain.,placeholder text,http://image.tmdb.org/t/p/w185/j90lLkvlcPNL6VVoLGQ0yzPUfJV.jpg,115,PG-13
2654,Solomon Kane,Fight evil... With evil.,placeholder text,http://image.tmdb.org/t/p/w185/n5SHBE4ulsDDVa9gYttUYwp1VZU.jpg,104,R
2767,The Usual Suspects,Five Criminals . One Line Up . No Coincidence.,placeholder text,http://image.tmdb.org/t/p/w185/jgJoRWltoS17nD5MAQ1yK2Ztefw.jpg,106,R
2885,Hugo,One of the most legendary directors of our time takes you on an extraordinary adventure.,placeholder text,http://image.tmdb.org/t/p/w185/fkeaJr29ypea1n24gzspsc7qK44.jpg,126,PG
3106,Fargo,A lot can happen in the middle of nowhere.,placeholder text,http://image.tmdb.org/t/p/w185/aZeX4XNSqa08TdMHRB1gDLO6GOi.jpg,98,R
3252,The Silence of the Lambs,To enter the mind of a killer she must challenge the mind of a madman.,placeholder text,http://image.tmdb.org/t/p/w185/qjAyTj2BSth1EQ89vNfo0JYVPFN.jpg,118,R
3390,The King's Speech,Find your voice.,placeholder text,http://image.tmdb.org/t/p/w185/v8M5Sytbut7vBXyZ1HDy8lUVVcB.jpg,118,R
3459,There Will Be Blood,There Will Be Greed. There Will Be Vengeance.,placeholder text,http://image.tmdb.org/t/p/w185/bFgqkuAFBHNuq0oTtewHwuLVWSO.jpg,158,R
3624,The Iron Lady,Never compromise,placeholder text,http://image.tmdb.org/t/p/w185/e67tal4u66eFz8yGnJGTO5FYRxY.jpg,105,PG-13
3680,The Untouchables,What are you prepared to do?,placeholder text,http://image.tmdb.org/t/p/w185/nv7FOs90Y3Cq5tBhEITEV8Zebyw.jpg,119,R
3753,Capote,In Cold Blood,placeholder text,http://image.tmdb.org/t/p/w185/bf3nxeLglBvXH5jAnMLmhc56muB.jpg,114,R
3915,Lost in Translation,Everyone wants to be found.,placeholder text,http://image.tmdb.org/t/p/w185/5T8VvuFTdaawKLJk34i69Utaw7o.jpg,102,R

movie.json

[3680,"The Untouchables","What are you prepared to do?","placeholder text","http://image.tmdb.org/t/p/w185/nv7FOs90Y3Cq5tBhEITEV8Zebyw.jpg",119,"R"]
[3753,"Capote","In Cold Blood","placeholder text","http://image.tmdb.org/t/p/w185/bf3nxeLglBvXH5jAnMLmhc56muB.jpg",114,"R"]
[3915,"Lost in Translation","Everyone wants to be found.","placeholder text","http://image.tmdb.org/t/p/w185/5T8VvuFTdaawKLJk34i69Utaw7o.jpg",102,"R"]

insert image description here

Next, we map the data of the person, select the model of the person, click the mapping data, and connect the line to match. If the connection is wrong, you can select this line, click to delete the line, reconnect, click import, and the import is successful.

person.csv (more data, put it in another article)

insert image description here

Next, we map the side data. For the side data, we need to select the starting point and the ending point. For the starting point, we choose the person’s id. After the selection, the table will be generated accordingly. For the ending point, choose the movie’s id. Then connect the data, click OK, click Import, and the data is imported successfully.

produce.csv

13,130
14,130
15,1
15,28
15,68
106,82
108,2498
109,82
145,130
490,3915
491,496
491,3915
549,737
549,532
549,564
607,598
609,598
616,615
664,641
664,698
664,797
665,641
665,698
665,797
665,3390
666,641
666,698
666,797
666,3390
683,1693
725,710
725,805
728,710
728,805
764,750
776,2498
791,2885
793,781
1119,1118
1253,1241
1295,1356
1297,1293
1298,1293
1365,1356
1371,2232
1557,1550
1705,1693
1767,1768
2021,2008
2068,2055
2074,2055
2075,2055
2243,2232
2244,2232
2779,2767
2895,2885
3113,3106
3392,3390
3464,3459
3689,3680

insert image description here

6. Data query

TuGraph uses the Cypher language for queries.

6.1 What is Cypher

Cypher is a declarative graph query language that expresses efficient query and update graph databases. Cypher is a relatively simple query syntax, which allows us to focus more on business domain issues.

Keywords in the Cypher language are not case-sensitive, but attribute values, labels, relation types, and variables are case-sensitive.

The concept of table does not exist in Neo4j, only labels (labels), nodes (Node), associations (Relation), paths (path), nodes stored in labels, nodes and relationships can be simply understood as points, edges and paths in the graph It is represented by nodes and relationships, such as: (a)-[r]->(b), which means a path from node a to node b via relationship r.

In data query, parentheses () are generally used for nodes, and brackets [] are used for associations.

6.2 Nodes and Relationships

Cypher uses a pair of parentheses () to represent a node, such as (n:role) represents a role node, n is a variable name , which is used to access this node when the command is executed, and cannot be used after the command is executed. At the same time, a single () indicates an anonymous node , and when matching, it means matching all nodes.

in relationship

  • – Indicates an undirected relationship
  • –> Indicates a directional relationship
  • -[r]-> assigns a variable name to the relationship to facilitate the operation of the relationship
  • -[r:Spouse]-> The matching relationship is the type of spouse

6.3 Matching Statements

In Neo4j , the MATCH command is used to obtain node and relationship information from the database, similar to SELECT in SQL . RETURN returns data after the MATCH search is completed, so MATCH must be used with RETURN at the same time.

6.2.1 Matching Nodes Based on Labels

// 匹配所有 xx节点
match (n:xx) return n

Example: match allperson

match (n:person) return n

insert image description here

6.2.2 Matching Nodes Based on Labels and Attributes

// 匹配 property 为 yy 的 xx 节点
match (n:xx{property:'yy'}) return n

For example: nodes that match nameasMartin Scorseseperson

match (n:person{name:'Martin Scorsese'}) return n

insert image description here

6.2.3 Matching any relationship

// 匹配出有任意关系的两个节点
match p = (n)-[r]->(m) return p

insert image description here

6.2.3 Optional matching

optional matchSimilar match, the difference is that optional matchwhen the content is not matched, it returns nullto facilitate the query to continue, and matchdirectly returns the query without results

Example: find The Lord of the Rings: The Return of the Kingall relations for movies

optional match p = (n)-[r]->(m:movie{title:'The Lord of the Rings: The Return of the King'}) return p

insert image description here

For example: find userall nodes

optional match (n: user) return n

insert image description here

6.2.4 Filter matching

Just SQLlike in , clausesNeo4j are provided in to filter the query results ofWHEREMATCH

Example: Query Perter Jacksonwhich movies have been produced

MATCH (n:person)-[r:produce]->(m:movie) where n.name='Peter Jackson' return m.title

insert image description here

At the same time, functions , string matching , logical matching and regular expression matching WHEREcan be combined for more refined matchingexists()starts with,ends with,containsin,not,and,or

Example: match all durationnodes that contain attributes

MATCH (n) where exists (n.duration) return n

insert image description here

Example: matches all works in the Lord of the Rings series

MATCH (n:movie) where n.title starts with "The Lord of the Rings" return n.title

insert image description here

Beside this there is

// 匹配所有 与 person1 和 person2 有关的节点,同时过滤掉 person3 和 person4
match p=(n)--(m) where  n.name in ['person1','person2'] and not m.name in ['person3','person4'] return p
// 匹配所有 name 包含 x 的节点
// 这个正则表达式等价为 match(n) where n.name contains 'x' return n
match (n) where n.name =~ '.+?x.+' return n

7. Data Creation/Update

7.1 Creating Nodes

CREATEThe syntax for creating a single node is as follows:

CREATE (
    <node-name>:<lable-name>
    {
        <Property1-name>:<Property1-value>
        ......
        <Propertyn-name>:<Propertyn-value>:
    }
)

For example: create a node James Wanofperson

create (n:person{name:'James Wan',id:10000,born:1977})

Match this created node

MATCH (n:person{name:'James Wan'}) return n

insert image description here

Then create Fast & Furious 7a movienode

create (n:movie{title:'Fast & Furious 7',id:10001})

7.2 Creating Relationships

For example: creating a relationship that James WanmakesFast & Furious 7

MATCH (n:person),(m:movie) WHERE n.name = 'James Wan' AND m.title = 'Fast & Furious 7' CREATE (n) -[r:produce]-> (m) RETURN r

insert image description here

7.3 Update data

Example: James Wanadd a photo to

MATCH (n:person{name:'James Wan'}) set n.poster_image = 'this is an image' RETURN n

insert image description here

8. Delete

There are two delete methods in Neo4j, DELETEand REMOVE. DELETEUsed to delete nodes and relationships, REMOVEused to delete labels and attributes of nodes and relationships. Both need to cooperate MATCH, first match the content, and then perform the operation

8.1 Delete property

For example: James Wandelete the photo

MATCH (n:person{name:'James Wan'}) REMOVE n.poster_image RETURN n

insert image description here

8.2 Delete nodes and edges

To delete a node, you need to delete all edges related to the node, which is consistent with graph theory - there is no edge without nodes. Therefore, to delete the node of Jinlun Fawang, you need to find the node and its relationship first, and then delete it

For example: to delete James Wan, you need to delete the node and all the edges related to it at the same time

MATCH (n:person{name:'James Wan'}) -[r]- () DELETE n,r

insert image description here

8.3 Clear the database

To clear the database means to clear all nodes and edges . There are two types of nodes : nodes connected by edges and isolated nodes . Therefore, it is necessary to match these two conditions at the same time and then delete them.

MATCH (n) -[r]- () DELETE n,r

I won't show it here...

Guess you like

Origin blog.csdn.net/cacique111/article/details/126842771