Nebula Java¶
Nebula Java is a Java client for connecting to and managing the NebulaGraph database.
Prerequisites¶
You have installed Java 8.0 or later versions.
Compatibility with NebulaGraph¶
NebulaGraph version | Nebula Java version |
---|---|
2.6.2 | 2.6.1 |
2.0.1 | 2.0.0 |
2.0.0 | 2.0.0 |
2.0.0-rc1 | 2.0.0-rc1 |
Download Nebula Java¶
-
(Recommended) To install a specific version of Nebula Java, use the Git option
--branch
to specify the branch. For example, to install v2.6.1, run the following command:$ git clone --branch v2.6.1 https://github.com/vesoft-inc/nebula-java.git
-
To install the daily development version, run the following command to download the source code from the
master
branch:$ git clone https://github.com/vesoft-inc/nebula-java.git
Use Nebula Java¶
Note
We recommend that each thread uses one session. If multiple threads use the same session, the performance will be reduced.
When importing a Maven project with tools such as IDEA, set the following dependency in pom.xml
.
Note
2.0.0-SNAPSHOT
indicates the daily development version that may have unknown issues. We recommend that you replace 2.0.0-SNAPSHOT
with a released version number to use a table version.
<dependency>
<groupId>com.vesoft</groupId>
<artifactId>client</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
If you cannot download the dependency for the daily development version, set the following content in pom.xml
. Released versions have no such issue.
<repositories>
<repository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
If there is no Maven to manage the project, manually download the JAR file to install Nebula Java.
Core of the example code¶
This sub-section shows the core of the example code. For all the code, see GraphClientExample.
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();
}