question

scano_183208 avatar image
scano_183208 asked Erick Ramirez edited

How do I add an edge between 2 vertices in C#?

I am having trouble adding an edge between two vertices using c# below is my code assuming that both vertices exist:

var fromStatement = @$"g.V().has('tenant_id', '{uom.TenantId}').has('layer', '{_inventoryLayer}').has('uom_id', '{uom.UomId}').as('from')";
var toStatement = $@"g.V().has('tenant_id', '{uom.TenantId}').has('layer', '{_inventoryLayer}').has('uom_id', '{uom.MeasureBy}').as('to')";
var from = await _inventoryGraphContext.DseSession.ExecuteGraphAsync(new SimpleGraphStatement(fromStatement));
var to = await _inventoryGraphContext.DseSession.ExecuteGraphAsync(new SimpleGraphStatement(toStatement));
if (to != null && from !=null)  
{                               
    var addEdgeFromTo = @$"{from}.addEdge('is_split_into', '{to}', 'quantity', '{uom.ConversionFactor}')";
    await _inventoryGraphContext.DseSession.ExecuteGraphAsync(new SimpleGraphStatement(addEdgeFromTo));
    var addEdgeToFrom = $@"{to}.addEdge('is_part_of', '{from}')";
    await _inventoryGraphContext.DseSession.ExecuteGraphAsync(new SimpleGraphStatement(addEdgeToFrom));
}

(addingEdge.txt)

I keep getting the following error:

Cassandra.InvalidQueryException
HResult=0x80131500
Message=No such property: Cassandra for class: Script31
Source=System.Private.CoreLib
StackTrace:

I am using Dse 6.7.7 and CassandraCSharp Driver 3.15

dsegraphcsharp driver
addingedge.txt (1005 B)
4 comments
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

bonian.hu_177317 avatar image bonian.hu_177317 commented ·

The example that we use to add the edge similar to the example below. (C# driver 3.14.0, Graph extension 2.1.0)

using Cassandra;
using Cassandra.DataStax.Graph;
using Gremlin.Net.Process.Traversal;

...
IGraphStatement insertStatemnet = new SimpleGraphStatement( "josh.addEdge('created', ripple, 'weight', 1.0f);");
session.ExecuteGraphAsync(insertStatemnet);

But the following syntax of adding the edge does not look familiar.

var addEdgeFromTo = @$"{from}.addEdge('is_split_into', '{to}', 'quantity', '{uom.ConversionFactor}')";
_inventoryGraphContext.DseSession.ExecuteGraphAsync(new SimpleGraphStatement(addEdgeFromTo));

Would you be able to let me know your DSE version, C# driver version?

Also, a simple graph example would be idea so we can quickly create a test graph.

Thank you!

0 Likes 0 ·
scano_183208 avatar image scano_183208 bonian.hu_177317 commented ·

@bonian.hu_177317

Hi! thank you for the reply. I want to steer away from using Gremlin.Net as Datastax no longer supports it. I am using Dse 6.7.7 and CassandraCSharp Driver 3.15

0 Likes 0 ·
bonian.hu_177317 avatar image bonian.hu_177317 scano_183208 commented ·

Hi Scano, apologize that the Gremlin.Net import statement might confused you. It is not required in this case instead of specifically used for works for fluent API.

You can use what Bettina gave below as a reference.

0 Likes 0 ·
Show more comments

1 Answer

bettina.swynnerton avatar image
bettina.swynnerton answered

Hi,

I am no C# expert, but you can add an edge by constructing your SimpleGraphStatement using the addE() method from the traversal API:

here is an example, adding an edge of label "know" with a property "weight" between two vertices of label "person":

var fromVertexStatement =  "g.V().has('person', 'person_id', 'person1')";

var toVertexStatement = "g.V().has('person', 'person_id', 'person3')";

var addEdge = $"{fromVertexStatement}.addE('knows').property('weight', 1.0).to({toVertexStatement})";

session.ExecuteGraph(new SimpleGraphStatement(addEdge));

For reference about the method:

https://docs.datastax.com/en/dse/6.7/dse-dev/datastax_enterprise/graph/reference/traversal/refTravAddE.html

Hope this helps!


Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.