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

Connect HugeGraphServer with gremlin-driver

Open Linary opened this issue 7 years ago • 0 comments

Although HugeGraph provides HugeGraphServer and HugeClient (HTTP protocol) for client operations, some users still want to connect HugeGraph and execute gremlin statements through Gremlin-Driver. Here is a feasible configuration and sample code.

  1. gremlin-server.yaml
host: localhost
port: 8182
scriptEvaluationTimeout: 30000
channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
graphs: {
  hugegraph: ${the path of hugegraph.properties}
}
plugins:
  - com.baidu.hugegraph
scriptEngines: {
  gremlin-groovy: {
    imports: [java.lang.Math],
    staticImports: [java.lang.Math.PI],
    scripts: [scripts/empty-sample.groovy]
  }
}
serializers:
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0,
      config: {
        serializeResultToString: false,
        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
      }
    }
metrics: {
  consoleReporter: {enabled: false, interval: 10000},
  csvReporter: {enabled: false, interval: 10000, fileName: /tmp/gremlin-server-metrics.csv},
  jmxReporter: {enabled: false},
  slf4jReporter: {enabled: false, interval: 10000},

  gangliaReporter: {enabled: false, interval: 10000, addressingMode: MULTICAST},
  graphiteReporter: {enabled: false, interval: 10000}
}
maxInitialLineLength: 4096
maxHeaderSize: 8192
maxChunkSize: 8192
maxContentLength: 65536
maxAccumulationBufferComponents: 1024
resultIterationBatchSize: 64
writeBufferLowWaterMark: 32768
writeBufferHighWaterMark: 65536
ssl: {
  enabled: false
}
  1. gremlin-driver-settings.yaml
hosts: [localhost]
port: 8182
serializer: {
  className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0
}
  1. dependency
<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>gremlin-driver</artifactId>
    <version>3.2.5</version>
</dependency>
  1. TestDemo.java
public static void main(String[] args) throws Exception {
        Cluster cluster = Cluster.open("gremlin-driver-settings.yaml");
        Client 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();
        }
    }
  1. output(data from com.baidu.hugegraph.example.Example2)
v[2:lop]
v[1:josh]
v[1:marko]
v[1:peter]
v[1:vadas]
v[2:ripple]
e[S1:josh>2>>S2:lop][1:josh-created->2:lop]
e[S1:josh>2>>S2:ripple][1:josh-created->2:ripple]
e[S1:marko>1>20130220>S1:josh][1:marko-knows->1:josh]
e[S1:marko>1>20160110>S1:vadas][1:marko-knows->1:vadas]
e[S1:marko>2>>S2:lop][1:marko-created->2:lop]
e[S1:peter>2>>S2:lop][1:peter-created->2:lop]

Linary avatar Jan 24 '19 12:01 Linary