Skip to content

Commit

Permalink
Add basic Gremlin examples and traversal examples on the Modern toy g…
Browse files Browse the repository at this point in the history
…raph for each GLV excluding Go
  • Loading branch information
ryn5 committed Sep 29, 2023
1 parent cc28ce7 commit 3ef30f5
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 111 deletions.
112 changes: 99 additions & 13 deletions gremlin-dotnet/example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Gremlin.Net.Driver;
using Gremlin.Net.Driver.Remote;
using Gremlin.Net.Structure.IO.GraphSON;
using Gremlin.Net.Process.Traversal;

// Common imports
using static Gremlin.Net.Process.Traversal.AnonymousTraversalSource;
Expand All @@ -16,21 +17,106 @@
using static Gremlin.Net.Process.Traversal.Direction;
using static Gremlin.Net.Process.Traversal.Cardinality;
using static Gremlin.Net.Process.Traversal.CardinalityValue;
using static Gremlin.Net.Process.Traversal.T;
using static Gremlin.Net.Process.Traversal.T;


ConnectionExample();
BasicGremlinExample();
ModernTraversalExample();

var server = new GremlinServer("localhost", 8182);
static void ConnectionExample()
{
var server = new GremlinServer("localhost", 8182);

// Connecting to the server
// using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
// Connecting to the server
// using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");

// Connecting to the server with customized configurations
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(
new GremlinServer(hostname:"localhost", port:8182, enableSsl:false, username:"", password:"")), "g");
// Connecting to the server with customized configurations
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(
new GremlinServer(hostname:"localhost", port:8182, enableSsl:false, username:"", password:"")), "g");

// Connecting and specifying a serializer
var client = new GremlinClient(server, new GraphSON3MessageSerializer());
// Specifying a serializer
var client = new GremlinClient(server, new GraphSON3MessageSerializer());

// Creating the graph traversal
var g = Traversal().WithRemote(remoteConnection);
g.AddV("test").Next();
Console.WriteLine(g);
// Creating the graph traversal
var g = Traversal().WithRemote(remoteConnection);
}

static void BasicGremlinExample()
{
var server = new GremlinServer("localhost", 8182);
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
var g = Traversal().WithRemote(remoteConnection);

// Basic Gremlin: adding and retrieving data
var v1 = g.AddV("person").Property("name", "marko").Next();
var v2 = g.AddV("person").Property("name", "stephen").Next();
var v3 = g.AddV("person").Property("name", "vadas").Next();

// Be sure to use a terminating step like next() or iterate() so that the traversal "executes"
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
g.V(v1).AddE("knows").To(v2).Property("weight", 0.75).Iterate();
g.V(v1).AddE("knows").To(v3).Property("weight", 0.75).Iterate();

// Retrieve the data from the "marko" vertex
var marko = g.V().Has("person","name","marko").ValueMap<object,object>().Next();
foreach (var kvp in marko)
{
if (kvp.Value is IEnumerable<object> enumerable)
{
Console.WriteLine($"{kvp.Key}: {enumerable.First()}");
}
}

// Find the "marko" vertex and then traverse to the people he "knows" and return their data
var peopleMarkoKnows = g.V().Has("person","name","marko").Out("knows").ValueMap<object,object>().ToList();
foreach (var person in peopleMarkoKnows)
{
if (person["name"] is IEnumerable<object> enumerable)
{
Console.WriteLine($"marko knows {enumerable.First()}");
}
}
}

static void ModernTraversalExample()
{
var server = new GremlinServer("localhost", 8182);
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(server), "g");
var g = Traversal().WithRemote(remoteConnection);

/*
This example requires the Modern toy graph to be preloaded upon launching the Gremlin server.
For details, see https://tinkerpop.apache.org/docs/current/reference/#gremlin-server-docker-image and use
conf/gremlin-server-modern.yaml.
*/
var e1 = g.V(1).BothE().ToList(); // (1)
var e2 = g.V(1).BothE().Where(OtherV().HasId(2)).ToList(); // (2)
var v1 = g.V(1).Next();
var v2 = g.V(2).Next();
var e3 = g.V(v1).BothE().Where(OtherV().Is(v2)).ToList(); // (3)
var e4 = g.V(v1).OutE().Where(InV().Is(v2)).ToList(); // (4)
var e5 = g.V(1).OutE().Where(InV().Has(T.Id, Within(2, 3))).ToList(); // (5)
var e6 = g.V(1).Out().Where(__.In().HasId(6)).ToList(); // (6)

Console.WriteLine(string.Join(", ", e1));
Console.WriteLine(string.Join(", ", e2));
Console.WriteLine(string.Join(", ", e3));
Console.WriteLine(string.Join(", ", e4));
Console.WriteLine(string.Join(", ", e5));
Console.WriteLine(string.Join(", ", e6));

/*
1. There are three edges from the vertex with the identifier of "1".
2. Filter those three edges using the where()-step using the identifier of the vertex returned by otherV() to
ensure it matches on the vertex of concern, which is the one with an identifier of "2".
3. Note that the same traversal will work if there are actual Vertex instances rather than just vertex
identifiers.
4. The vertex with identifier "1" has all outgoing edges, so it would also be acceptable to use the directional
steps of outE() and inV() since the schema allows it.
5. There is also no problem with filtering the terminating side of the traversal on multiple vertices, in this
case, vertices with identifiers "2" and "3".
6. There’s no reason why the same pattern of exclusion used for edges with where() can’t work for a vertex
between two vertices.
*/
}
4 changes: 2 additions & 2 deletions gremlin-dotnet/example/example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Gremlin.Net" Version="3.7.0" />
<ItemGroup>
<PackageReference Include="Gremlin.Net" Version="3.7.0" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions gremlin-dotnet/example/example.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example", "example.csproj", "{29ECA4F3-7C69-446D-8D00-4D396DE33C75}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{29ECA4F3-7C69-446D-8D00-4D396DE33C75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29ECA4F3-7C69-446D-8D00-4D396DE33C75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29ECA4F3-7C69-446D-8D00-4D396DE33C75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29ECA4F3-7C69-446D-8D00-4D396DE33C75}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FDC873E9-BBF5-41B1-BAE2-5AC9EBBC42DA}
EndGlobalSection
EndGlobal
109 changes: 89 additions & 20 deletions gremlin-driver/src/main/java/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,42 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

// Common imports as listed at reference/#gremlin-java-imports
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.IO;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
import static org.apache.tinkerpop.gremlin.process.traversal.Operator.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Order.*;
import static org.apache.tinkerpop.gremlin.process.traversal.P.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Pop.*;
import static org.apache.tinkerpop.gremlin.process.traversal.SackFunctions.*;
import static org.apache.tinkerpop.gremlin.process.traversal.Scope.*;
import static org.apache.tinkerpop.gremlin.process.traversal.TextP.*;
import static org.apache.tinkerpop.gremlin.structure.Column.*;
import static org.apache.tinkerpop.gremlin.structure.Direction.*;
import static org.apache.tinkerpop.gremlin.structure.T.*;
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;

import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.AbstractIoRegistry;
import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
import org.apache.tinkerpop.gremlin.structure.io.binary.TypeSerializerRegistry;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.util.MessageSerializer;
import org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Example {

public static void main(String[] args) throws Exception {
connectionExample();
basicGremlinExample();
modernTraversalExample();
}

public static void connectionExample() throws Exception {
// Creating an embedded graph
Graph graph = TinkerGraph.open();
GraphTraversalSource g = traversal().withEmbedded(graph);
Expand All @@ -59,32 +67,93 @@ public static void main(String[] args) throws Exception {
// Connecting and customizing configurations with a cluster
// See reference/#gremlin-java-configuration for full list of configurations
Cluster cluster = Cluster.build().
channelizer("org.apache.tinkerpop.gremlin.driver.Channelizer$HttpChannelizer").
keepAliveInterval(180000).
maxConnectionPoolSize(8).
path("/gremlin").
port(8192).
serializer(new GraphBinaryMessageSerializerV1()).
create();
channelizer("org.apache.tinkerpop.gremlin.driver.Channelizer$HttpChannelizer").
keepAliveInterval(180000).
maxConnectionPoolSize(8).
path("/gremlin").
port(8192).
serializer(new GraphBinaryMessageSerializerV1()).
create();
g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));

// Connecting and specifying a serializer
IoRegistry registry = new FakeIoRegistry(); // an IoRegistry instance exposed by a specific graph provider
TypeSerializerRegistry typeSerializerRegistry = TypeSerializerRegistry.build().addRegistry(registry).create();
MessageSerializer serializer = new GraphBinaryMessageSerializerV1(typeSerializerRegistry);
cluster = Cluster.build().
serializer(serializer).
create();
serializer(serializer).
create();
Client client = cluster.connect();
g = traversal().withRemote(DriverRemoteConnection.using(client, "g"));

// Cleanup
g.close();
}
public static class FakeIoRegistry extends AbstractIoRegistry {
}

private static void basicGremlinExample() {
Graph graph = TinkerGraph.open();
GraphTraversalSource g = traversal().withEmbedded(graph);

// Basic Gremlin: adding and retrieving data
Vertex v1 = g.addV("person").property("name","marko").next();
Vertex v2 = g.addV("person").property("name","stephen").next();
Vertex v3 = g.addV("person").property("name","vadas").next();

// Be sure to use a terminating step like next() or iterate() so that the traversal "executes"
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
g.V(v1).addE("knows").to(v2).property("weight",0.75).iterate();
g.V(v1).addE("knows").to(v3).property("weight",0.75).iterate();

System.out.println(g.V());
// Retrieve the data from the "marko" vertex
Map<Object,Object> marko = g.V().has("person","name","marko").valueMap().next();
System.out.println("name: " +((ArrayList) marko.get("name")).get(0));

// Find the "marko" vertex and then traverse to the people he "knows" and return their data
List<Map<Object,Object>> peopleMarkoKnows = g.V().has("person","name","marko").out("knows").valueMap().toList();
for (Map<Object, Object> map : peopleMarkoKnows) {
System.out.println("marko knows " + ((ArrayList) map.get("name")).get(0));
}
}

public static class FakeIoRegistry extends AbstractIoRegistry {
private static void modernTraversalExample() {
/*
This example requires the Modern toy graph to be preloaded upon launching the Gremlin server.
For details, see https://tinkerpop.apache.org/docs/current/reference/#gremlin-server-docker-image and use
conf/gremlin-server-modern.yaml.
*/
Graph modern = TinkerFactory.createModern();
GraphTraversalSource g = traversal().withEmbedded(modern);

List<Edge> e1 = g.V(1).bothE().toList(); // (1)
List<Edge> e2 = g.V(1).bothE().where(otherV().hasId(2)).toList(); // (2)
Vertex v1 = g.V(1).next();
Vertex v2 = g.V(2).next();
List<Edge> e3 = g.V(v1).bothE().where(otherV().is(v2)).toList(); // (3)
List<Edge> e4 = g.V(v1).outE().where(inV().is(v2)).toList(); // (4)
List<Edge> e5 = g.V(1).outE().where(inV().has(id, within(2,3))).toList(); // (5)
List<Vertex> e6 = g.V(1).out().where(in().hasId(6)).toList(); // (6)

System.out.println("1: " + e1.toString());
System.out.println("2: " + e2.toString());
System.out.println("3: " + e3.toString());
System.out.println("4: " + e4.toString());
System.out.println("5: " + e5.toString());
System.out.println("6: " + e6.toString());

/*
1. There are three edges from the vertex with the identifier of "1".
2. Filter those three edges using the where()-step using the identifier of the vertex returned by otherV() to
ensure it matches on the vertex of concern, which is the one with an identifier of "2".
3. Note that the same traversal will work if there are actual Vertex instances rather than just vertex
identifiers.
4. The vertex with identifier "1" has all outgoing edges, so it would also be acceptable to use the directional
steps of outE() and inV() since the schema allows it.
5. There is also no problem with filtering the terminating side of the traversal on multiple vertices, in this
case, vertices with identifiers "2" and "3".
6. There’s no reason why the same pattern of exclusion used for edges with where() can’t work for a vertex
between two vertices.
*/
}
}
3 changes: 3 additions & 0 deletions gremlin-javascript/example/conf/remote-graph.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
gremlin.remote.driver.clusterFile=/Users/ryant/Projects/tinkerpop/gremlin-server/conf/gremlin-server-modern.yaml
gremlin.remote.driver.sourceName=g
Loading

0 comments on commit 3ef30f5

Please sign in to comment.