跳转至

Nebula JavaGraph

Graph是一款Java语言的客户端,可以连接、管理NebulaGraph图数据库。

前提条件Graph

已安装Java,版本为8.0及以上。

版本对照表Graph

NebulaGraph版本 Nebula Java版本
2.6.0 2.6.0
2.0.1 2.0.0
2.0.0 2.0.0
2.0.0-rc1 2.0.0-rc1

下载Nebula JavaGraph

  • (推荐)如果需要使用指定版本的Nebula Java,请使用选项--branch指定分支。例如使用v2.6.0发布版本,请执行如下命令:

    $ git clone --branch v2.6.0 https://github.com/vesoft-inc/nebula-java.git
    
  • 如果需要安装日常开发版本,请执行如下命令下载master分支的源码:

    $ git clone https://github.com/vesoft-inc/nebula-java.git
    

使用方法Graph

Note

建议一个线程使用一个会话,如果多个线程使用同一个会话,会降低效率。

使用IDEA等工具导入Maven项目,请在pom.xml中添加如下依赖:

Note

2.0.0-SNAPSHOT为日常研发版本,可能存在未知问题,建议使用release版本号替换2.0.0-SNAPSHOT

<dependency>
  <groupId>com.vesoft</groupId>
  <artifactId>client</artifactId>
  <version>2.0.0-SNAPSHOT</version>
</dependency>

如果无法下载日常研发版本的依赖,请在pom.xml中添加如下内容(release版本不需要添加):

<repositories> 
  <repository> 
    <id>snapshots</id> 
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url> 
  </repository> 
</repositories>

如果没有Maven管理项目,请手动Graph进行安装。

核心代码Graph

详细示例请参见Graph。

NebulaPool pool = new NebulaPool();
Session session = null;
try {
    NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
    nebulaPoolConfig.setMaxConnSize(100);
    List<HostAddress> addresses = Arrays.asList(new HostAddress("192.168.xx.1", 9669),
            new HostAddress("192.168.xx.2", 9670));
    pool.init(addresses, nebulaPoolConfig);
    session = pool.getSession("root", "nebula", false);

    //create space
    String space = "test";
    String createSpace = "CREATE SPACE IF NOT EXISTS " + space + " (partition_num=15, replica_factor=1, vid_type=fixed_string(30)); ";
    ResultSet resp = session.execute(createSpace);


    //create schema
    String createSchema = "USE " + space + "; CREATE TAG IF NOT EXISTS person(name string, age int);"
            + "CREATE EDGE IF NOT EXISTS like(likeness double)";
    ResultSet resp = session.execute(createSchema);

    //insert vertex
    String insertVertexes = "INSERT VERTEX person(name, age) VALUES " + "'Bob':('Bob', 10), "
            + "'Lily':('Lily', 9), " + "'Tom':('Tom', 10), " + "'Jerry':('Jerry', 13), "
            + "'John':('John', 11);";
    ResultSet resp = session.execute(insertVertexes);

    // inert edge
    String insertEdges = "INSERT EDGE like(likeness) VALUES " + "'Bob'->'Lily':(80.0), "
            + "'Bob'->'Tom':(70.0), " + "'Lily'->'Jerry':(84.0), " + "'Tom'->'Jerry':(68.3), "
            + "'Bob'->'John':(97.2);";
    ResultSet resp = session.execute(insertEdges);

    // query
    String query = "GO FROM \"Bob\" OVER like " + "YIELD properties($$).name, properties($$).age, properties(edge).likeness";
    ResultSet resp = session.execute(query);
    printResult(resp);
}finally {
  if (session != null) {
      session.release();
  }
  pool.close();
}

最后更新: October 22, 2021
Back to top