Gremlin Java replace old vertex properties with new property - gremlin

I am trying to use gremlin java to replace some vertex's property like this:
graph.V(1).properties().drop().property("foo", "bar").iterate()
However, this only remove property but doesn't add new property.
I think iterate should probably be immediately after drop, but I am connecting to remote graph db so I wish to reduce query count.
Hope there is some way to achieve this:
graph.V(1).properties().drop().property("foo", "bar")
.V(2).properties().drop().property("foo", "bar").....iterate()

Yes, you can definitely do that, check out the live example: https://gremlify.com/27
You can select the vertex and see it's properties, or run g.V().valueMap(true) and afterwards:
g.V().sideEffect(properties ().drop()).property ("foo", "boo");
To see the new properties applied to it.
(For simplicity, I assumed a single vertex on the graph but this applies for any vertex).

The answer by #gremlify is correct. And alternatively you can use .union() command also.
g.V().union(
properties().drop(),
property('foo', 'boo')
);

Related

Gremlin optional as empty

Could you please help me to understand whether there is any option where we can skip the optional in the gremlin,
the scenario is, we are traversing through a graph and has an optional statement
depending on a query parameter in the request, I will be having a traverse path for optional, if not, there is no path for the optional
for example,
if query_parameter:
path=has('labeltest').inE().outV()
g.V('test').optional(path)
as optional path is for the query_parameter available condition, this is failing and returning no result if query parameter not available. We don't prefer to repeat the code on the g.v() by an if and else condition. Any thoughts to tell the optional to not do anything or dummy options to return the previous path. Appreciate your thoughts as I am new to this, thank you in advance
Regards
Hari
In the case where you want nothing to happen you could just set your path variable to be identity(). The effect would be as follows:
gremlin> g.V('3').optional(identity())
==>v[3]
Specifically, in Python you can do this:
>>> p=__.identity()
>>> g.V('3').optional(p).next()
v[3]
If you are doing this from python using the Gremlin Language Variant (GLV) then you can only add the optional portion of the query when needed. GLV's are lazily evaluated so you can conditionally construct them in code. They will only be executed when you add a terminal step (e.g. toList()). This means that you can build your traversal like this:
t=g.V('test')
if query_parameter:
t=t.has('labeltest').inE().outV()
res=t.toList()
With this code, the optional path portion of the traversal will only be executed when query_parameter is True, else it will just return the test vertex.

Removing a label from Vertex

Amazon Neptune supports multiple labels for a vertex. While I'm able to add a new label to vertex, I couldn't find a way to remove it.
I found out that
vertex.addLabel('human')
vertex.removeLabel('human')
http://tinkerpop.apache.org/docs/current/reference/#_multi_label
is not supported on AWS Neptune.
Also trying drop() labels like properties not working.
Neptune documentation says:
gremlin> g.addV('label1').property(id, 'customid')
gremlin> g.addV('label2').property(id, 'customid')
gremlin> g.V('customid').label()
==>label1::label2
But that way you can only add label nothing about the removal of the label.
I am looking for some way like removeLabel() to remove label without removing the vertex.
You will find this text in the Apache TinkerPop documentation at [1]
"This is because TinkerPop does not allow the vertex label to be changed after the vertex has been created."
The ability to change a vertex label after it is created is not allowed per the TinkerPop reference documentation and implementation. This as far as I know is honored by most if not all of the TinkerPop enabled Graph DBs.
The example you found is part of the custom support for Neo4J when connected directly (not via a Gremlin Server style of connection) where the vertex object can be directly manipulated.
If you need the concept of an editable label I would suggest using a property instead. The net result will be about the same in terms of looking up by property versus by label and a lot more portable.
[1] http://tinkerpop.apache.org/docs/current/reference/#_graphml_reader_writer

Neo4j: How to use APOC apoc.algo.cover procedure?

Hi,
I try to use the "cover" function from APOC like this :
WITH ["f1,"f2",...] as list1
MATCH (n:Frag)
WHERE n.frag in list1
WITH COLLECT(ID(n)) as nodeIds
CALL apoc.algo.cover(nodeIds)
YIELD rel
RETURN rel
It works but it is very slow the first time. If I do it once again, it becomes muck quicker! What does that mean?
Probably your issue is not related to apoc.algo.cover usage, but to the WHERE part of your query. You can try a performance improvement adding an index in the Frag.frag property.
CREATE INDEX ON :Frag(frag)
After creating the index run your query again. Note that the index is not immediately available, but will be created in the background.

In tensorflow, can you define your own collection name?

I searched all resources in tensorflow's API documents and can't find any indication.
It seems when using get_variable(), I can put a specific name for collections term like:
x=tf.get_variable('x',[2,2],collections='my_scope')
but get only empty list when doing:
tf.get_collection('my_scope')
collectionS needs a list of collection name.
>>x = tf.get_variable('x',[2,2], collections=['my_scope'])
>>tf.get_collection('my_scope')
[<tensorflow.python.ops.variables.Variable at 0x10d8e1590>]
watch out that if you use it some other operations can have side effects.
like tf.all_variables() will not work and thus tf.initialize_all_variables() also will not see your variable. One way to fix it is to specify the default collection too.
>>x = tf.get_variable('x',[2,2], collections=['my_scope', tf.GraphKeys.VARIABLES])
but things starts to get tedious.
Actually, you can use tf.get_collection to create a new collection:
tf.get_collection('my_collection')
var = tf.get_variable('var', [2, 2], initializer=tf.constant_initializer())
tf.add_to_collection('my_collection', var)

How do you take advantage of a composite index restricted to a vertex label?

Suppose I have defined a composite index and vertex label like so:
mgmt = g.getManagementSystem()
name = mgmt.makePropertyKey('name').dataType(String.class).make()
god = mgmt.getVertexLabel('god')
mgmt.buildIndex('byName',Vertex.class).addKey(name).indexOnly(god).buildCompositeIndex()
mgmt.commit()
How do I take advantage of this index in Gremlin?
Assuming it is something like g.V('label','god').has('name','zeus'), which is more performant?
g.V('label','god').has('name','zeus')
g.V('name','zeus').has('label','god')
Traditionally we would go with (2) because name is more highly selective than label, but is there something special about vertex labels that makes this untrue?
Ok, you've already found out, that it's currently impossible in Gremlin and only available through the Query API. Regarding the performance: Your two approaches won't make a difference; under the hood both queries generate the same Cassandra/HBase query (or whatever storage backend you are using).
Cheers,
Daniel
It doesn't work yet in TP2, but will work with TP3 (Titan 0.9 / next release). Currently you can do this:
g.query().has('label','god').has('name','zeus').vertices()
No idea about the performance profile still.
https://github.com/thinkaurelius/titan/issues/755

Resources