I am interested in parameterizing the Gremlin Query in Java code as we do in case of SQL query using PreparedStatement (example : statement.setString(int, String), statement.setInt(int, int)).
Kindly, please let me know can we do this kind of stuff in Java for Gremlin query.
Thanks in advance.
Regards,
Kamal
For those looking for an answer here for Titan 1.x and TinkerPop 3.x:
gremlin> cluster = Cluster.open()
==>localhost/127.0.0.1:8182
gremlin> client = cluster.connect()
==>org.apache.tinkerpop.gremlin.driver.Client$ClusteredClient#412c995d
gremlin> client.submit("g.V(start).valueMap(m)",[start:1L, m:(['name','age'] as String[])]).all().get()
==>result{object={name=[marko], age=[29]} class=java.util.HashMap}
Check this out at https://github.com/tinkerpop/rexster/wiki/RexPro-Java
When possible, parameterize Gremlin scripts, as this leads to better overall performance. The above example can be done as a parameterized request as follows:
RexsterClient client = RexsterClientFactory.open("localhost", "tinkergraph");
List<Map<String, Object>> results = client.execute("g.v(start).map",
new HashMap<String, Object>(){{
put("start", 1);
}});
Map<String, Object> map = results.get(0);
System.out.println(map.get("name"));
Akshaya
Related
I am using Spark to make a JanusGraph from a data stream, but am having issues indexing and creating properties. I want to create an index by a vertex property called "register_id". I am not sure I'm doing it the right way.
So, here's my code:
var gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
gr1.close()
// This is done to clear the graph made in every run.
JanusGraphFactory.drop(gr1)
gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
var reg_id_prop = gr1.makePropertyKey("register_id").dataType(classOf[String]).make()
var mgmt = gr1.openManagement()
gr1.tx().rollback()
mgmt.buildIndex("byRegId", classOf[Vertex]).addKey(reg_id_prop).buildCompositeIndex()
When I run the above, I get an error saying:
"Vertex with id 5164 was removed".
Also, how do I check if I have vertices with a certain property in the graph or not in Scala. I know in gremlin, g.V().has('name', 'property_value') works, but can't figure out how to do this in Scala. I tried Gremlin-Scala but can't seem to find it.
Any help will be appreciated.
You should be using mgmt object to build the schema, not the graph object. You also need to make sure to mgmt.commit() the schema updates.
gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
var mgmt = gr1.openManagement()
var reg_id_prop = mgmt.makePropertyKey("register_id").dataType(classOf[String]).make()
mgmt.buildIndex("byRegId", classOf[Vertex]).addKey(reg_id_prop).buildCompositeIndex()
mgmt.commit()
Refer to the indexing docs from JanusGraph.
For your second question on checking for the existence of a vertex using the composite index, you need to finish your traversal with a terminal step. For example, in Java, this would return a boolean value:
g.V().has('name', 'property_value').hasNext()
Refer to iterating the traversal docs from JanusGraph.
Reading over the gremlin-scala README, it looks like it has a few options for terminal steps that you could use like head, headOption, toList, or toSet.
g.V().has('name', 'property_value').headOption
You should also check out the gremlin-scala-examples and the gremlin-scala traversal specification.
Group[code=a]->Choice[selected=true]
Group[code=a]->Choice[selected=false]
Group[code=a]->Choice[selected=false]
Group[code=b]->Choice[selected=false]
Group[code=b]->Choice[selected=false]
Group[code=c]->Choice[selected=false]
Group[code=c]->Choice[selected=true]
Given the above Vertices, I'm looking to query for Group Vertices, where a group does not have any Choice vertices, with a selected attribute as true.
Hence the result should return only Group b
Group[code=b]
Any help is appreciated.
Here's your graph - when asking questions about Gremlin it's always helpful to provide your sample data in this way:
graph = TinkerGraph.open()
g = graph.traversal()
g.addV('group').property('code','a').as('a').
addV('group').property('code','b').as('b').
addV('group').property('code','c').as('c').
addV('choice').property('selected',true).
addE('link').from('a').
addV('choice').property('selected',false).
addE('link').from('a').
addV('choice').property('selected',false).
addE('link').from('a').
addV('choice').property('selected',false).
addE('link').from('b').
addV('choice').property('selected',false).
addE('link').from('b').
addV('choice').property('selected',false).
addE('link').from('c').
addV('choice').property('selected',true).
addE('link').from('c').iterate()
One way to get the answer you want is to do a traversal like this:
gremlin> g.V().hasLabel('group').
......1> where(__.not(out('link').has('selected',true))).
......2> values('code')
==>b
The above answer is for TinkerPop 3.x. In TinkerPop 2.x the pattern is the same. You would basically do:
g.V().has('label','group').filter{ it._().out('link').has('selected',true).hasNext() }
I have been trying to figure this if-else in a Gremlin query.
Assume g.V({0}) is the group vertex below.
var q = "g.V({0}).as('groupName', 'groupId', 'ownerId').inE(eIsAdminOf, eIsMemberOf).as('rel', 'joinDate').outV().hasLabel(userLabel).as('memberId')";
//TODO:var q = "g.V({0}).as('groupName', 'groupId', 'ownerId').inE(eIsAdminOf";
//if .has('mCanList',true).inE(eIsAdminOf, eIsMemberOf)
//if .has('mCanList',false).inE(eIsAdminOf)
//, eIsMemberOf).as('rel', 'joinDate').outV().hasLabel(userLabel).as('memberId')";
I want the .inE(eIsAdminOf, eIsMemberOf) to be based on property value mCanList of true or false as in the comments above.
Have been trying a choose to no avail:
var q = "g.V({0}).as('groupName', 'groupId', 'ownerId','mCanList');
q += ".by(values('mCanList').choose(is(true),.inE(eIsAdminOf, eIsMemberOf), .inE(eIsAdminOf))";
q += '.as('rel', 'joinDate').outV().hasLabel(userLabel).as('memberId')”;
I am using node.js to build the gremlin query with the gremlin library. The worst option for me is to build 2 separate async queries which build the results separately based on
if .has('mCanList',true).inE(eIsAdminOf, eIsMemberOf) or
if .has('mCanList',false).inE(eIsAdminOf)
TIA
I'm not sure that I follow the reasoning behind all the step labeling that you have so I've mostly omitted that to demonstrate use of choose() which seems to be the focus of your question. I roughly approximated what I think your graph structure is based on how you described the problem (if you have future questions, please consider providing some sample graph creation code that can be easily cut/paste into the a Gremlin Console session). In any case, here's what I think you need:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV(id,1).property("mCanList",true).as('a').
......1> addV(id,2).as('b').
......2> addV(id,3).as('c').
......3> addE("isAdminOf").from("b").to("a").
......4> addE("isMemberOf").from("c").to("a").iterate()
gremlin> g.V(1).choose(has('mCanList',true),inE("isAdminOf","isMemberOf"),inE("isAdminOf"))
==>e[1][2-isAdminOf->1]
==>e[2][3-isMemberOf->1]
gremlin>
gremlin> g.V(1).property('mCanList',false)
==>v[1]
gremlin> g.V(1).choose(has('mCanList',true),inE("isAdminOf","isMemberOf"),inE("isAdminOf"))
==>e[1][2-isAdminOf->1]
If I try to directly edit your Gremlin I think your traversal basically just needs to look like this:
var q = "g.V({0});
q += ".choose(has('mCanList',true),inE(eIsAdminOf, eIsMemberOf), inE(eIsAdminOf))";
q += ".outV().hasLabel(userLabel)";
I presume that "eIsAdminOf", "eIsMemberOf" and "userLabel" are JS variables - if not they would need quotes around them if they happen to be actual label names. Again, I'm not clear on what you were doing with all the uses of as() - none of that seemed relevant to your traversal based on your question.
I am using Orientdb 2.2.10
What I want?
I want to get a vertex having uId = 'ram' of classtype = "Person" from my Graphdb.
My graphdb is indexed with unique key 'uid'.
How I am solving it?
I am using this function to get the vertex : graph.getVertexByKey(key);
I am getting the desire result but my IDE(eclipse) showing it as deprecated method
Is there any alternative of this in 2.2.10?
If not, is it safe to use it?
Thanks
You can use:
Iterable<Vertex> it=g.getVertices("uid", "ram");
Hope it helps.
Regards.
Correct me if I am wrong about EntityFramewroks.
As far as I understand, it is impossible to execute a query against the database when you do not know what results (objects) you are going to get in return because it needs to map the results to the expected object, which is passed when executing raw SQL query against the database.
Question: How can one execute SQL query without specifying the object?
Your help (and any input) will be highly appreciated.
Kind regards,
Moses
It is still unclear what you are asking, but I will adapt answer to your edits of your question.
I think you are referring to something like: http://msdn.microsoft.com/en-gb/data/jj592907.aspx
using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
}
or
using (var context = new BloggingContext())
{
var blogNames = context.Database.SqlQuery<string>(
"SELECT Name FROM dbo.Blogs").ToList();
}