Java SDK guide¶
This topic introduces how to set up the Java SDK to connect to NebulaGraph Database Developer Edition and perform simple queries.
Prerequisites¶
- JDK 8 is installed.
- A NebulaGraph Database Developer Edition service group is running. For more information, see Getting started with NebulaGraph Database Developer Edition.
Steps¶
-
Import the Java SDK into your Java project. For example, to import with Maven, add the following dependency to the
pom.xmlfile:<dependency> <groupId>com.vesoft</groupId> <artifactId>driver</artifactId> <version>5.3.1</version> </dependency>Note: After you reference the NebulaGraph Database Developer Edition SDK dependency in a project and package it as a JAR file, if you encounter the exception
java.lang.IllegalArgumentException: Address types of NameResolver 'unix' for '192.168.15.8:9669' not supported by transportwhen running this JAR file, add the pluginmaven-shade-pluginto your project'spom.xmlfile and configure the transformer as follows:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <configuration> <!-- put your configurations here --> <filters> <filter> <artifact>*:*</artifact> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"> </transformer> </transformers> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> -
Select either of the following methods to connect to NebulaGraph Database Developer Edition and execute queries:
Note
For on-premises users, apart from specifying the username and password when connecting to NebulaGraph Database Developer Edition, you can also enable the sessionless mode, which allows you to not provide authentication information. For more information, see Sessionless mode.
-
If only one client needs to interact with NebulaGraph Database Developer Edition, you can use the client to establish a direct connection and execute queries.
NebulaClient client = null; try { client = NebulaClient.builder("<host>:<port>", "<username>", "<password>") .withAuthOptions(Collections.emptyMap()) .withConnectTimeoutMills(1000) .withRequestTimeoutMills(3000) .build(); client.execute("USE nba MATCH (v:player) RETURN v.id, v.name, v.score, v.gender, v.rate"); } catch (Exception e) { throw e; } finally { if (client != null) { client.close(); } }
-
If multiple clients need to interact with NebulaGraph Database Developer Edition simultaneously, you can create a connection pool to manage a set of connections. You can use the pool to get a client and return it after queries are executed.
NebulaPool pool = null; try { pool = NebulaPool .builder("<host>:<port>", "<username>", "<password>") .withMaxClientSize(10) .withMinClientSize(1) .withConnectTimeoutMills(1000) .withRequestTimeoutMills(30000) .withBlockWhenExhausted(true) .withMaxWaitMills(Long.MAX_VALUE) .build(); NebulaClient client = pool.getClient(); client.execute("USE nba MATCH (v:player) RETURN v.id, v.name, v.score, v.gender, v.rate"); pool.returnClient(client); } catch (Exception e) { throw e; } finally { if (pool != null) { pool.close(); } }
Replace
<host>,<port>,<username>, and<password>with the actual NebulaGraph Database Developer Edition connection information. -