incubator-hugegraph icon indicating copy to clipboard operation
incubator-hugegraph copied to clipboard

[Question] How to connect HugeGraph-Server with gremlin-java-client?

Open zhengyingying opened this issue 5 years ago • 5 comments

Environment ( 环境信息 - 必填 )

  • Server Version: v0.11.2 (refer here)
  • Backend: memory, 1 node, HDD
  • OS: 2 CPUs, 40 G RAM, Centos 7.7

Your Question ( 问题描述 )

希望通过Java代码远程访问Huge Graph Server,但是按照[https://github.com/hugegraph/hugegraph/issues/345]的方法无法实现。

本地hugegraph.yaml文件内容如下:

     hosts: [192.168.1.101]
     port: 8188
     serializer: {
        className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV2d0,
        config: {
            serializeResultToString: false,
            ioRegistries: [ com.baidu.hugegraph.io.HugeGraphIoRegistry ]
         }
      }

远程【192.168.1.101】gremlin-server.yaml文件内容如下:

    host: 127.0.0.1
    port: 8188

    scriptEvaluationTimeout: 30000
    channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
    graphs: {
      hugegraph: conf/hugegraph.properties
    }
    scriptEngines: {
      gremlin-groovy: {
        plugins: {
          com.baidu.hugegraph.plugin.HugeGraphGremlinPlugin: {},
          org.apache.tinkerpop.gremlin.server.jsr223.GremlinServerGremlinPlugin: {},
          org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: {
            classImports: [
              java.lang.Math,
              com.baidu.hugegraph.backend.id.IdGenerator,
              com.baidu.hugegraph.type.define.Directions,
              com.baidu.hugegraph.type.define.NodeRole,
              com.baidu.hugegraph.traversal.algorithm.CustomizePathsTraverser,
              com.baidu.hugegraph.traversal.algorithm.CustomizedCrosspointsTraverser,
              com.baidu.hugegraph.traversal.algorithm.FusiformSimilarityTraverser,
              com.baidu.hugegraph.traversal.algorithm.HugeTraverser,
              com.baidu.hugegraph.traversal.algorithm.NeighborRankTraverser,
              com.baidu.hugegraph.traversal.algorithm.PathsTraverser,
              com.baidu.hugegraph.traversal.algorithm.PersonalRankTraverser,
              com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser,
              com.baidu.hugegraph.traversal.algorithm.SubGraphTraverser,
              com.baidu.hugegraph.traversal.optimize.Text,
              com.baidu.hugegraph.traversal.optimize.TraversalUtil,
              com.baidu.hugegraph.util.DateUtil
            ],
            methodImports: [java.lang.Math#*]
          },
          org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {
            files: [scripts/empty-sample.groovy]
          }
        }
      }
    }
    serializers:
      - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1,
          config: {
            serializeResultToString: false,
            ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
          }
      }
      - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0,
          config: {
            serializeResultToString: false,
            ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
          }
      }
      - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV2d0,
          config: {
            serializeResultToString: false,
            ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
          }
      }
      - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0,
          config: {
            serializeResultToString: false,
            ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
          }
      }
    metrics: {
      consoleReporter: {enabled: false, interval: 180000},
      csvReporter: {enabled: false, interval: 180000, fileName: ./metrics/gremlin-server-metrics.csv},
      jmxReporter: {enabled: false},
      slf4jReporter: {enabled: false, interval: 180000},
      gangliaReporter: {enabled: false, interval: 180000, addressingMode: MULTICAST},
      graphiteReporter: {enabled: false, interval: 180000}
    }
    maxInitialLineLength: 4096
    maxHeaderSize: 8192
    maxChunkSize: 8192
    maxContentLength: 65536
    maxAccumulationBufferComponents: 1024
    resultIterationBatchSize: 64
    writeBufferLowWaterMark: 32768
    writeBufferHighWaterMark: 65536
     ssl: {
      enabled: false
    }

事先在远端【192.168.1.101】HugeGraph数据库中,使用console写入数据: gremlin> hugegraph.traversal().V() ==>[id:1:josh,label:person,type:vertex,properties:[name:josh,age:32,city:Beijing]] ==>[id:2:ripple,label:software,type:vertex,properties:[name:ripple,lang:java,price:199]] ==>[id:1:peter,label:person,type:vertex,properties:[name:peter,age:35,city:Shanghai]] ==>[id:1:vadas,label:person,type:vertex,properties:[name:vadas,age:27,city:Hongkong]] ==>[id:2:lop,label:software,type:vertex,properties:[name:lop,lang:java,price:328]] ==>[id:1:marko,label:person,type:vertex,properties:[name:marko,age:29,city:Beijing]]

本地Java测试代码如下:

            cluster = Cluster.open("conf/hugegraph.yaml");
            client = cluster.connect();
            
            try {
                ResultSet results = client.submit("hugegraph.traversal().V()");
                results.stream().forEach(result -> {
                    System.out.println(result.getVertex());
                });

                results = client.submit("hugegraph.traversal().E()");
                results.stream().forEach(result -> {
                    System.out.println(result.getEdge());
                });
            } finally {
                client.close();
                cluster.close();
            }

报错信息如下:

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/baidu/hugegraph/io/HugeGraphSONModule.registerCommonSerializers(Lorg/apache/tinkerpop/shaded/jackson/databind/module/SimpleModule;)V @58: invokespecial
  Reason:
    Type 'com/baidu/hugegraph/date/SafeDateFormat' (current frame, stack[5]) is not assignable to 'java/text/DateFormat'
  Current Frame:
    bci: @58
    flags: { }
    locals: { 'org/apache/tinkerpop/shaded/jackson/databind/module/SimpleModule', integer }
    stack: { 'org/apache/tinkerpop/shaded/jackson/databind/module/SimpleModule', 'java/lang/Class', uninitialized 47, uninitialized 47, 'java/lang/Boolean', 'com/baidu/hugegraph/date/SafeDateFormat' }
  Bytecode:
0x0000000: 2a12 0fbb 0010 59b7 0011 b600 1257 2a12
0x0000010: 13bb 0014 59b7 0015 b600 1257 2a12 16bb
0x0000020: 0017 59b7 0018 b600 1257 033c 2a12 19bb
0x0000030: 001a 591b b800 1bb2 001c b700 1db6 0012
0x0000040: 572a 1219 bb00 1e59 bb00 1e59 b700 1fb2
0x0000050: 001c 1220 b700 21b6 0022 572a 1223 bb00
0x0000060: 2459 b700 25b6 0012 572a 1223 bb00 2659
0x0000070: b700 27b6 0022 572a 1228 bb00 2959 b700
0x0000080: 2ab6 0012 572a 1228 bb00 2b59 b700 2cb6
0x0000090: 0022 57b1                              

at com.baidu.hugegraph.io.HugeGraphIoRegistry.<init>(HugeGraphIoRegistry.java:36)
at com.baidu.hugegraph.io.HugeGraphIoRegistry.<clinit>(HugeGraphIoRegistry.java:27)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.apache.tinkerpop.gremlin.driver.ser.AbstractMessageSerializer.lambda$addIoRegistries$0(AbstractMessageSerializer.java:50)
at org.apache.tinkerpop.gremlin.driver.ser.AbstractMessageSerializer$$Lambda$28/1481818223.accept(Unknown Source)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at org.apache.tinkerpop.gremlin.driver.ser.AbstractMessageSerializer.addIoRegistries(AbstractMessageSerializer.java:48)
at org.apache.tinkerpop.gremlin.driver.ser.AbstractGraphSONMessageSerializerV2d0.configure(AbstractGraphSONMessageSerializerV2d0.java:77)
at org.apache.tinkerpop.gremlin.driver.Settings$SerializerSettings.lambda$create$0(Settings.java:480)
at org.apache.tinkerpop.gremlin.driver.Settings$SerializerSettings$$Lambda$27/405896924.accept(Unknown Source)
at java.util.Optional.ifPresent(Optional.java:159)
at org.apache.tinkerpop.gremlin.driver.Settings$SerializerSettings.create(Settings.java:480)
at org.apache.tinkerpop.gremlin.driver.Cluster.getBuilderFromSettings(Cluster.java:229)
at org.apache.tinkerpop.gremlin.driver.Cluster.build(Cluster.java:173)
at org.apache.tinkerpop.gremlin.driver.Cluster.open(Cluster.java:265)

zhengyingying avatar Jun 15 '21 12:06 zhengyingying

@zhengyingying HugeGraph has made many changes in the serialization part of Graphson, which does not guarantee that the tinkerpop-java-client that can now adapt to Gremlin.

It is currently known that: HugeVertex and HugeEgde are not supported by the derivatives of the Gremlin client. (Users should better use our hugegraph-java-client instead, related doc)

So, based in version 0.11.2, a runnable configuration: (Note: 0.11.2 is a legacy version, watch out the diff in the latest release)

gremlin-server.yaml:

serializers:
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1,
      config: {
        serializeResultToString: false,
        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
      }
  }
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0,
      config: {
        serializeResultToString: true,
        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
      }
  }

client driver.yaml:

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

demo code:

ResultSet results = client.submit("hugegraph.traversal().V()");
results.stream().forEach(result -> {
    System.out.println(result.getObject());
//  System.out.println(result.getVertex());
});

results = client.submit("hugegraph.traversal().E()");
results.stream().forEach(result -> {
    System.out.println(result.getObject());
//  System.out.println(result.getEdge());
});

Note: Because deserialization on the gremlin client is not supported, getVertex and getEdge methods cannot be used

注意:因为不支持在gremlin客户端的反序列化,所以不能用getVertex和getEdge方法。

Linary avatar Jun 16 '21 04:06 Linary

@Linary 非常感谢,这个问题已经解决了。

但是通过HugeClient访问远端server,有报错。

还是上述配置,使用下面的代码访问远端的server。

 HugeClient hugeClient = new HugeClientBuilder("http://192.168.1.101:8080", "hugegraph").build();
 GraphManager graph = hugeClient.graph();

报错如下:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkNotNull(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;
at com.baidu.hugegraph.util.E.checkNotNull(E.java:31)
at com.baidu.hugegraph.api.API.<init>(API.java:37)
at com.baidu.hugegraph.api.version.VersionAPI.<init>(VersionAPI.java:31)
at com.baidu.hugegraph.driver.VersionManager.<init>(VersionManager.java:31)
at com.baidu.hugegraph.driver.HugeClient.initManagers(HugeClient.java:82)
at com.baidu.hugegraph.driver.HugeClient.<init>(HugeClient.java:63)
at com.baidu.hugegraph.driver.HugeClientBuilder.build(HugeClientBuilder.java:67)

server端可以通过url http://192.168.1.101:8080 访问

image

zhengyingying avatar Jun 16 '21 11:06 zhengyingying

Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label

github-actions[bot] avatar Jul 06 '21 21:07 github-actions[bot]

For the remote server,I have the exactly same problem with you.It's because the guava version is not correctly.

xingtianxia1314 avatar Apr 08 '24 07:04 xingtianxia1314

For the remote server,I have the exactly same problem with you.It's because the guava version is not correctly.

@xingtianxia1314 @dosu-bot maybe u could submit a new issue if the context is changed (Or tell us your local environment / server version...etc)

imbajin avatar Apr 08 '24 08:04 imbajin