Gremlin 基础知识
数据创建
1. 创建属性类型(PropertyKey)
graph.schema().propertyKey("name").asText().create()
graph.schema().propertyKey("age").asInt().create()
graph.schema().propertyKey("addr").asText().create()
graph.schema().propertyKey("lang").asText().create()
graph.schema().propertyKey("tag").asText().create()
graph.schema().propertyKey("weight").asFloat().create()
2. 创建顶点类型(VertexLabel)
graph.schema().vertexLabel("person")
.properties("name", "age", "addr", "weight")
.useCustomizeStringId()
.create()
graph.schema().vertexLabel("software")
.properties("name", "lang", "tag", "weight")
.primaryKeys("name")
.create()
graph.schema().vertexLabel("language")
.properties("name", "lang", "weight")
.primaryKeys("name")
.create()
3. 创建边类型(EdgeLabel)
graph.schema().edgeLabel("knows")
.sourceLabel("person").targetLabel("person")
.properties("weight")
.create()
graph.schema().edgeLabel("created")
.sourceLabel("person").targetLabel("software")
.properties("weight")
.create()
graph.schema().edgeLabel("contains")
.sourceLabel("software").targetLabel("software")
.properties("weight")
.create()
graph.schema().edgeLabel("define")
.sourceLabel("software").targetLabel("language")
.properties("weight")
.create()
graph.schema().edgeLabel("implements")
.sourceLabel("software").targetLabel("software")
.properties("weight")
.create()
graph.schema().edgeLabel("supports")
.sourceLabel("software").targetLabel("language")
.properties("weight")
.create()
### 4. 创建index类型(IndexLabel)
graph.schema().indexLabel("personByAge").onV("person").by("age").range().ifNotExist().create()
graph.schema().indexLabel("personByAge").onV("person").by("age").range().ifNotExist().create()
5. 添加顶点与边
okram = graph.addVertex(T.label, "person", T.id, "okram",
"name", "Marko A. Rodriguez", "age", 29,
"addr", "Santa Fe, New Mexico", "weight", 1)
spmallette = graph.addVertex(T.label, "person", T.id, "spmallette",
"name", "Stephen Mallette", "age", 0,
"addr", "", "weight", 1)
tinkerpop = graph.addVertex(T.label, "software", "name", "TinkerPop",
"lang", "java", "tag", "Graph computing framework",
"weight", 1)
tinkergraph = graph.addVertex(T.label, "software", "name", "TinkerGraph",
"lang", "java", "tag", "In-memory property graph",
"weight", 1)
gremlin = graph.addVertex(T.label, "language", "name", "Gremlin",
"lang", "groovy/python/javascript", "weight", 1)
okram.addEdge("created", tinkerpop, "weight", 1)
spmallette.addEdge("created", tinkerpop, "weight", 1)
okram.addEdge("knows", spmallette, "weight", 1)
tinkerpop.addEdge("define", gremlin, "weight", 1)
tinkerpop.addEdge("contains", tinkergraph, "weight", 1)
tinkergraph.addEdge("supports", gremlin, "weight", 1)
javeme = graph.addVertex(T.label, "person", T.id, "javeme",
"name", "Jermy Li", "age", 29, "addr",
"Beijing", "weight", 1)
zhoney = graph.addVertex(T.label, "person", T.id, "zhoney",
"name", "Zhoney Zhang", "age", 29,
"addr", "Beijing", "weight", 1)
linary = graph.addVertex(T.label, "person", T.id, "linary",
"name", "Linary Li", "age", 28,
"addr", "Wuhan. Hubei", "weight", 1)
hugegraph = graph.addVertex(T.label, "software", "name", "HugeGraph",
"lang", "java", "tag", "Graph Database",
"weight", 1)
javeme.addEdge("created", hugegraph, "weight", 1)
zhoney.addEdge("created", hugegraph, "weight", 1)
linary.addEdge("created", hugegraph, "weight", 1)
javeme.addEdge("knows", zhoney, "weight", 1)
javeme.addEdge("knows", linary, "weight", 1)
hugegraph.addEdge("implements", tinkerpop, "weight", 1)
hugegraph.addEdge("supports", gremlin, "weight", 1)
alaro = graph.addVertex(T.label, "person", T.id, "dalaro",
"name", "Dan LaRocque ", "age", 0,
"addr", "", "weight", 1)
mbroecheler = graph.addVertex(T.label, "person", T.id, "mbroecheler",
"name", "Matthias Broecheler",
"age", 0, "addr", "San Francisco", "weight", 1)
titan = graph.addVertex(T.label, "software", "name", "Titan",
"lang", "java", "tag", "Graph Database", "weight", 1)
dalaro.addEdge("created", titan, "weight", 1)
mbroecheler.addEdge("created", titan, "weight", 1)
okram.