• 单机离线调用模式;
    • Client/Server请求模式;

    由于lib目录下已经包含了HugeCore的jar包,且HugeGraph已经作为插件注册到Console中,用户可以直接写groovy脚本调用HugeGraph-Core的代码,然后交由Gremlin-Console内的解析引擎执行,就能在不启动Server的情况下操作图。

    这种模式便于用户快速上手体验,但是不适合大量数据插入和查询的场景。下面给一个示例:

    在script目录下有一个示例脚本:example.groovy

    其实这一段groovy脚本几乎就是Java代码,不同之处仅在于变量的定义可以不写类型声明,以及每一行末尾的分号可以去掉。

    1. objc[5038]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10137a4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x102bbb4e0). One of the two will be used. Which one is undefined.
    2. \,,,/
    3. (o o)
    4. -----oOOo-(3)-oOOo-----
    5. plugin activated: com.baidu.hugegraph
    6. plugin activated: tinkerpop.server
    7. plugin activated: tinkerpop.utilities
    8. plugin activated: tinkerpop.tinkergraph
    9. 2018-01-15 14:36:19 7516 [main] [WARN ] com.baidu.hugegraph.config.HugeConfig [] - The config option 'rocksdb.data_path' is redundant, please ensure it has been registered
    10. 2018-01-15 14:36:19 7523 [main] [WARN ] com.baidu.hugegraph.config.HugeConfig [] - The config option 'rocksdb.wal_path' is redundant, please ensure it has been registered
    11. 2018-01-15 14:36:19 7604 [main] [INFO ] com.baidu.hugegraph.HugeGraph [] - Opening backend store 'cassandra' for graph 'hugegraph'
    12. >>>> query all vertices: size=6
    13. >>>> query all edges: size=6

    可以看到,插入了6个顶点、6条边,并查询出来了。进入console之后,还可继续输入groovy语句对图做操作:

    1. gremlin> g.V()
    2. ==>v[2:ripple]
    3. ==>v[1:vadas]
    4. ==>v[1:peter]
    5. ==>v[1:josh]
    6. ==>v[1:marko]
    7. ==>v[2:lop]
    8. ==>e[S1:josh>2>>S2:ripple][1:josh-created->2:ripple]
    9. ==>e[S1:marko>1>20160110>S1:vadas][1:marko-knows->1:vadas]
    10. ==>e[S1:peter>2>>S2:lop][1:peter-created->2:lop]
    11. ==>e[S1:marko>1>20130220>S1:josh][1:marko-knows->1:josh]
    12. ==>e[S1:marko>2>>S2:lop][1:marko-created->2:lop]

    更多的Gremlin语句请参考

    2 Client/Server请求模式

    因为Gremlin-Console只能通过WebSocket连接HugeGraph-Server,默认HugeGraph-Server是对外提供HTTP连接的,所以先修改gremlin-server的配置。

    注意:将连接方式修改为WebSocket后,HugeGraph-Client、HugeGraph-Loader、HugeGraph-Studio等配套工具都不能使用了。

    channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer修改成channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer或直接注释,然后按照步骤启动Server。

    1. bin/gremlin-console.sh
    2. objc[5761]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10ec584c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10ecdc4e0). One of the two will be used. Which one is undefined.
    3. \,,,/
    4. (o o)
    5. -----oOOo-(3)-oOOo-----
    6. plugin activated: com.baidu.hugegraph
    7. plugin activated: tinkerpop.server
    8. plugin activated: tinkerpop.utilities
    9. plugin activated: tinkerpop.tinkergraph

    连接server,需在配置文件中指定连接参数,在conf目录下有一个默认的remote.yaml

    1. # cat conf/remote.yaml
    2. hosts: [localhost]
    3. port: 8182
    4. serializer: {
    5. className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0,
    6. config: {
    7. serializeResultToString: true,
    8. ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
    9. }
    10. }

    连接成功之后,在console的上下文中能使用的变量只有hugegraph和hugegraph1两个图对象(在gremlin-server.yaml中配置),如果想拥有更多的变量,可以在scripts/empty-sample.groovy中添加,如:

    1. // an init script that returns a Map allows explicit setting of global bindings.
    2. def globals = [:]
    3. // defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
    4. // note that the name of the key in the "global" map is unimportant.
    5. globals << [hook: [
    6. onStartUp : { ctx ->
    7. ctx.logger.info("Executed once at startup of Gremlin Server.")
    8. },
    9. onShutDown: { ctx ->
    10. ctx.logger.info("Executed once at shutdown of Gremlin Server.")
    11. }
    12. ] as LifeCycleHook]
    13. // define schema manger for hugegraph
    14. schema = hugegraph.schema()
    15. // define the default TraversalSource to bind queries to - this one will be named "g".
    16. g = hugegraph.traversal()

    这样在console中便可以直接使用schema和g这两个对象,做元数据的管理和图的查询了。

    不定义了也没关系,因为所有的对象都可以通过graph获得,例如:

    1. gremlin> :> hugegraph.traversal().V()
    2. ==>v[2:ripple]
    3. ==>v[1:vadas]
    4. ==>v[1:peter]
    5. ==>v[1:josh]
    6. ==>v[1:marko]

    在Client/Server模式下,所有跟Server有关的操作都要加上:>,如果不加,表示在console本地操作。

    更多关于gremlin-console的使用,请参考