4

I'm trying to change the output format of the Gremlin http server to a Non typed JSON object like the one below.

{
  "requestId": "320599c8-7d49-4cc6-87a6-42115009f90c",
  "status": {
    "message": "",
    "code": 200,
    "attributes": {}
  },
  "result": {
    "data": [
      {
        "info": [
          {
            "total_seen": [
              2845
            ],
            "first_seen": [
              1501568446000
            ],
            "last_seen": [
              1580157081000
            ],
            "value": [
              "http://test.com"
            ]
          }
        ]
      }
    ],
    "meta": {}
  }
}

But the response i get from the server is a typed one

{
  "requestId": "e8621470-7ee7-4f6b-a8ea-1d49c85093c8",
  "status": {
    "message": "",
    "code": 200,
    "attributes": {
      "@type": "g:Map",
      "@value": []
    }
  },
  "result": {
    "data": {
      "@type": "g:List",
      "@value": [
        {
          "@type": "g:Map",
          "@value": [
            "info",
            {
              "@type": "g:List",
              "@value": [
                {
                  "@type": "g:Map",
                  "@value": [
                    "total_seen",
                    {
                      "@type": "g:List",
                      "@value": [
                        {
                          "@type": "g:Int32",
                          "@value": 2797
                        }
                      ]
                    },
                    "first_seen",
                    {
                      "@type": "g:List",
                      "@value": [
                        {
                          "@type": "g:Int64",
                          "@value": 1501568446000
                        }
                      ]
                    },
                    "last_seen",
                    {
                      "@type": "g:List",
                      "@value": [
                        {
                          "@type": "g:Int64",
                          "@value": 1578774368000
                        }
                      ]
                    },
                    "vertex_type",
                    {
                      "@type": "g:List",
                      "@value": [
                        "url"
                      ]
                    },
                    "url_value",
                    {
                      "@type": "g:List",
                      "@value": [
                        "http://test.com"
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    },
    "meta": {
      "@type": "g:Map",
      "@value": []
    }
  }
}

I tried changing the settings in gremlin-server.yaml by commenting GraphSON V2 and V3

# Copyright 2019 JanusGraph Authors
#
# Licensed 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.

host: 127.0.0.1
port: 8182
scriptEvaluationTimeout: 30000
channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
graphs: {
  graph: conf/gremlin-server/janusgraph-cassandra-es-server.properties
}
scriptEngines: {
  gremlin-groovy: {
    plugins: { org.janusgraph.graphdb.tinkerpop.plugin.JanusGraphGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.server.jsr223.GremlinServerGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.tinkergraph.jsr223.TinkerGraphGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: {classImports: [java.lang.Math], methodImports: [java.lang.Math#*]},
               org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: [scripts/empty-sample.groovy]}}}}
serializers:
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}
#  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
  # Older serialization versions for backwards compatibility:
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0, config: {ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }}
#  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistryV1d0] }}
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistryV1d0] }}
processors:
  - { className: org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor, config: { sessionTimeout: 28800000 }}
  - { className: org.apache.tinkerpop.gremlin.server.op.traversal.TraversalOpProcessor, config: { cacheExpirationTime: 600000, cacheMaxSize: 1000 }}
metrics: {
  consoleReporter: {enabled: true, interval: 180000},
  csvReporter: {enabled: true, interval: 180000, fileName: /tmp/gremlin-server-metrics.csv},
  jmxReporter: {enabled: true},
  slf4jReporter: {enabled: true, interval: 180000},
  gangliaReporter: {enabled: false, interval: 180000, addressingMode: MULTICAST},
  graphiteReporter: {enabled: false, interval: 180000}}
maxInitialLineLength: 4096
maxHeaderSize: 8192
maxChunkSize: 8192
maxContentLength: 65536
maxAccumulationBufferComponents: 1024

Ii would be greatful if someone can tell me what i'm doing wrong here. Thanks.

alexgids
  • 396
  • 3
  • 11

1 Answers1

4

The server log output might be showing the error that points you to the problem but my guess is that you're trying to connect with a driver serializer configuration that isn't getting recognized by the server and it therefore defaults to GraphSON 3.0 serialization. That change happened a long time ago, way back on TINKERPOP-1565 for the 3.3.0 release.

When we started pushing GraphSON 3.0 we wanted it to be the default JSON based network serialization format, so we gave it the "application/json" mime type which formerly went to untyped GraphSON 1.0. So, you need a bit of a custom configuration to make the driver and server recognized what you want to have happen:

gremlin> cluster = Cluster.build().serializer(new GraphSONMessageSerializerV1d0()).create()
==>localhost/127.0.0.1:8182
gremlin> client = cluster.connect()
==>org.apache.tinkerpop.gremlin.driver.Client$ClusteredClient@277b8fa4
gremlin> x = client.submit("[1,2,3]").all().get()
==>result{object=1 class=java.lang.Integer}
==>result{object=2 class=java.lang.Integer}
==>result{object=3 class=java.lang.Integer}

You can see that you have to explicitly build the GraphSONMessageSerializerV1d0 (and in your case you would likely add the JanusGraphIoRegistry manually to that by using the constructor that takes a GraphSONMapper.

All that said, while GraphSON 1.0 isn't going anywhere it's curious that you would need to utilize it directly. Unless you have a really good reason to do so, I'd highly recommend that you stick to GraphSON 3.0. It has the widest support among graph providers is about as fast as Gryo these days for most serialization operations. If you're exclusively on the JVM, I'd even suggest trying GraphBinary rather than going back to GraphSON 1.0.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thanks for the answer @stephen . Even if were to use GraphSON 3.0 how can i get 'non typed' JOSN respone in the Gremlin http server. Just curious if there are any settings in the gremlin-server.yaml file. – alexgids Feb 11 '20 at 08:23
  • 1
    GraphSON 3.0 is typed and there is no option to use it any other way. We typically don't really want users working directly with GraphSON results and would prefer them working with serialized objects in the programming language of their choice. The serialization mechanism should be transparent. – stephen mallette Feb 11 '20 at 11:03
  • 1
    I guess if you need raw JSON then you could do one of the following (1) send a script and output your data as a JSON string in the form of your choosing though we'd prefer you not use scripts so much - prefer bytecode requests or (2) write your own `MessageSerializer` implementation that does something special to your results and configure that into your Gremlin Server. in regards to (1) Gremlin bytecode requests will probably be able to do this pretty soon should this feature get implemented https://issues.apache.org/jira/browse/TINKERPOP-2334 – stephen mallette Feb 11 '20 at 11:05
  • Thanks a lot. FInally we decided we will use GraphSON 3.0 going forward and we are planning on writing a parser to parse it our needs. – alexgids Feb 12 '20 at 06:43