Get all vertex in JgraphT - jgrapht

In jgrapht, I have added some vertex.
I wanted to know how to get all vertex that I have added or already exist in jgrapht ?
Is there a way to get it ?

This will give all the vertex in the graph.
DefaultDirectedGraph<String, DefaultEdge> directedGraph = new DefaultDirectedGraph<Node, DefaultEdge>(
DefaultEdge.class)
directedGraph.vertexSet();

You can use what Anto Livish's answer if you just have a graph
If you create a GraphPath (like a shortest path), you can use getVertexList() and store them in a list

Related

AWS Neptune find id of all the vertices connected to a particular vertice

I am trying to get ids of all the vertices connected to a particular vertex. Where id is not "id" property, instead its the id of the node (T.id)
g.V().has(T.id, "id1").both("is")
==>v[id2]
==>v[id3]
==>v[id4]
But when I am trying to get only the ids of the vertices I don't get any.
g.V().has(T.id, "id1").both("is").values("id")
g.V().has(T.id, "id1").both("is").id
What I want is something like below:
==>id2
==>id3
==>id4
If I understood you correctly,
You can use this:
g.V("id1").both("is").id()
Anyway if you want to get the id you can use id() step

Gremlin Drop Multiple Vertices

I am trying to drop all vertices returned by a given Gremlin query. The goal is to delete all children vertices which are children of a specific vertex.
Here's an example:
gremlin> g.V('dcb26be6-8d39-ae81-6ef2-6f60d06bce10').emit().repeat(out())
==>v[dcb26be6-8d39-ae81-6ef2-6f60d06bce10]
==>v[16b26be6-8d37-e882-38c6-a56f39ee4259]
==>v[9cb26be6-8d3c-d61e-4ab4-6c6993e8be7a]
==>v[82b26be6-8d3a-c01a-3771-085c94d1780a]
==>v[00b26be6-8d3c-68d9-6871-702a1247a692]
==>v[d4b26be6-8d38-81ea-b75d-25bbf563f81e]
==>v[cab26be6-8d39-3611-76fa-f369eab9d50e]
This query returns all vertices that have outward facing edges connected to the parent dcb26be6-8d39-ae81-6ef2-6f60d06bce10 vertex. Is there an easy way to drop all of the vertices returned by this query?
Thanks
EDIT:
#stephan had a great response however if the children have edges pointing to eachother gremlin gets mad at me. Check this out:
gremlin> g.V('2ab26c9e-1bbb-73f6-4ee8-6cecc7e21ee1').emit().repeat(out()).fold().unfold()
==>v[2ab26c9e-1bbb-73f6-4ee8-6cecc7e21ee1]
==>v[0eb26c9e-1bbc-12f3-e074-d7328ee4984e]
**==>v[92b26c9e-1bbd-b59f-0b5f-d4c985b176b6]**
==>v[18b26c9e-1bbf-a96c-90d3-e50e61fe7267]
==>v[12b26c9e-1bc1-40ee-292d-2bc7b08dcb9e]
==>v[ccb26c9e-1bbc-a82a-532f-7fbdea87deb1]
==>v[42b26c9e-1bbd-5f1f-f3ad-6f6670ab16ee]
==>v[7ab26c9e-1bc1-e773-6995-18159d610b77]
==>v[3ab26c9e-1bbe-add8-2ab2-948d7c9c0021]
**==>v[2eb26c9e-1bbf-1657-e212-98d1dfff33cd]**
**==>v[92b26c9e-1bbd-b59f-0b5f-d4c985b176b6]**
==>v[8cb26c9e-1bc2-500b-ae27-370a0cc4d392]
==>v[42b26c9e-1bc0-b4b0-4d54-fc7f20ca71d4]
==>v[7ab26c9e-1bc1-e773-6995-18159d610b77]
==>v[3ab26c9e-1bbe-add8-2ab2-948d7c9c0021]
**==>v[2eb26c9e-1bbf-1657-e212-98d1dfff33cd]**
As you can see vertex 92b26c9e-1bbd-b59f-0b5f-d4c985b176b6 appears twice as a response to this query. So when I try to do g.V('2ab26c9e-1bbb-73f6-4ee8-6cecc7e21ee1'). emit(). repeat(out()). fold(). unfold()
Here's the response I get
gremlin> g.V('2ab26c9e-1bbb-73f6-4ee8-6cecc7e21ee1'). emit(). repeat(out()). fold(). unfold().drop()
{"requestId":"2def0086-d71f-42e4-9c5f-c692d07cc96a","detailedMessage":"The
vertex does not exist 92b26c9e-1bbd-b59f-0b5f-
d4c985b176b6","code":"ConstraintViolationException"}
Is there any way to remove duplicates from the initial query?
Maybe it's just the end of the day and my brain is fried, but how about:
g.V('dcb26be6-8d39-ae81-6ef2-6f60d06bce10').
store('a').
repeat(out().store('a')).
cap('a').
unfold().
drop()
or perhaps slightly less readable imo:
g.V('dcb26be6-8d39-ae81-6ef2-6f60d06bce10').
emit().
repeat(out()).
fold().
unfold().
drop()
You may get a nicer answer - maybe even from me :)
You need a barrier step, which both fold and cap are - however both cause side effects (they cost memory/processing power). The barrier step seems like a better fit for this:
g.V('dcb26be6-8d39-ae81-6ef2-6f60d06bce10')
.emit()
.repeat(out())
.barrier()
.drop()

Transform Array in Spark

val degrees: VertexRDD[Int] = graph.degrees
val ngb=graph.collectNeighbors(EdgeDirection.Out)
val deg2 = degrees.leftOuterJoin(ngb)
Now i want a key/value pair RDD where key is degree and value is neighbor vertex id.
Basically i want to change from (id,Array[1,2,3]) to {(id,1)(id,2)(id,3)}
I have tried nested map and creating a string for the outer map. I am stuck here. If anyone can help. Thanks in advance.
Sorry, got it myself.
Did flatMap(y => y) with identity function. It was so simple.

Get result as a sub graph rather than vertice : Gremlin - orient-db

Hi : I am a neebie in Gremlin and Orient-db. I was playing around with Gremlin and orient db in java. I was able to query my vertices and edges using the available methods and get the results.I am using back("Alias-name") to get the result vertices of my query.
My question is can I get a graph of all the result vertices ( the related graph and the information of the result) since right now I am able to get the vertices but I want the entire "sub graph" information of my resultant vertices in the same query.
Any help is greatly appreciated.
Here is a code with generic example:
GremlinPipeline startPipe = pipe.has("friend-name", "friend-name")
.in("friend-depends").as("friend-depends")
.outE("resource-depends").inV()
.has("resource-name", "car")
.back("friend-depends");
v(Friend)[#15:13]
v(Friend)[#15:7]
v(Friend)[#15:12]
The results are right but I would like to get the results as
Friend#15:13{friend-name:Frank,Friend-type:Personal,in_depends:#17:10 (friend of friends),... with edges} v2
Thanks,
Sabari
Gremlin does not provide an explicit subgraph function (as of the unreleased 2.5.0). The only way to get a subgraph with Gremlin is to explicitly extract those graph elements yourself. My preference is to simply sideEffect the elements to an in-memory TinkerGraph. You can see an example here:
http://gremlindocs.com/#recipes/subgraphing
Given your amended question, you may use the the path step to get individual parts of the path as in:
gremlin> g = TinkerGraphFactory.createTinkerGraph();
==>tinkergraph[vertices:6 edges:6]
gremlin> g.v(1).outE.inV.path
==>[v[1], e[9][1-created->3], v[3]]
==>[v[1], e[7][1-knows->2], v[2]]
==>[v[1], e[8][1-knows->4], v[4]]
gremlin> g.v(1).outE.inV.has('age',T.gte,31).path
==>[v[1], e[8][1-knows->4], v[4]]
That looks a bit like what you are looking for. From there you could sideEffect to a subgraph. From there you could choose to not use back anymore:
gremlin> g.v(1).outE.inV.has('age',T.gte,31).path.sideEffect{println it}.collect{it.last()}
[v[1], e[8][1-knows->4], v[4]]
==>v[4]
or stick with it:
gremlin> g.v(1).as('x').outE.inV.has('age',T.gte,31).path.sideEffect{println it}.back('x')
[v[1], e[8][1-knows->4], v[4]]
==>v[1]

Using a new KeySpaces in Titan Cassandra and persisting data

I have just started using Titan over Cassandra. I am new to Titan and also new to the the Graph Database Concept. Just followed the instructions on github and wiki.
Configuration conf = new BaseConfiguration();
conf.setProperty("storage.backend", "cassandra");
conf.setProperty("storage.hostname", "127.0.0.1");
TitanGraph g = TitanFactory.open(conf);
This is how I opened the graph.
I understand that the default key space is titan. I created some nodes and relations in the default key space. I did Indexing on the vertex and queried the nodes and were able to iterate through the results.
Now my questions -
1) How do I set a new Key Space ?
I tried using the property
conf.setProperty("storage.keyspace ", "newkeyspace");
Unfortunately, when I checked the cassandra keyspaces, I could only find titan. There was no keyspace by the name newkeyspace. What could be the reason?
2) How do I persist a graph have been created?
for example,
g.createKeyIndex("name", Vertex.class);
Vertex juno = g.addVertex(null);
juno.setProperty("name", "juno");
juno.setProperty("type", "node");
Vertex juno1 = g.addVertex(null);
juno1.setProperty("name", "juno");
juno1.setProperty("type", "node1");
This is one sample graph. Once I issue a query of the form
Iterator<Vertex> junoIterator = g.getVertices("name", "juno")
.iterator();
while (junoIterator.hasNext()) {
Vertex vertex = (Vertex) junoIterator.next();
System.out.println(vertex.getProperty("type"));
}
My expected result is
node
node1
The same query should work fine once I comment the following segment -
/*g.createKeyIndex("name", Vertex.class);
Vertex juno = g.addVertex(null);
juno.setProperty("name", "juno");
juno.setProperty("type", "node");
Vertex juno1 = g.addVertex(null);
juno1.setProperty("name", "juno");
juno1.setProperty("type", "node1");*/
Here, what I believe is that the nodes, relations and index already have been built and is persisted in some datastore. Am I of the wrong belief?
Please advice.
I have figured out the issue. Was just a matter of commit.
A single line g.commit(); solves it all.
To create a new Titan Table(Keyspace in cassandra) in please use the property :
set("storage.cassandra.keyspace","TitanTest")
the whole command wud be like :
graph = TitanFactory.build().
set("storage.backend","cassandra").
set("storage.hostname","127.0.0.1").set("index.search.backend","elasticsearch").set("storage.cassandra.astyanax.cluster-name","Test Cluster").set("storage.cassandra.keyspace","TitanTest").set("cache.db-cache","true").open();

Resources