Skip to content

Commit

Permalink
Create GLV sample applications
Browse files Browse the repository at this point in the history
  • Loading branch information
ryn5 committed Oct 17, 2023
1 parent 4230c97 commit ee35361
Show file tree
Hide file tree
Showing 17 changed files with 891 additions and 364 deletions.
1 change: 1 addition & 0 deletions gremlin-dotnet/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Gremlin.Net.3.7.0/
132 changes: 132 additions & 0 deletions gremlin-dotnet/example/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

using Gremlin;
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;
using static Gremlin.Net.Process.Traversal.__;
using static Gremlin.Net.Process.Traversal.P;
using static Gremlin.Net.Process.Traversal.Order;
using static Gremlin.Net.Process.Traversal.Operator;
using static Gremlin.Net.Process.Traversal.Pop;
using static Gremlin.Net.Process.Traversal.Scope;
using static Gremlin.Net.Process.Traversal.TextP;
using static Gremlin.Net.Process.Traversal.Column;
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;


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

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 with customized configurations
using var remoteConnection = new DriverRemoteConnection(new GremlinClient(
new GremlinServer(hostname:"localhost", port:8182, enableSsl:false, username:"", password:"")), "g");

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

// 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").Values<string>("name").Next();
Console.WriteLine("name: " + marko);
// 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").Values<string>("name").ToList();
foreach (var person in peopleMarkoKnows)
{
Console.WriteLine("marko knows " + person);
}
}

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.
*/
}
14 changes: 14 additions & 0 deletions gremlin-dotnet/example/example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<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
9 changes: 9 additions & 0 deletions gremlin-driver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ limitations under the License.
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>example/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
157 changes: 157 additions & 0 deletions gremlin-driver/src/main/java/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package example;

// Common imports
import org.apache.tinkerpop.gremlin.driver.MessageSerializer;
import org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
import static org.apache.tinkerpop.gremlin.process.traversal.P.*;
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 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);

// Connecting to the server
g = traversal().withRemote(DriverRemoteConnection.using("localhost", 8182, "g"));

// Connecting to the server with a configurations file
g = traversal().withRemote("gremlin-driver/src/main/java/example/conf/remote-graph.properties");

// 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(8182).
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();
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();

// Retrieve the data from the "marko" vertex
Object marko = g.V().has("person","name","marko").values("name").next();
System.out.println("name: " + marko);

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

private static void modernTraversalExample() {
// Performs basic traversals on the Modern toy graph which can be created using TinkerFactory
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.
*/
}
}
18 changes: 18 additions & 0 deletions gremlin-driver/src/main/java/example/conf/remote-graph.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
gremlin.remote.driver.sourceName=g
Loading

0 comments on commit ee35361

Please sign in to comment.