本文為您介紹Gremlin多值屬性示例。
多值屬性
Gremlin語法中對頂點提供了多值屬性支持,在添加或者更新屬性時可以通過cardinality參數指定屬性的類型。
Gremlin提供了兩種類型的多值屬性:set和list。GDB從1.0.20版本開始支持set屬性。
注意事項
只有頂點支持多值屬性,邊不支持。
一個set屬性的多個value可以是不同類型,但是GDB在判斷值是否相同時是按照語義進行比較的,類型不同但是值相等的兩個value會被認為是相同的(例如1和1.0),因而只會保留一個。
屬性的cardinality可以改變,以最后一次調用property()更新屬性為準。set屬性更新為single類型后,只保留后者的值;single屬性更新為set類型后,前后兩個值都保留。
set屬性查詢結果中的多個值是按照語義排序的。如果是數值型,按值升序排列;如果是字符串型,按字典序排列。
set屬性和single屬性一樣,會自動建立索引,查詢性能跟single屬性相近。
Set屬性用法
Console
設置屬性
g.addV('person'). property(id, '11111'). property('name', 'marko'). property(set, 'email', 'marko@a.com'). property(set, 'email', 'marko@b.com')
查詢屬性
g.V('11111').properties('email') ==>vp[email->marko@a.com] ==>vp[email->marko@b.com]
刪除一個值
g.V('11111').properties('email').hasValue('marko@a.com').drop()
刪除所有值
g.V('11111').properties('email').drop()
Java
package com.gdb.alibaba;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
public class Test {
public static void main(String[] args) {
try {
if (args.length != 1) {
System.out.println("gdb-remote.yaml path needed");
return;
}
String yaml = args[0];
Cluster cluster = Cluster.build(new File(yaml)).create();
Client client = cluster.connect().init();
String dsl = "g.addV(yourLabel).property(propertyKey, propertyValue).property(set, setPropertyKey, setPropertyValue0).property(set, setPropertyKey, setPropertyValue1)";
Map<String, Object> parameters = new HashMap<>();
parameters.put("yourLabel", "person");
parameters.put("propertyKey", "name");
parameters.put("propertyValue", "marko");
parameters.put("setPropertyKey", "email");
parameters.put("setPropertyValue0", "marko@a.com");
parameters.put("setPropertyValue1", "marko@b.com");
ResultSet results = client.submit(dsl, parameters);
List<Result> result = results.all().join();
if (result.size() > 0) {
String vertexId = (String) ((DetachedVertex) result.get(0).getObject()).id();
parameters.put("yourId", vertexId);
}
dsl = "g.V(yourId).properties(setPropertyKey)";
results = client.submit(dsl, parameters);
result = results.all().join();
result.forEach(r -> {
Object element = r.getObject();
if (element instanceof DetachedVertexProperty) {
DetachedVertexProperty property = (DetachedVertexProperty) element;
System.out.println(property.key() + ": " + property.value());
}
});
cluster.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Python
用法1:
from gremlin_python.process.anonymous_traversal import traversal from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection from gremlin_python.process.traversal import T, Cardinality g = traversal().withRemote( DriverRemoteConnection('ws://HOST:PORT/gremlin', 'g', username=USER, password=PASS)) v = g.addV('person').property(T.id_, "123").property("name", "marko") \ .property(Cardinality.set_, "email", "marko@gmail.com") \ .property(Cardinality.set_, "email", "marko@hotmail.com").iterate() properties = g.V('123').properties('email') for prop in properties: print(prop.key + ": " + str(prop.value)) g.V('123').drop().iterate()
用法2:
from gremlin_python.driver import client client = client.Client('ws://HOST:PORT/gremlin', 'g', username=USER, password=PASS) dsl = 'g.addV("person").property(id, "123").property("name", "marko").property(set, "email", "marko@a.com").property(set, "email", "marko@b.com")' callback = client.submitAsync(dsl) for result in callback.result(): print(result) dsl = 'g.V("123").properties("email")' callback = client.submitAsync(dsl) for result in callback.result(): for prop in result: print(prop.key + ": " + str(prop.value))
Go
bindings := make(map[string]interface{})
bindings["GDB___id"] = "22"
bindings["GDB___label"] = "goTest"
bindings["GDB___PK"] = "name"
bindings["GDB___PV"] = "Jack"
bindings["GDB___PK_PHONE"] = "phone"
bindings["GDB___PV_PHONE1"] = "111111"
bindings["GDB___PV_PHONE2"] = "222222"
dsl := "g.addV(GDB___label).property(id, GDB___id).property(GDB___PK, GDB___PV).property(set, GDB___PK_PHONE, GDB___PV_PHONE1).property(set, GDB___PK_PHONE, GDB___PV_PHONE2)"
results, err := client.SubmitScriptBound(dsl, bindings)
if err != nil {
log.Fatalf("Error while querying: %s\n", err.Error())
}
// get response, add vertex should return a Vertex
for _, result := range results {
v := result.GetVertex()
log.Printf("get vertex: %s", v.String())
// read vertex property
for _, p := range v.VProperties() {
log.Printf("prop: %s", p.String())
}
}
Javascript
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
const __ = gremlin.process.statics;
const t = gremlin.process.t;
const cardinality = gremlin.process.cardinality
const authenticator =
new gremlin.driver.auth.PlainTextSaslAuthenticator(USER, PASS);
const graph = new Graph();
const g = graph.traversal().withRemote(
new DriverRemoteConnection('ws://HOST:PORT/gremlin', {authenticator}));
g.addV('person')
.property(t.id, '2222')
.property('name', 'james')
.property(cardinality.set, 'phone', '111111')
.property(cardinality.set, 'phone', '222222')
.iterate()
.then(data => {
g.V('2222').properties().toList().then(
properties => { console.log(properties); });
});
文檔內容是否對您有幫助?