Gremlin installation and usage detailed steps

Gremlin is a graph database query tool. Note that it is just a tool similar to dbeaver, navicat, and sqlyog. It is a tool specifically designed to analyze graph databases.

download

Download addressApache Download Mirrors

To save trouble, you can directly

wget  https://www.apache.org/dyn/closer.lua/tinkerpop/3.5.1/apache-tinkerpop-gremlin-console-3.5.1-bin.zip

unzip

unzip apache-tinkerpop-gremlin-console-3.5.1-bin.zip 

Edit conf file

There is remote-secure.yaml here itself and can be edited directly, but it is not recommended. , one of this yaml corresponds to a database connection. We directly create a new one and copy the following content.

vim  remote-secure-test.yaml 

# hosts The intranet address vip of the KonisGraph instance of the graph database, such as 10.xx.xx.107
hosts: [10.xx.xx.107]
# port Gremlin port of the KonisGraph instance of the graph database, such as 8186
port: 8186
# username/password The account and password of the KonisGraph instance of the graph database, such as account: steven, password: test-pwd-123
username: steven
password: test-pwd-123
connectionPool: {   enableSsl: false,   sslEnabledProtocols: [TLSv1.2] } # serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1, config: { serializeResultToString: true }} serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0, config: { serializeResultToString: true, useMapperFromGraph: graph }}



Note that enableSsl: false, which one I copied before, is true and keeps reporting errors.

The serializer has also changed.

start up

bash ${gremlin_home}/bin/gremlin.sh

Console input

 :remote connect tinkerpop.server conf/remote-secure-test.yaml

At this time, the startup is successful.

You can also use this package for window. The only difference is that gremlin.bat is started.

 Because there are not many articles about gremlin, many big guys ignore the novices. Let me explain what you need to pay attention to.

For example, mysql, I just want to simply experience some basic experiences such as select * where group limit. Do I need to download a mysql library, and then download a navicat or dbeaver.

Gremlin has a built-in library (this is what I understand, and it can be easily operated).

Connect locally

Using gremlin embedded data

graph = TinkerFactory.createModern()

g = traversal().withEmbedded(graph)

 At this point you can simply experience gremlin's syntax.

gremlin> g.V().elementMap()

gremlin> g.E().elementMap()

Connect remotely

If you follow the steps online to create a gallery. After you have configured it according to my previous configuration, you can directly

:remote connect tinkerpop.server conf/remote-secure-test.yaml

:remote console

Note that connecting to a remote location will automatically initialize s schema and g:graph. If you use local schema, you must initialize the graph yourself.

At this point, the simple connection is OK.

Study address

Getting Started

Summary of article links for in-depth study of the graph database language Gremlin_Jermy Li's Blog-CSDN Blog

The difference between graph database and our relational database,

Graphs only have the concept of a library and not a table. All data is together, such as the student table, teacher table, and school table. There is no concept of a table, only points and edges. A relationship graph composed of vertices and edges.

The main purpose of the graph database is to discover the relationship between points. For example, this database stores the information of 1.3 billion people, which is 1.3 billion points. The edge is who I know. Now there is a need. What kind of relationship can I know Jay Chou? ?

My high school classmate’s friend’s colleague’s cousin’s daughter-in-law’s sister’s best friend’s boyfriend’s best friend is the neighbor of Jay Chou’s assistant

If this relationship requires you to use mysql to check the same data and the same relationship, it is really difficult to check.

But graph data only requires one line of code (my ability is limited and I don’t know how to optimize it)

g.V().hasLabel(person).properties('name','cc').repeat(outE().otherV()).until(has('name','JAY')).path()

easy to understand. That is, the person who found out cc, based on the person's point, radiates the edges outward until there is a point =jay

Okay, let’s start with basic learning. Remember the picture below, all operations are based on this picture as a tutorial 

Getting Started

gremlin> g.V().elementMap()
==>[id:1,label:person,name:marko,age:29]
==>[id:2,label:person,name:vadas,age:27]
==>[id:3,label:software,name:lop,lang:java]
==>[id:4,label:person,name:josh,age:32]
==>[id:5,label:software,name:ripple,lang:java]
==>[id:6,label:person,name:peter,age:35]
gremlin> g.E().elementMap()
==>[id:7,label:knows,IN:[id:2,label:person],OUT:[id:1,label:person],weight:0.5]
==>[id:8,label:knows,IN:[id:4,label:person],OUT:[id:1,label:person],weight:1.0]
==>[id:9,label:created,IN:[id:3,label:software],OUT:[id:1,label:person],weight:0.4]
==>[id:10,label:created,IN:[id:5,label:software],OUT:[id:4,label:person],weight:1.0]
==>[id:11,label:created,IN:[id:3,label:software],OUT:[id:4,label:person],weight:0.4]
==>[id:12,label:created,IN:[id:3,label:software],OUT:[id:6,label:person],weight:0.2]
gremlin> 

1. Basic operations of graphs

V()E()id()label()properties()valueMap()values(),elementMap()

V(): Query vertices, generally used as the first step in graph query, and there are many types of statements that can be continued later.

E(): Query edges, generally used as the first step in graph query, and there are many types of statements that can be continued later.

id(): Get the ids of vertices and edges.

label(): Get the labels of vertices and edges

properties(): Get the attributes of vertices and edges. In addition  properties(), it can also  be used with and to obtain the name or value of the attribute key().value()

valueMap(): Get the attributes of vertices and edges.  The difference is that the structures they return are different . valueMap()The  latter flattens all attributes into a large list, and one element represents an attribute. The former maintains the attributes of a vertex or an edge. As a group, each group consists of key-value pairs of several attributes.properties()

values(): Get the attribute values ​​of vertices and edges.

elementMap:获取了标签和id 和valueMap properties都不一样

Test 1

gremlin> g.V()  (1) 查所有的点
==>v[1]
==>v[2]
==>v[3]
==>v[4]
==>v[5]
==>v[6]
gremlin> g.V(1)  (2) 查id=1的点
==>v[1]
gremlin> g.V(1).values('name')  (3) 查id=1的点的名字
==>marko
gremlin> g.V(1).outE('knows')  (4) 查id=1的点的know边(不查create边)
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.V(1).outE('knows').inV().values('name')  (5)查id=1的konw的箭头指向点的name
==>vadas
==>josh
gremlin> g.V(1).out('knows').values('name')  (6) //查id=1的点的know边的点的name
==>vadas
==>josh
gremlin> g.V(1).out('knows').has('age', gt(30)).values('name')  (7)
==>josh

Test 2: Add point edges

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0] //Note that this is a new graph
gremlin> g = traversal().withEmbedded(graph)
==>graphtraversalsource[tinkergraph[vertices: 0 edges:0], standard]
gremlin> v1 = g.addV("person").property(id, 1).property("name", "marko").property("age", 29).next( )
==>v[1]
gremlin> v2 = g.addV("software").property(id, 3).property("name", "lop").property("lang", "java"). next()
==>v[3]
gremlin> g.addE("created").from(v1).to(v2).property(id, 9).property("weight", 0.4) ==>
e [9][1-created->3]
//Added two points (1 person and one soft) and one edge (created) 

2. The concept of edge traversal

Here is a tip on how to remember,

If the current object is a point, then the following method out in without V and E is to check points, and the one with E is to check edges. Points can check points and edges.

If the current object is an edge, then the following method must have V, and the edge can only check points.

How to look at out and in a->b a is out and b is in. Look at the direction of the arrow, whichever side is inV

1. Steps based on the vertex (such as vertex "4" in the above figure): 

out(label): access the OUT direction adjacent points of the vertex according to the specified EdgeLabel (it can be zero EdgeLabel, representing all types of edges; it can also be one or more EdgeLabel, representing the edges of any given EdgeLabel, the same below) in
( label): access the vertex's IN-direction adjacent edge according
to the specified EdgeLabel both(label): access the vertex's bidirectional adjacent point
outE(label) based on the specified EdgeLabel: access the vertex's OUT-direction adjacent edge
inE( label): access the IN-direction adjacent edges of the vertex according to the specified EdgeLabel
bothE(label): access the bidirectional adjacent edges of the vertex according to the specified EdgeLabel

Here are some small demos

gremlin> gV(4).out()
==>v[5]
==>v[3] -- Take 4 as the vertex and look at the outside arrows pointing to 3 and 5 4create3 and 5 

gremlin> gV(4).in()
==>v[1] --With 4 as the vertex, the starting point of the arrow pointing to 4 is 1 1knows4

2. Edge-based Steps (such as the edge “knows” in the figure above):

outV(): accesses the outgoing vertex of the edge (note: this is based on the edge, and the above Steps are all based on the vertex). The outgoing vertex refers to the starting vertex of the edge.
inV(): accesses the incoming vertex of the edge. The incoming vertex is Refers to the target vertex of the edge , that is, the vertex pointed by the arrow
bothV(): accesses the bidirectional vertex of the edge
otherV(): accesses the partner vertex of the edge, that is, the vertex at the other end relative to the base vertex

3.demo1

gV(4).out().in() This is commonly used to check the vertices that are related to 1, for example, they are partners and like the same song as 1

gremlin> g.V(4).outE().inV().inE().outV().simplePath().path()
==>[v[4],e[11][4-created->3],v[3],e[9][1-created->3],v[1]]
==>[v[4],e[11][4-created->3],v[3],e[12][6-created->3],v[6]]

4 creates 3, and 1 and 6 also create 3, so 16 and 4 are cooperative relationships. 

In fact, it is the following. I also found 1 and 6. As for the additional 4, I will talk about it later.

gremlin> g.V(4).out().in()
==>v[4]
==>v[1]
==>v[4]
==>v[6]

3. has filter learning

hasLabel(labels…​): If the object’s label matches any one in the labels list, it can pass
hasId(ids…​): If the object’s id matches any one of the ids list, it can pass
has(key, value): Contains attributes." The object with the attribute "key=value" passes and acts on the vertex or edge
has(label, key, value): The object containing the attribute "key=value" and the label value matches passes and acts on the vertex or edge has
(key, predicate): contains The object whose key is key and the corresponding value satisfies the predicate passes. It acts on the vertex or edge
hasKey(keys…​): The attribute key of the object must contain all members of the keys list to pass. It acts on the vertex attribute
hasValue(values…​): object Only when the attribute value contains all the members of the values ​​list can it pass. It acts on the vertex attribute
has(key): the object containing the attribute whose key is key passes. It acts on the vertex or edge
hasNot(key): the object which does not contain the attribute whose key is key. By, acting on vertices or edges

g.V().has('person','name',within('vadas','marko')).values('age').mean()

Find the age of the point whose label is person and whose name is (vadas or marko). Calculate the average value and initialize it.

Loop operation

Guess you like

Origin blog.csdn.net/cclovezbf/article/details/132337641