Gremlin python

From BITPlan Wiki
Jump to navigation Jump to search

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

  1. Java
  2. Python
  3. Gremlin-Server
  4. 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

./run -i
./run -s
# 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.