Java connection example

299 views
Skip to first unread message

Philipp Kraus

unread,
Oct 1, 2020, 7:03:41 AM10/1/20
to Gremlin-users
Hello,

I'm trying to use Gremlin (Maven package 3.4.8) with Janusgraph (Maven package 0.5.2) in Java 1.8. I would like to connect from my Java program to my remote Janusgraph / Gremlin instance. I found examples with a configuration object, but on the release notes I see I should use AnonymousTraversalSource.traversal()
Im using Janusgraph with Gremlin in a docker container and an Apache Cassandra and ElasticSearch in two other containers. On my Go & JavaScript programms I'm using the URL ws://janusgraph:8182/gremlin to connect to my server. 

Can you show me an current Java example how I can crate a remote connection to my server? My goal is later that I have got a Gremlin, Janusgraph, ElasticSearch and Cassandra cluster.

Thanks a lot

Stephen Mallette

unread,
Oct 1, 2020, 9:01:49 AM10/1/20
to gremli...@googlegroups.com
The old method of connecting should still work without issue, but as you have read, the preferred method is to use AnonymoustTraversalSource.traversal() and then bind that to an embedded graph or to a remote instance. There are plenty of examples of how to use it:

Java Example

Java and all TinkerPop managed GLVs Examples

was something in those examples not clear?







--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/cb8cd181-d5c9-4034-ade3-1f9e585cca3en%40googlegroups.com.

Philipp Kraus

unread,
Oct 1, 2020, 2:31:59 PM10/1/20
to Gremlin-users
Thanks a lot, this seems to help, I will test it.
But one question, can I set the settings of the YAML file manually via String / Configuration object? I don't find any source code / explanation for that

Stephen Mallette

unread,
Oct 1, 2020, 2:46:01 PM10/1/20
to gremli...@googlegroups.com
We don't provide examples for every single variation of using() - you can find a full list in the javadocs:


One of them allows you to reference a path to a configuration file. Another allows you to submit an existing Cluster object. Another allows a Configuration object. You have many options.



Philipp Kraus

unread,
Oct 2, 2020, 3:22:01 AM10/2/20
to Gremlin-users
Thanks, yes it try to start with the Cluster object but this is a little bit hard to understand at the beginning

Philipp Kraus

unread,
Oct 2, 2020, 8:12:46 AM10/2/20
to Gremlin-users
I try with this YAML configuration and the given code to establish a connection to my Janusgraph instance (which is run at the jost janusgraph and gremlin listen unde rthe default port 8182) []I'm using a docker compose and I'm running my Java program inside my docker container, I can ping and get access to janusgraph:8182):

---

hosts: janusgraph
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}
gremlin.remote.remoteConnectionClass: org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}

----

try
{
m_traversal = AnonymousTraversalSource.traversal()
.withRemote(
p_config.<String>get( "config" )
.orElseThrow( () -> new CConfigurationNotFound( "configuration file is not set" ) )
);
}
catch ( final Exception l_exception )
{
throw new RuntimeException( l_exception );
}

--- 

I get the exception:

java.lang.IllegalStateException: org.apache.tinkerpop.gremlin.process.remote.RemoteConnectionException: java.lang.RuntimeException: java.lang.RuntimeException: java.util.concurrent.TimeoutException: Timed out while waiting for an available host - check the client configuration and connectivity to the server if this message persists

but the host is reachable without any problems. What I'm doing wrong?

Thanks a lot for help

Stephen Mallette

unread,
Oct 2, 2020, 8:40:37 AM10/2/20
to gremli...@googlegroups.com
I don't know if I follow your code fragment completely so I'll just offer the following....If you have a YAML file, let's say it's called conf.yaml, that looks like this:

hosts: janusgraph
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}
config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}  

then:

Cluster cluster = Cluster.open("conf.yaml");
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));

I'd wonder a bit about your YAML file as the "hosts" value looks suspicious to me. Is that the actual name of the host? perhaps that should be localhost or an IP address or the like? I don't know much about JanusGraph/Docker Compose usage so I'm not sure if there are other configurations related to that which are getting in your way. 



Philipp Kraus

unread,
Oct 2, 2020, 9:40:02 AM10/2/20
to Gremlin-users
Thanks, you are right, the host entry was wrong, I don't us IP addresses I'm just using hostnames, so yes, the name is correct, I have fixed my YAML with:

hosts: [janusgraph]
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}


I just try in a first test-case this code

AnonymousTraversalSource.traversal().withRemote( DriverRemoteConnection.using( Cluster.open( "myconfig.yaml" ) ) ).V().toList();

but I get as result an empty graph [GraphStep(vertex,[])] and .V().toList() throws an exception:

java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.tinkerpop.gremlin.process.remote.traversal.RemoteTraverser

But my graph database is not empty, so the vertex list should not be empty
As data types I'm using UUID, Date, String, Integer, Long, Double, Boolean, Geoshape and byte[], did I need to specify for any of these types a serializer?

Thanks

Stephen Mallette

unread,
Oct 2, 2020, 10:06:45 AM10/2/20
to gremli...@googlegroups.com
not sure if this is the problem but i see you're using the toString() serialization option:

  serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}  

get rid of that "config" key

Philipp Kraus

unread,
Oct 4, 2020, 8:10:44 AM10/4/20
to Gremlin-users
Thanks for this hint, I have replace this item to "false", but if I would like to get access to a property of a vertex I get an exception:

java.lang.IllegalStateException: The property does not exist as the key has no associated value for the provided element: v[8400]:uuid

I'm using this call: UUID.fromString( m_vertex.value( "uuid" ) )
m_vertex is an object of org.apache.tinkerpop.gremlin.structure.Vertex
and the vertex contains this proeprty, so I don't understand because why I cannot get this property

Stephen Mallette

unread,
Oct 4, 2020, 8:27:05 AM10/4/20
to gremli...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages