Adding Label to the Vertex using TransactionalGraph

79 views
Skip to first unread message

Ram shaw

unread,
Apr 10, 2015, 9:22:29 AM4/10/15
to aureliu...@googlegroups.com
I want to add the label of the vertex, while using titanTransaction i got the method addVertexWithLabel but in transactiongraph i didn't get such method. How can i then add the label. 
using titangraph
------------------------
final TitanTransaction titanTransaction = graph.newTransaction();
Vertex vertexCustomer = titanTransaction.addVertexWithLabel("Customer");

using transactionalgraph
-----------------------------------
final TransactionalGraph tx = graph.newTransaction();
Vertex vertexCustomer = tx.addVertex(null);


Stephen Mallette

unread,
Apr 10, 2015, 9:55:39 AM4/10/15
to aureliu...@googlegroups.com
There is some discrepancy between some Titan features and Blueprints (to be rectified with TinkerPop3).  Blueprints does not support labels, so TransactionalGraph does not have such methods.  You must cast to titan specific classes to make use of such things.



--
You received this message because you are subscribed to the Google Groups "Aurelius" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aureliusgraph...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/aureliusgraphs/61041b1b-3a97-47f8-9390-36901a83b9b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ram shaw

unread,
Apr 14, 2015, 9:30:26 AM4/14/15
to aureliu...@googlegroups.com
Hi Stephen,

              Thanks for reply. I tried like you told but getting the below exception at runtime.
java.lang.RuntimeException: java.lang.ClassCastException: com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx cannot be cast to com.thinkaurelius.titan.core.TitanGraph at backtype.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:128) at backtype.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:99) at backtype.storm.disruptor$consume_batch_when_available.invoke(disruptor.clj:80) at 


Code which i tried is :
-------------------------------
final TransactionalGraph tx = graph.newTransaction();
Vertex vertexCustomer = ((TitanGraph)tx).addVertexWithLabel("Customer");

Stephen Mallette

unread,
Apr 15, 2015, 6:40:14 AM4/15/15
to aureliu...@googlegroups.com
All I meant by my original answer was the Blueprints doesn't support "labels" so you have to use Titan classes to do it.  Your code already shows that you can use the return value of g.newTransaction() (a StandardTitanTransaction) to use addVertexWithLabel, so why not use that?  

Ram shaw

unread,
Apr 15, 2015, 11:43:35 AM4/15/15
to aureliu...@googlegroups.com
Hi,

   As per titan documentation in "Table content - 9" says TransactionalGraph is thread-independent, can be used in multi-thread environment to speed up transaction processing. To make multi-thread graph, i am trying to use TransactionalGraph while creating vertex & edges with it's corresponding Label.


Thanks,
Ram

Daniel Kuppitz

unread,
Apr 15, 2015, 1:07:46 PM4/15/15
to aureliu...@googlegroups.com
Hi Ram,

g.newTransaction() returns an instance of type StandardTitanTx, which in turn implements the Blueprints TransactionalGraph interface (thus it can be used in a multi-thread environment like any other implementation of TransactionalGraph). So your first sample shows exactly what you should do:

final TitanTransaction titanTransaction = graph.newTransaction();
Vertex vertexCustomer = titanTransaction.addVertexWithLabel("Customer");

Either don't use a variable of type TransactionalGraph at all or cast it each time you need to add a labeled vertex:

final TransactionalGraph tx = graph.newTransaction();
Vertex vertexCustomer = ((TitanGraphTransaction) tx).addVertexWithLabel("Customer");

Cheers,
Daniel



it's thread there's no need to use the old Blueprints TransactionalGraph graph at all. 

Ram shaw

unread,
Apr 16, 2015, 9:53:47 AM4/16/15
to aureliu...@googlegroups.com
Thanks Daniel for reply. I have one more issue with my code which is running fine with single thread but while running with multi-thread getting exception like Local lock contention. For this issue i tried 
new TransactionRetryHelper.Builder<Vertex>(graph).perform(new TransactionWork<Vertex>() {
@Override
public Vertex execute(final TransactionalGraph graph) throws Exception
                                final TitanTransaction titanTransaction = graph.newTransaction();
                               Vertex vertexCustomer = titanTransaction.addVertexWithLabel("Customer");
                               Vertex vertexProviderCity = checkIfAleardyExistOrNot(titanTransaction, cityObject);
if (vertexProviderCity == null)
{
vertexProviderCity = titanTransaction.addVertexWithLabel(prop.getProperty("ProvCity"));
ElementHelper.setProperties(vertexProviderCity, "name", cityName);
}
                              Edge livesInCity = graph.addEdge(null, vertexCustomer ,vertexProviderCity,"LIVES_IN_CITY");
                                graph.commit();
return null;
}
}).build().exponentialBackoff(5, 200);

Daniel Kuppitz

unread,
Apr 16, 2015, 10:02:19 AM4/16/15
to aureliu...@googlegroups.com
        Edge livesInCity = graph.addEdge(null, vertexCustomer ,vertexProviderCity,"LIVES_IN_CITY")
        graph.commit();
        return null;
    }
}).build().exponentialBackoff(5, 200);

Shouldn't you use titanTransaction.addEdge(vertexCustomer, vertexProviderCity, "LIVES_IN_CITY") and titanTransaction.commit()?

Cheers,
Daniel

Daniel Kuppitz

unread,
Apr 16, 2015, 10:13:52 AM4/16/15
to aureliu...@googlegroups.com
Actually that code contains even more errors. I tried to fix them all (untested):

new TransactionRetryHelper.Builder<Edge>(graph.newTransaction()).perform(new TransactionWork<Edge>() {
    @Override
    public Edge execute(final TransactionalGraph graph) throws Exception { 
        final TitanTransaction tx = (TitanTransaction) graph;
        final Vertex vertexCustomer = tx.addVertexWithLabel("Customer");
        Vertex vertexProviderCity = checkIfAleardyExistOrNot(tx, cityObject);
        if (vertexProviderCity == null) {
            vertexProviderCity = tx.addVertexWithLabel(prop.getProperty("ProvCity"));
            vertexProviderCity.setProperty("name", cityName);
        }
        return tx.addEdge(vertexCustomer ,vertexProviderCity,"LIVES_IN_CITY");
    }
}).build().exponentialBackoff(5, 200);

Cheers,
Daniel

Ram shaw

unread,
Apr 16, 2015, 10:38:47 AM4/16/15
to aureliu...@googlegroups.com
Thanks Daniel. Actually I am using titanTransaction.addEdge(vertexCustomer, vertexProviderCity, "LIVES_IN_CITY") and titanTransaction.commit() only. Sorry for mistake in previous question. I will try above suggestion & will get back if needed further assistance.


Thanks,
Ram

Ram shaw

unread,
Apr 16, 2015, 10:50:10 AM4/16/15
to aureliu...@googlegroups.com

Hi Daniel, 
            I am changing a bit your code, & can you tell me is that will be fine or not because If commit will not be done inside execute() method then how to commit outside TransactionRetryHelper block of code?
new TransactionRetryHelper.Builder<Edge>(graph.newTransaction()).perform(new TransactionWork<Edge>() {
    @Override
    public Edge execute(final TransactionalGraph graph) throws Exception { 
        final TitanTransaction tx = (TitanTransaction) graph;
        final Vertex vertexCustomer = tx.addVertexWithLabel("Customer");
        Vertex vertexProviderCity = checkIfAleardyExistOrNot(tx, cityObject);
        if (vertexProviderCity == null) {
            vertexProviderCity = tx.addVertexWithLabel(prop.getProperty("ProvCity"));
            vertexProviderCity.setProperty("name", cityName);
        }
        tx.addEdge(vertexCustomer ,vertexProviderCity,"LIVES_IN_CITY");
        tx.commit();
        return null;
    }
}).build().exponentialBackoff(5, 200);

Daniel Kuppitz

unread,
Apr 16, 2015, 10:55:22 AM4/16/15
to aureliu...@googlegroups.com
The TransactionRetryHelper does that for you. Please be sure to actually understand what you're trying to do and how classes, that you're using, work under the hood.

Cheers,
Daniel


Reply all
Reply to author
Forward
0 new messages