Orient DB Java Api to return multiple values - orientdb

I'm trying to get multiple values by running an OrientDb command from Java. Specifically I am trying to get a list of Vertices that are linked to a vertex and the #rid of the Edges.
E.g If vertex V1 is linked to vertex V2 by edge E1, my query for V1 should return #rid of E1 and V2.
I can do that in Orient Studio by running the query:
select #rid, expand(in) from ExampleEdge where out = '#14:33'
How can I code the above query in Java? All the examples are showing only single value results like:
Iterable<Vertex> vertexes = graph.command(new OCommandSQL("select expand(in()) from node where #rid = '#14:33'")).execute();

I have this simple structure:
to get the RIDS, you can use this code:
String yourRid = "#12:0";
Iterable<Vertex> targets = g.command(new OSQLSynchQuery<Vertex>("select from ?")).execute(yourRid);
for (Vertex target : targets) {
Iterable<Edge> r = target.getEdges(Direction.IN, "exampleEdge");
List<Edge> results = new ArrayList<Edge>();
CollectionUtils.addAll(results, r.iterator());
System.out.println("Starting Vertex: "+yourRid);
System.out.println();
for (Edge result:results){
System.out.println("Edge "+result.getId()+" connected with Vertex "+result.getVertex(Direction.OUT).getId());
}
}
Output:
Starting Vertex: #12:0
Edge #13:3 connected with Vertex #12:1
Edge #13:4 connected with Vertex #12:2
Edge #13:5 connected with Vertex #12:3

Related

How to get the properties of the shortest path between 2 vertexes

I am using Java, and I want to get the property" name "of each vertex of the shortest path between #26:1 and #24.0 . I am using the sql command select dijkstra (#26:1,#24.0,"distance") from V. And I get the result OResultSet. I dont know how the get the rid of each vertex in my java program (I mean OVertex or ORID of each vertex : objects offered by orientdb in my java program) .
try to do it with following code:
String query3 = "SELECT dijkstra (#26:1, #28:1, 'valeur') FROM V";
OResultSet rs3 = db.query(query3);
while(rs3.hasNext()) {
OResult row = rs3.next();
String rid= row.getProperty("#rid");
}
rs3.close();
For more information, you can look for in official java-api
I hope it will help you!

OrientDB: Read vertex and include connected vertex rids grouped by edge classes

I would like to display all connected record ids grouped on the edge-classes they are connected with.
V1
^
|
edgeClass1
|
V2 <--edgeClass1-- V0 --edgeClass2--> V3
/ \
edgeClass3 edgeClass4
\ /
v v
V4
From this I would like to be able to target V0 and get the whole V0 record as well as all connected vertices grouped by their edge classes as follows:
{
rid: <V0rid>
someV0Property: someV0Value,
edgeClass1: [<V1rid>, <V2rid>],
edgeClass2: [<V3rid>],
edgeClass3: [<V4rid>],
edgeClass4: [<V4rid>]
}
The available edgeClasses are not known in advance, so I can't target them explicitly.
Is this possible?

How to get the available connected vertex in OrientDB

I have a case: I want to get all the connected vertex (including the middle vertex) from a base vertex.
For example, the graph as below
enter image description here
I want to query all the connected vertexes from vertex ("giggs"), and I also want to query the connected path. ex: "giggs"->"192.168.0.1"->"ronaldo"->"192.168.0.2"->"veri". I used query as below:
MATCH {class: ic, as: s, where: (title = 'giggs')}.(outE(){where: 'some condition'}.inV().inE(){where: 'some condition'}.outV()){class: %s, as: t, while: ($depth <= 5), where: ($matched.s != $currentMatch)} RETURN $paths
I can get all the target nodes, ex: "veri", but I don't know the preceding vertex of "veri" and the edge between "veri" and its preceding vertex.
So how I can write the query? Thanks in advance.
Try this:
TRAVERSE both() FROM (SELECT EXPAND(s) FROM (MATCH {CLASS:ic, AS:s, WHERE:(name='giggs')} RETURN s))
Hope it helps
Regards

How to get all vertices of all outgoing edges from a vertex scala gremlin

I need to get all list of vertices label of all outgoing egdes from a vertex using scala gremlin.
My code looks like below,
val names :ListBuffer[String] = ListBuffer()
val toList: List[Vertex] = graph.V().hasLabel(100).outE().outV().toList()
for(vertex <- toList){
names += vertex.label()
}
Its returning the same label name for all vertex
Eg :
Vertex A is having outE to B,C,D . It returns the label of A.
Output:
ListBuffer(100, 100, 100)
Anything am i missing?
I believe you asking for the wrong vertex in the end. Honestly, I often make the same mistake. Maybe this is the traversal you looking for:
graph.V().hasLabel(100).outE().inV().label().toList()
If you like me and often get confused by inV() and outV() you can use otherV which gets the opposite vertex. Like so:
graph.V().hasLabel(100).outE().otherV().label().toList()
Finally you can even shorten your traversal by not explicitly stating the edge part:
graph.V().hasLabel(100).out().label().toList()
By using out() instead of outE() you don't need to specify you want the vertex, out() gets you the vertex directly.

Can't delete/remove multiple property keys on Vertex Titan 1.0 Tinkerpop 3

Very basic question,
I just upgraded my Titan from 0.54 to Titan 1.0 Hadoop 1 / TP3 version 3.01.
I encounter a problem with deleting values of
Property key: Cardinality.LIST/SET
Maybe it is due to upgrade process or just my TP3 misunderstanding.
// ----- CODE ------:
tg = TitanFactory.open(c);
TitanManagement mg = tg.openManagement();
//create KEY (Cardinality.LIST) and commit changes
tm.makePropertyKey("myList").dataType(String.class).cardinality( Cardinality.LIST).make();
mg.commit();
//add vertex with multi properties
Vertex v = tg.addVertex();
v.property("myList", "role1");
v.property("myList", "role2");
v.property("myList", "role3");
v.property("myList", "role4");
v.property("myList", "role4");
Now, I want to delete all the values "role1,role2...."
// iterate over all values and try to remove the values
List<String> values = IteratorUtils.toList(v.values("myList"));
for (String val : values) {
v.property("myList", val).remove();
}
tg.tx().commit();
//---------------- THE EXPECTED RESULT ----------:
Empty vertex properties
But unfortunately the result isn't empty:
System.out.println("Values After Delete" + IteratorUtils.toList(v.values("myList")));
//------------------- OUTPUT --------------:
After a delete, values are still apparent!
15:19:59,780 INFO ThriftKeyspaceImpl:745 - Detected partitioner org.apache.cassandra.dht.Murmur3Partitioner for keyspace titan
15:19:59,784 INFO Values After Delete [role1, role2, role3, role4, role4]
Any ideas?
You're not executing graph traversals with the higher level Gremlin API, but you're currently mutating the graph with the lower level graph API. Doing for loops in Gremlin is often an antipattern.
According to the TinkerPop 3.0.1 Drop Step documentation, you should be able to do the following from the Gremlin console:
v = g.addV().next()
g.V(v).property("myList", "role1")
g.V(v).property("myList", "role2")
// ...
g.V(v).properties('myList').drop()
property(key, value) will set the value of the property on the vertex (javadoc). What you should do is get the VertexProperties (javadoc).
for (VertexProperty vp : v.properties("name")) {
vp.remove();
}
#jbmusso offered a solid solution using the GraphTraversal instead.