Gremlin python
This mini-tutorial is inspired by this stackoverflow question
The goal is to get access to an apache tinkerpop/gremlin graph database via Python.
Prerequisites
- Java
- Python
- Gremlin-Server
- Gremlin-Console (for debugging)
Installing Java
sudo apt-get install openjdk-8-jre
java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
Installing Python and Pip
sudo apt install python2.7
python --version
Python 2.7.15+
sudo apt install python-pip
pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Installing Gremlin Server and Console
Trial script
#!/bin/bash
# WF 2019-09-17
# test gremlin-python
# see https://stackoverflow.com/questions/57936915/how-do-i-get-gremlin-python-with-gremlin-server-3-4-3-to-work
version=3.4.3
mirror=http://mirror.dkd.de/apache/tinkerpop/$version
gsd=apache-tinkerpop-gremlin-server-${version}
gcd=apache-tinkerpop-gremlin-console-${version}
# check that the given program is installed
checkinstalled() {
local l_prg="$1"
local l_version="$2"
local l_package="$3"
which $l_prg
if [ $? -ne 0 ]
then
echo "$l_prg is not available - shall i install it from $l_package y/n?"
read x
case $x in
y) sudo apt-get install $l_package;;
*) echo "aborting ..."
exit 1;;
esac
fi
$l_prg $l_version
}
install() {
echo "checking prerequisites ..."
checkinstalled java "-version" "openjdk-8-jre"
checkinstalled python "--version" "python2.7"
checkinstalled pip "--version" "python-pip"
for d in $gsd $gcd
do
if [ ! -d $d ]
then
zip=$d-bin.zip
if [ ! -f $zip ]
then
echo "downloading $zip"
curl -s $mirror/$zip -o $zip
else
echo "$zip already downloaded"
fi
echo "unzipping $zip"
unzip $zip
else
echo "$d already unzipped"
fi
done
}
# commandline option
while [ "$1" != "" ]
do
option=$1
shift
case $option in
-i|--install)
install;;
-s|--server)
echo "starting gremlin-server ..."
#conf=$(realpath $gsd/conf/gremlin-server-modern-py.yaml)
conf=$(realpath $gsd/conf/gremlin-server-modern.yaml)
$gsd/bin/gremlin-server.sh $conf
;;
-c|--console)
echo "starting gremlin-console ..."
$gcd/bin/gremlin.sh
;;
-h|--help)
usage;;
*)
usage;;
esac
done
Trial result
# install needed parts
./run -i
checking prerequisites ...
/usr/bin/java
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
/usr/bin/python
Python 2.7.5
/opt/local/bin/pip
pip 19.2.3 from /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
apache-tinkerpop-gremlin-server-3.4.3 already unzipped
apache-tinkerpop-gremlin-console-3.4.3 already unzipped
# run gremlin server
./run -s
starting gremlin-server ...
[INFO] GremlinServer - 3.4.3
\,,,/
(o o)
-----oOOo-(3)-oOOo-----
[INFO] GremlinServer - Configuring Gremlin Server from /Users/wf/source/python/gremlintest/apache-tinkerpop-gremlin-server-3.4.3/conf/gremlin-server-modern.yaml
[INFO] MetricManager - Configured Metrics Slf4jReporter configured with interval=180000ms and loggerName=org.apache.tinkerpop.gremlin.server.Settings$Slf4jReporterMetrics
[INFO] DefaultGraphManager - Graph [graph] was successfully configured via [conf/tinkergraph-empty.properties].
[INFO] ServerGremlinExecutor - Initialized Gremlin thread pool. Threads in pool named with pattern gremlin-*
[INFO] ServerGremlinExecutor - Initialized GremlinExecutor and preparing GremlinScriptEngines instances.
[INFO] ServerGremlinExecutor - Initialized gremlin-groovy GremlinScriptEngine and registered metrics
[INFO] ServerGremlinExecutor - A GraphTraversalSource is now bound to [g] with graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
[INFO] OpLoader - Adding the standard OpProcessor.
[INFO] OpLoader - Adding the session OpProcessor.
[INFO] OpLoader - Adding the traversal OpProcessor.
[INFO] TraversalOpProcessor - Initialized cache for TraversalOpProcessor with size 1000 and expiration time of 600000 ms
[INFO] GremlinServer - Executing start up LifeCycleHook
[INFO] Logger$info - Loading 'modern' graph data.
[INFO] GremlinServer - idleConnectionTimeout was set to 0 which resolves to 0 seconds when configuring this value - this feature will be disabled
[INFO] GremlinServer - keepAliveInterval was set to 0 which resolves to 0 seconds when configuring this value - this feature will be disabled
[WARN] AbstractChannelizer - The org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0 serialization class is deprecated.
[INFO] AbstractChannelizer - Configured application/vnd.gremlin-v3.0+gryo with org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0
[WARN] AbstractChannelizer - The org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0 serialization class is deprecated.
[INFO] AbstractChannelizer - Configured application/vnd.gremlin-v3.0+gryo-stringd with org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0
[INFO] AbstractChannelizer - Configured application/vnd.gremlin-v3.0+json with org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0
[INFO] AbstractChannelizer - Configured application/json with org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0
[INFO] AbstractChannelizer - Configured application/vnd.graphbinary-v1.0 with org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1
[INFO] AbstractChannelizer - Configured application/vnd.graphbinary-v1.0-stringd with org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1
[INFO] GremlinServer$1 - Gremlin Server configured with worker thread pool of 1, gremlin pool of 2 and boss thread pool of 1.
[INFO] GremlinServer$1 - Channel started at port 8182.
# and in an other console
./run -c
starting gremlin-console ...
\,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> g=traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
No signature of method: groovysh_evaluate.DriverRemoteConnection() is applicable for argument types: (String, String) values: [ws://localhost:8182/gremlin, g]
Type ':help' or ':h' for help.