Search code examples
gremlinjanusgraphgremlinpython

How to query janusgraph in python


I'd like to query janusgraphin python. I follow the instruction here, but didn't success. What might be wrong ? Thanks

Here's my code:

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

from gremlin_python.process.anonymous_traversal import traversal

connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
# The connection should be closed on shut down to close open connections with connection.close()
g = traversal().withRemote(connection)

g.V().count()

The result is weird to me, even after I add some nodes.

>>> g.V().count()
[['V'], ['count']]

to_list works on count(),but doesn't work on vertex.

>>> g.V().has_label("table").count().to_list()
Out[11]: [582]
>>> g.V().to_list()
Traceback (most recent call last):
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-59b774c853b9>", line 1, in <module>
    g.V().to_list()
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/process/traversal.py", line 65, in to_list
    return list(iter(self))
           ^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/process/traversal.py", line 48, in __next__
    self.traversal_strategies.apply_strategies(self)
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/process/traversal.py", line 701, in apply_strategies
    traversal_strategy.apply(traversal)
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/driver/remote_connection.py", line 78, in apply
    remote_traversal = self.remote_connection.submit(traversal.bytecode)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/driver/driver_remote_connection.py", line 107, in submit
    results = result_set.all().result()
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/driver/resultset.py", line 90, in cb
    f.result()
  File "/opt/anaconda3/lib/python3.11/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/opt/anaconda3/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/driver/connection.py", line 90, in _receive
    status_code = self._protocol.data_received(data, self._results)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/driver/protocol.py", line 98, in data_received
    message = self._message_serializer.deserialize_message(message)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/driver/serializer.py", line 283, in deserialize_message
    result = self._graphbinary_reader.to_object(b)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 204, in to_object
    return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 503, in objectify
    return cls.is_null(buff, reader, cls._read_list, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 232, in is_null
    return None if nullable and buff.read(1)[0] == 0x01 else else_opt(buff, reader)
                                                             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 510, in _read_list
    the_list.append(r.read_object(b))
                    ^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 195, in read_object
    return self.to_object(b)
           ^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 204, in to_object
    return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 965, in objectify
    return cls.is_null(buff, reader, cls._read_traverser, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 232, in is_null
    return None if nullable and buff.read(1)[0] == 0x01 else else_opt(buff, reader)
                                                             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 970, in _read_traverser
    obj = r.read_object(b)
          ^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 195, in read_object
    return self.to_object(b)
           ^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 204, in to_object
    return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 683, in objectify
    return cls.is_null(buff, reader, cls._read_vertex, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 232, in is_null
    return None if nullable and buff.read(1)[0] == 0x01 else else_opt(buff, reader)
                                                             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 689, in _read_vertex
    props = r.read_object(b)
            ^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 195, in read_object
    return self.to_object(b)
           ^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 204, in to_object
    return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 503, in objectify
    return cls.is_null(buff, reader, cls._read_list, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 232, in is_null
    return None if nullable and buff.read(1)[0] == 0x01 else else_opt(buff, reader)
                                                             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 510, in _read_list
    the_list.append(r.read_object(b))
                    ^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 195, in read_object
    return self.to_object(b)
           ^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 204, in to_object
    return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 713, in objectify
    return cls.is_null(buff, reader, cls._read_vertexproperty, nullable)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 232, in is_null
    return None if nullable and buff.read(1)[0] == 0x01 else else_opt(buff, reader)
                                                             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 717, in _read_vertexproperty
    vp = VertexProperty(r.read_object(b), r.to_object(b, DataType.string, False), r.read_object(b), None)
                        ^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 195, in read_object
    return self.to_object(b)
           ^^^^^^^^^^^^^^^^^
  File "/Users/jianfezhang/github/metadata-repo/metadata-pilot/.venv/lib/python3.11/site-packages/gremlin_python/structure/io/graphbinaryV1.py", line 204, in to_object
    return self.deserializers[DataType(bt)].objectify(buff, self, nullable)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: <DataType.custom: 0>

Solution

  • See the TinkerPop documentation:

    Queries in gremin-python need to be ended with a to_list() step. Different than the groovy gremlin repl, the standard python repl does not do this automatically.

    Without the to_list() step, the bytecode of the query is returned, but not executed.