Using the SDK

Updated at:

This topic describes how to use the SDK in specific scenarios to achieve your desired results.

Script parameterization

If your requests are identical and only the parameters change, you can use a template to access the instance. For more information, see Parameter templates. Do not use more than 128 template parameters in a single request statement.

Request timeout settings

The default timeout for Graph Database (GDB) is 30 s. If you have time-consuming requests, set a longer timeout. For more information, see Timeout settings.

SDK parameter configuration guide

hosts: [ ${gdbHost} ]
port: 8182
username: ${username}
password: ${password}
connectionPool: {  
  maxSize: 16,  
  minSize: 16,  
  maxInProcessPerConnection: 4,  
  maxContentLength: ${yourExpectedSize}
}
serializer: {  
  className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1,
  config: { serializeResultToString: false }
}
Note
  • ${gdbHost}: The endpoint of GDB. For example, gds-bp*******************.graphdb.rds.aliyuncs.com.

  • ${username}: The instance account.

  • ${password}: The instance password.

  • ${yourExpectedSize}: The default value is 65536. If your result set is too large, an error such as Max frame length of 65536 has been exceeded may occur. To resolve this issue, you can adjust this value as needed.

  • maxSize and minSize: The client has a connection pool and supports concurrent access from multiple threads. Set these two parameters to the same value. The value must be greater than or equal to the number of client access threads to prevent the Timed out while waiting for an available host error.

  • Serialization protocol (Use the GraphBinaryMessageSerializerV1 protocol if possible):

    • GraphBinaryMessageSerializerV1 protocol: Returns Vertex and Edge as ReferenceVertex and ReferenceEdge. Properties are not returned during serialization. This protocol provides good performance.

    • GraphSONMessageSerializerV3d0 protocol: Returns Vertex and Edge as DetachedVertex and DetachedEdge. All properties are returned during serialization. This process adds significant serialization overhead if a vertex has many properties.

Fetch only the results you need

For better performance, fetch only the results that you need in a query. Avoid fetching unnecessary data.

  • Recommended request:

    g.V('id').valueMap('name') // This DSL fetches only the name property
  • Standard request:

    g.V('id').valueMap() // This DSL fetches all properties of the point

Actively fetch results after a request

After the client submits a request, the server places the results in a queue. The client must then explicitly fetch the results.

ResultSet results = client.submit(dsl,parameters); // After this step, actively fetch the results.
List<Result> result = results.all().join(); // Actively fetch the results.

Property length limit

GDB supports simple Java primitive types. The maximum length for a String type is 64 KB. Do not store overly long properties in GDB.

Fetch results in batches

To fetch a large amount of data, you can limit the size of each returned package to prevent errors.

Example:

ResultSet results = client[Integer.parseInt(threadIndex) % num].submitAsync(dsl, options.create()).get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
List<Result> list = new LinkedList<>();

while (true) {
    CompletableFuture<List<Result>> batch = results.some(1024);
    List<Result> tmpList = batch.get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    if (tmpList == null || tmpList.isEmpty()) {
        break;
    }
    /* Fetch the required data in batches. */
    list.addAll(tmpList);
}

Result set transformation

You can transform the result set from GDB into objects that the SDK can use.

Example:

 List<Result> results = client.submitAsync("userTest", dsl, parameters);

            System.out.println("before-output-detail -  " + dsl + " list size - " + results.size() + " useTimeMillis - " + String.valueOf(System.currentTimeMillis() - start));
            results.forEach(result -> {
                if (result.getObject() instanceof ReferenceVertex) {
                    ReferenceVertex vertex = (ReferenceVertex) result.getObject();
                    System.out.println("ReferenceVertex id - " + vertex.id() + " label - " + vertex.label() + " properties - ");
                    vertex.properties().forEachRemaining(p -> System.out.println(p));
                } else if (result.getObject() instanceof DetachedVertex) {
                    System.out.println("DetachedVertex id - " + result.getVertex().id() + " label - " + result.getVertex().label() + " properties - ");
                    result.getVertex().properties().forEachRemaining(p -> System.out.println(p));
                } else {
                    System.out.println("DefaultResult - " + result);
                }
            });