.Net
Updated at:
This topic describes how to connect and use Graph Database (GDB) using the .NET programming environment.
Prerequisites
Make sure that your Graph Database instance and your Elastic Compute Service (ECS) virtual machine are in the same Virtual Private Cloud (VPC) network.
Preparations
- Install the .NET environment, version 2.0 or later. If you use a Windows development environment, use Windows 8 or later.
- Run the .NET command to generate the gremlinTest console project.
dotnet new console -o gremlinTest - Navigate to the project folder.
cd gremlinTest - Install the Gremlin.NET SDK dependency for your project.
dotnet add package gremlin.net - Edit the Program.cs file.Replace the contents of the file with the following code. Then, update the configuration with your GDB instance details:
- Set
${your-gdb-endpoint}to the domain name of your GDB instance. - Set
${username}to the username for your GDB instance. - Set
${password}to the password for your GDB instance.
using System; using System.Threading.Tasks; using System.Collections.Generic; using Gremlin.Net; using Gremlin.Net.Driver; namespace gremlinTest { class Program { private static string endpoint = "${your_gdb_endpoint}"; private static int port = 8182; private static string username = "${username}"; private static string password = "${password}"; static void Main(string[] args) { try { var gremlinServer = new GremlinServer(endpoint, port, username: username, password: password); var gremlinClient = new GremlinClient(gremlinServer); Program program = new Program(); program.RunQueryAsync(gremlinClient).Wait(); } catch (Exception e) { Console.WriteLine("{0}", e); } } private async Task RunQueryAsync(GremlinClient gremlinClient) { var vertices = await gremlinClient.SubmitWithSingleResultAsync<dynamic>("g.V().limit(1)"); Console.WriteLine("{0}", vertices); } } } - Set
Connect and execute
- Run the command running program. The following code provides an example on the returned result:
v[3a63cc90-d957-4324-9ffc-16a8e4c1c1f4] - The preceding example uses
g.V().limit(1)to traverse and return one vertex from GDB. To query other data, you can replace the Domain-Specific Language (DSL) query. - The following are other DSL examples. For more information about the vertex and edge structure of a graph, see http://tinkerpop.apache.org/docs/current/reference/#traversal.
- Delete vertices and edges of specified labels.
g.E().hasLabel('gdb_sample_knows').drop() g.E().hasLabel('gdb_sample_created').drop() g.V().hasLabel('gdb_sample_person').drop() g.V().hasLabel('gdb_sample_software').drop() - Add vertices and specify the IDs and the properties for the vertices.
g.addV('gdb_sample_person').property(id, 'gdb_sample_marko').property('age', 28).property('name', 'marko') g.addV('gdb_sample_person').property(id, 'gdb_sample_vadas').property('age', 27).property('name', 'vadas') g.addV('gdb_sample_person').property(id, 'gdb_sample_josh').property('age', 32).property('name', 'josh') g.addV('gdb_sample_person').property(id, 'gdb_sample_peter').property('age', 35).property('name', 'peter') g.addV('gdb_sample_software').property(id, 'gdb_sample_lop').property('lang', 'java').property('name', 'lop') g.addV('gdb_sample_software').property(id, 'gdb_sample_ripple').property('lang', 'java').property('name', 'ripple') - Modify or add the age property.
g.V('gdb_sample_marko').property('age', 29) - Establish relationships and specify the weight property.
g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_vadas')).property('weight', 0.5f) g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_josh')).property('weight', 1.0f) g.addE('gdb_sample_created').from(V('gdb_sample_marko')).to(V('gdb_sample_lop')).property('weight', 0.4f) g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_lop')).property('weight', 0.4f) g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_ripple')).property('weight', 1.0f) g.addE('gdb_sample_created').from(V('gdb_sample_peter')).to(V('gdb_sample_lop')).property('weight', 0.2f) - Query the number of vertices of all the vertices and the specified labels.
g.V().count() g.V().hasLabel('gdb_sample_person').count() - Query for vertices that meet specific conditions, such as people older than 29 or all people sorted by name in descending order.
g.V().hasLabel('gdb_sample_person').has('age', gt(29)) g.V().hasLabel('gdb_sample_person').order().by('name', decr) - Perform a traversal query, such as finding the people that Marko knows and the software created by those people.
g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person') g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person').outE('gdb_sample_created').inV().hasLabel('gdb_sample_software') - Delete relationships and the vertex.
g.V('gdb_sample_marko').outE('gdb_sample_knows').where(inV().has(id, 'gdb_sample_josh')).drop() g.V('gdb_sample_marko').drop()
For more information about how to use Gremlin.Net, see Gremlin.Net in the Apache TinkerPop 3 document.
Is this page helpful?