I cannot select a specific vertex, by executing g.V(3640).valueMap(true).unfold(). Any command which contains an ID between the parentheses in the g.V() command does not seem to work.
This is what I did:
I'm new to Graph databases and experimenting with the Gremlin console. I started by creating an instance:
graph = TinkerGraph.open()
g=graph.traversal()
and loading sample data by importing a .graphml database file:
g.io(graphml()).readGraph('/full/path/to/air-routes-latest.graphml')
which seemed to work fine because a count gives a nice result back
gremlin> g.V().count()
==>3642
Unfortunately the following does not work:
gremlin> g.V(3640).valueMap(true).unfold()
Which I think is odd, because by executing the following
gremlin> g.V()
==>v[3640]
==>v[2306]
...
the ID does seem to exist. Any ideas why I cannot access a specific ID? I tried different commands but g.V() seems to work fine, and g.V(3640) does not. Is it because I use TinkerGraph instead of a Gremlin database, of what might be the problem?
EDIT:
It seems that my id's were saved as strings, because g.V("2").valueMap(true).unfold() does give me results.
I think you likely have an issue with the "type" of the identifier. I suspect that if you do:
g.V(3640L)
that you will get the vertex you want. By default, TinkerGraph handles id equality with equals() so if you try to find an integer when the id is a long it will act like it's not there. You can modify that default if you like with an IdManager configuration discussed here. Note that this is also discussed in more detail in Practical Gremlin.
Related
On an Azure cosmosDB gremlin instance,
I have 2 vertices A and B linked by and edge E.
Both vertices has a 'name' property.
I'd like to run a query which will take A's name and put it in B
when I run
g.V("AId").as("a").oute().inv().hasLabel('B').property("name",select('a').values('name'))
I get the following error :
GraphRuntimeException ExceptionMessage : Gremlin Query Execution Error: Cannot create ValueField on non-primitive type GraphTraversal.
It looks like the select operator is not correctly used.
Thank you for your help
EDITED based on discussion in comments
You have oute and inv in lower case. In general, the steps use camelCase naming, such as outE and inV (outside of specific GLVs), but in the comments it was mentioned that CosmosDB will accept all lower case step names. Assuming therefore, that is not the issue here, the query as written looks fine in terms of generic Gremlin. The example below was run using TinkerGraph, and uses the same select mechanism to pick the property value.
gremlin> g.V(3).as("a").outE().inV().has('code','LHR').property("name",select('a').values('city'))
==>v[49]
gremlin> g.V(49).values('name')
==>Austin
What you are observing may be specific to CosmosDB and it's probably worth contacting their support folks to double check.
I have an application with more than 3000 vertices having the same label , let's say ABC. It is required for my application to get the list of all the vertices and their properties for the user to choose the entity and interact with it. For that I am writing a GetAllVertices query for label ABC.
The id's of the vertices are numbers
Ex: 1,2,3,..
The following query returns the correct amount of vertices ~ 3000
g.V().hasLabel('ABC').dedup().count()
The following query however only returns around 1600 entries
g.V().hasLabel('ABC').elementMap()
I am trying to understand what is happening and how can I get the elementMap for all the vertices that I am interested in. I think it might be because of the hash function elementMap() might be using that is causing the collision of the keys and thus resulting in overwriting some of the keys with different entries.
Using TinkerGraph I am not able to reproduce this behavior.
gremlin> g.inject(0).repeat(addV('ABC').property(id,loops())).times(3000)
==>v[2999]
gremlin> g.V().hasLabel('ABC').count()
==>3000
gremlin> g.V().hasLabel('ABC').elementMap().count()
==>3000
If you can say more about the data in your graph I can do some additional tests and try to reproduce what you are seeing.
UPDATED 2022-08-03
I ran the same test on Amazon Neptune version 1.1.1.0.R4 from a Neptune notebook, and it worked there as well.
%%gremlin
g.inject(0).repeat(addV('ABC').property('p1',loops())).times(3000)
v[a6c131cc-42e8-3713-c82d-faa193b118a0]
%%gremlin
g.V().hasLabel('ABC').count()
3000
%%gremlin
g.V().hasLabel('ABC').elementMap().count()
3000
I am trying to execute a math query.
gts.V()
.hasLabel("account")
.has("id",42)
.both("account_label1").as("label1")
.and(__.identity()
.project("a","b")
.by(__.identity()
.both("label1_label2")
.both("label2_label3")
.values("createTime"))
.by(__.identity()
.both("label1_label4")
.both("label4_label5")
.values("launchTime"))
.math("floor((a-b)/(86400))").is(100))
.select("label1")
.toList()
Above query fails with error
The provided traverser does not map to a value: v[137]->[IdentityStep, VertexStep(BOTH,[label1_label2],vertex), VertexStep(BOTH,[label2_label3],vertex), NoOpBarrierStep(2500), PropertiesStep([createTime],value)]
Why is gremlin injection NoOpBarrierStep?
What is the meaning of the NoOpBarrierStep(2500)?
What will be the correct gremlin query for the same?
When you use project() it expects a value for each by() modulator and that value should not produce an empty Iterator. Here's a simple example:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().project('x').by(out())
==>[x:v[3]]
The provided traverser does not map to a value: v[2]->[VertexStep(OUT,vertex)]
Type ':help' or ':h' for help.
Display stack trace? [yN]
The first vertex is able to traverse out() but the next one processed by project() has no outgoing edges and therefore produces this error. In your case, that simply means that not all of your traversers can traverse both().both() or if they can, you would want to be sure that they all had "createTime" property values. Either of those scenarios could cause the problem.
You could fix this in a variety of ways. Obviously, if it's a data problem you could simply fix your data and always assume that the traversal path is right. If that's not the case, you need to write your Gremlin to be a bit more forgiving if the traversal path is not available. In my case I could do:
gremlin> g.V().project('x').by(out().fold())
==>[x:[v[3],v[2],v[4]]]
==>[x:[]]
==>[x:[]]
==>[x:[v[5],v[3]]]
==>[x:[]]
==>[x:[v[3]]]
Perhaps in your case you might do:
by(coalesce(both("label1_label2").both("label2_label3").values("createTime"),
constant('n/a')))
Note that you do not need to specify identity() for the start of your anonymous traversals.
Finally, in answer to your questions about NoOpBarrierStep, that step is injected into traversals where Gremlin thinks it can take advantage of a bulking optimization. You can add them yourself with barrier() step as well. Here's a quick description of "bulking" as taken from the TinkerPop Reference Documentation:
The theory behind a "bulking optimization" is simple. If there are one million traversers at vertex 1, then there is no need to calculate one million both()-computations. Instead, represent those one million traversers as a single traverser with a Traverser.bulk() equal to one million and execute both() once.
I wrote the simple query below to traversal between Person to Country but it’s not returning any results.
g.V().hasLabel("Person").as("p").out("from").hasLabel("Country").as("c").select("p", "c")
In the actual data, only Person vertices exists and no Country vertices or from edges exist. I expected to at least return p - basically I want to do a left outer join. However, if I have Country and from data as well, the query returns results.
I tried another query using match as well but still no results unless there are actual data:
g.V().hasLabel("Person").has("name","bob").match(__.as("p").out("from").hasLabel("Country").as("c")).select("p", "c")
I'm running these queries against Datastax Enterpise Graph.
Any idea why it’s returning no results?
The result you are getting is expected. If there are no "from" edges then the traverser essentially dies and does not proceed any further. Perhaps you could consider using project():
g.V().hasLabel("Person").
project('name','country').
by('name')
by(out('from').hasLabel('Country').values('name').fold())
With the "modern" toy graph in TinkerPop, the output looks like this:
gremlin> g.V().hasLabel('person').project('name','knows').by().by(out('knows').values('name').fold())
==>[name:v[1],knows:[vadas,josh]]
==>[name:v[2],knows:[]]
==>[name:v[4],knows:[]]
==>[name:v[6],knows:[]]
In the future, when you submit questions about Gremlin, please include a Gremlin script that can be pasted into a Gremlin Console which makes it easier to try to more directly answer your specific question.
I have setup my cassandra and titan running. And gremlin also works fine. I have connected gremlin to cassandra using,
gremlin>conf=new BaseConfiguration();
gremlin>conf.setProperty('storage.backend','cassandra');
gremlin>conf.setProperty('storage.hostname', '192.168.14.129');
gremlin>conf.setProperty('storage.keyspace','test');
gremlin>g=TitanFactory.open(conf);
And i have created a vertex,
gremlin> v1 = g.addVertex(label,"person","f_name","Anna");
==>v[8424]
How do i check if this data is entered into cassandra in test keyspace(already in cassandra)?
TinkerPop v3.x distinguishes between a Graph and a TraversalSource.
You should be doing the following only once:
graph = TitanFactory.open(conf)
g = graph.traversal()
Then execute all your traversals with:
g.V().some(...).gremlin(...).steps(...)
To find a Vertex by its id in Titan, you may have to cast the id to a Long. Assuming a Vertex with id 8424l, you can do:
g.V(8424l) // returns a traversal
g.V(8424l).next() // returns that vertex
You shouldn't be calling graph.traversal() more than once, as you get a performance hit every time. In the default Titan v1.0.0 setup, notice how the traversal initialization is done when starting Gremlin server (see conf/gremlin-server/gremlin-server.yaml which executes the scripts/empty-sample.groovy file).
According to your comment underneath here you want to retrieve the just added vertex. Please refer to the Tinkerpop reference. Using your g as notation for the graph it's simple as
gremlin> g.traversal().V(8424)
For getting the properties of the vertex read valueMap step in the reference. For obtaining the vertex not by its Id, but by its properties read has step. Be aware that I linked to the reference for Tinkerpop 3.2.0. You might chose a different version of that document to meet your very version of the stack.