How to merge Two Vertex details and Edge Properties details Together in a single gremlin query - gremlin

How to merge Two Vertex details and Edge Properties details Together in a single gremlin query.
I'm having two Vertices :
UserInfo vertex
PostInfo vertex
EdgeInfo Between them
With Gremlin Query : g.v(2569472).out('_label','WallPost')[0..1]
I'm getting UserPost Vertex details , i want to add UserVertex detail in the response (i.e gender ) and Edge property detail in the response (i.e EdgeMessage)
I'm trying to compare sql equivalent innerjoin operation with gremlin from sql2gremlin but i'm not able to get the desired result.

SQL2Gremlin is written for TinkerPop 3, you're still using TinkerPop 2 (which is a lot more complicated IMO). Anyway, here's how you would do it in TP2:
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.v(1).outE("knows")
==>e[7][1-knows->2]
==>e[8][1-knows->4]
gremlin> g.v(1).outE("knows").inV().retain([g.v(4)])
==>v[4]
gremlin> g.v(1).as("x").outE("knows").as("y").inV().retain([g.v(4)]).select(["x","y"]) {it.map()} {it.weight}
==>[x:{name=marko, age=29}, y:1.0]

Related

is it possible to declare a variable with gremlin node values and later traverse using the variable

I wonder if gremlin can handle a data structure graph where you can save the reference to the node in a variable and later use it to traverse neighboring nodes.
node1= g.V(1)
node1.out('knows')
There are two confusing aspects here:
A Traversal is a kind of Iterator that can only be used once
The Gremlin Console iterates each Traversal for a significant (configurable) number of times
The second aspect can be prevented with the code example below. The first aspect makes the answer to your question a practical "no". Of course you can code some kind of abstraction that fulfills your needs yourself.
gremlin> node1 = g.V(1); null
==>null
gremlin> node1.class
==>class org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal
gremlin> contacts = node1.out('knows'); null
==>null
gremlin> contacts
==>v[2]
==>v[4]
gremlin> contacts
gremlin>

gremlin query to add list of values as edge property

I am trying to find a way to add list of values as a property of an edge on the graph. I was able to do it on a vertex with the following query but it doesn't work for an edge.
e.g
gremlin> g.V(2).property(list, 'test', 'a')
==>v[2]
gremlin> g.V(2).property(list, 'test', 'b')
==>v[2]
gremlin> g.V(2).values('test')
==>a
==>b
gremlin>
If i do the similar thing on an edge i see the following error. Edge property being an list is not supported for edge or what?
gremlin> g.E(2).property(list, 'test', 'a')
org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jEdge cannot be cast
to org.apache.tinkerpop.gremlin.structure.Vertex
Type ':help' or ':h' for help.
My requirement is to add/remove a value (string) from the list (property of an edge)
I am using neo4j as my database.
Edges do not support Cardinality. That is a structural feature allowed to vertices only. I believe that you can however store an array of primitives in Neo4j so you could write your "list" that way.

How to use a UUID as id in Gremlin?

I'm adding verticles like this:
g.addV("foobar").property("id", 1).property(...etc...
How can I set a property with a uuid instead of an integer id?
An "id" can have multiple meanings. If you simply mean that you want to use a UUID as a unique identifier to lookup your vertices then taking the approach you have is fine, when used in conjunction with the underlying indexing functionality of your chosen graph database. In other words, as long as you have an index on "id" then you will quickly find your vertex. In this sort of usage, "id" is really just a property of the vertex obviously and you may find that for certain graph databases that "id" is actually a reserved term and can't be used as a property key. It is likely best to choose a different key name.
If instead of using "id" as a property key, you mean that you wish to set the actual vertex identifier, referred to by T.id, as in:
g.addV(T.id, uuid)
then you first need to use a graph database implementation that allows the assignment of identifiers. TinkerGraph is one such implementation. In this way, you natively assign the identifier of the vertex rather than allowing the graph database to create it for you.
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV(id, UUID.randomUUID())
==>v[c2d673de-2425-4b42-bc1e-68ff20e3b0a8]
gremlin> g.V(UUID.fromString("c2d673de-2425-4b42-bc1e-68ff20e3b0a8"))
==>v[c2d673de-2425-4b42-bc1e-68ff20e3b0a8]

Cosmos DB Search Query which takes Vertex values with any character

I am using Azure cosmos Db, In cosmosDB i have so many vertexes, each vertex has properties in key value form. I want to find any gremlin query which checks Vertex values which start with any character.
Filter Query is there but for azure filter query is not supported, so is there any other Gremlin query which takes Vertex properties values which start with any character?
Although I never tried CosmosDB myself, I have to disagree with John. According to CosmosDB's docs, any range query on any property is processed from the index. Hence, if you want to find all person vertices that have a name property that starts with a, you can do:
g.V().has("person", "name", between("a", "b"))`
A concrete example over TinkerPop's toy graph:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has("name", between("m", "n")).valueMap()
==>[name:[marko], age:[29]]
gremlin> g.V().has("name", between("j", "k")).valueMap()
==>[name:[josh], age:[32]]
gremlin> g.V().has("name", between("j", "n")).valueMap()
==>[name:[marko], age:[29]]
==>[name:[lop], lang:[java]]
==>[name:[josh], age:[32]]
“start with any character” requires Full-Text search, yet Cosmos DB does not support it per their documentation https://learn.microsoft.com/en-us/azure/cosmos-db/gremlin-support#gremlin-steps .
JanusGraph support Full-Text search or String search, for example:
g.V().has('bookname', textPrefix('uni'))
For more info please refer to http://docs.janusgraph.org/latest/index-parameters.html#text-search

Tinkerpop - how do I find a node in the graph?

I can't seem to find a specific node in the graph without traversing the whole thing. Is there something I'm missing?
I'm using tinkerpop blueprints.
Orientdb gives some sort of unsemantic id to a node such as '#8:1' - how do I find this without knowing the id? vertex has a property like 'user=jason' that will identify it.
I'm thinking I'll just use redis to store the user/location pair or otherwise use a supernode (no thanks)
Blueprints has the notion of key indices.
https://github.com/tinkerpop/blueprints/wiki/Graph-Indices
Given your example, define a key index for "user", then query it with the key index. Here's an example using OrientDB from a Gremlin prompt:
gremlin> g = new OrientGraph("memory://graph")
==>orientgraph[memory://graph]
gremlin> g.createKeyIndex("user", Vertex.class)
==>null
gremlin> g.addVertex([user:"Jason"])
==>v[#8:-3]
gremlin> g.addVertex([user:"Rick"])
==>v[#8:-4]
gremlin> g.stopTransaction(SUCCESS)
==>null
gremlin> g.V('user','Jason')
==>v[#8:1]

Resources