Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TINKERPOP-2995 Create Sample Applications in each GLV #2298

Merged
merged 54 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
c34b5c8
Create GLV sample applications
ryn5 Sep 25, 2023
2e2bbfa
Remove redundant Java imports
ryn5 Oct 17, 2023
4fee2e1
Update license
ryn5 Oct 18, 2023
5500124
Add async/await examples to .net
ryn5 Oct 18, 2023
598ce6b
Delete .gitignore in dotnet
ryn5 Oct 18, 2023
4e2261f
Declare each connection separately and comment extras
ryn5 Oct 19, 2023
3b2b3c2
Split examples into separate files
ryn5 Oct 27, 2023
59c2abc
Add license
ryn5 Oct 30, 2023
5450f33
Rename JS files
ryn5 Oct 30, 2023
fe42586
Add simple query and prints to all connections
ryn5 Oct 30, 2023
1025df2
Edit function names to follow convention
ryn5 Oct 30, 2023
f3dfb9b
Minor fixes
ryn5 Nov 6, 2023
bb6bdf5
Minor edits
ryn5 Nov 6, 2023
b644876
Improve connections example outputs
ryn5 Nov 23, 2023
937d1fb
Remove configs file connection example
ryn5 Nov 23, 2023
1ddcb26
Bash script to run examples
ryn5 Nov 23, 2023
64ad25b
Create pom.xml
ryn5 Nov 23, 2023
6e6a8fa
Add cleanup comment
ryn5 Nov 23, 2023
a34dbc6
Fix hanging connections issue by closing clusters
ryn5 Nov 24, 2023
c1d9ae4
Update script
ryn5 Nov 24, 2023
f76a3c5
Update versioning
ryn5 Nov 24, 2023
e9e5f9d
Create GLV sample applications
ryn5 Sep 25, 2023
4eba0dd
Remove redundant Java imports
ryn5 Oct 17, 2023
4d4a3a0
Update license
ryn5 Oct 18, 2023
6062e92
Add async/await examples to .net
ryn5 Oct 18, 2023
1941603
Delete .gitignore in dotnet
ryn5 Oct 18, 2023
fe8c8e3
Declare each connection separately and comment extras
ryn5 Oct 19, 2023
94a356e
Split examples into separate files
ryn5 Oct 27, 2023
5251284
Add license
ryn5 Oct 30, 2023
743370b
Rename JS files
ryn5 Oct 30, 2023
60b0b80
Add simple query and prints to all connections
ryn5 Oct 30, 2023
8020ec5
Edit function names to follow convention
ryn5 Oct 30, 2023
da5ba95
Minor fixes
ryn5 Nov 6, 2023
ffbb35b
Minor edits
ryn5 Nov 6, 2023
72df20c
Improve connections example outputs
ryn5 Nov 23, 2023
3f5780f
Remove configs file connection example
ryn5 Nov 23, 2023
e8fc7c9
Bash script to run examples
ryn5 Nov 23, 2023
fe1cc20
Create pom.xml
ryn5 Nov 23, 2023
311a221
Add cleanup comment
ryn5 Nov 23, 2023
8b408d8
Fix hanging connections issue by closing clusters
ryn5 Nov 24, 2023
5599e3d
Update script
ryn5 Nov 24, 2023
78caf91
Update versioning
ryn5 Nov 24, 2023
e1adeb1
Merge branch 'ryan/glv-examples' of https://github.com/Bit-Quill/tink…
ryn5 Nov 24, 2023
fab1645
Update versioning
ryn5 Nov 24, 2023
052029a
Remove image version, add warning
ryn5 Nov 28, 2023
0d67396
Update release notes
ryn5 Nov 28, 2023
cd05624
Write docs for examples
ryn5 Nov 28, 2023
eae6b75
Move script to bin
ryn5 Nov 30, 2023
d7e30f3
Stop generating reduced pom
ryn5 Dec 5, 2023
ff2f6fd
Add exit trap to close containers
ryn5 Dec 5, 2023
37855bf
Delete dependency-reduced-pom.xml
ryn5 Dec 5, 2023
ac792a3
Update run command
ryn5 Dec 6, 2023
0a47f61
Add licenses
ryn5 Dec 13, 2023
d0d65e5
Update pom versioning
ryn5 Dec 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions gremlin-dotnet/example/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
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();
FlorianHockmann marked this conversation as resolved.
Show resolved Hide resolved

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());
// using var remoteConnection = new DriverRemoteConnection(client, "g");

// Creating the graph traversal
var g = Traversal().WithRemote(remoteConnection);
}

static async 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 = await g.V().Has("person","name","marko").Values<string>("name").Promise(t => t.Next());
Console.WriteLine("name: " + marko);

// Find the "marko" vertex and then traverse to the people he "knows" and return their data
var peopleMarkoKnows = await g.V().Has("person","name","marko").Out("knows").Values<string>("name").Promise(t => t.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)
FlorianHockmann marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -260,6 +260,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.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 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();
ryn5 marked this conversation as resolved.
Show resolved Hide resolved
basicGremlinExample();
modernTraversalExample();
}

public static void connectionExample() throws Exception {
ryn5 marked this conversation as resolved.
Show resolved Hide resolved
// Creating an embedded graph
// Graph graph = TinkerGraph.open();
// GraphTraversalSource g = traversal().withEmbedded(graph);

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

// Connecting to the server with a configurations file
// GraphTraversalSource 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();
// GraphTraversalSource 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 = Cluster.build().
// serializer(serializer).
// create();
// Client client = cluster.connect();
// GraphTraversalSource 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
Loading