Skip to content
Blog

Java API

See the following link for the full documentation of the Java API.

Handling Kuzu output using getNext()

For the examples in this section we will be using the following schema:

CREATE NODE TABLE person(id INT64 PRIMARY KEY);

The getNext() function in a QueryResult returns a reference to the resulting FlatTuple. Additionally, to reduce resource allocation all calls to getNext() reuse the same FlatTuple object. This means that for a QueryResult, each call to getNext() actually overwrites the FlatTuple previously returned by the previous call to getNext().

Thus, we don’t recommend using QueryResult like this:

QueryResult result = conn.query("MATCH (p:person) RETURN p.*");
List<FlatTuple> tuples = new ArrayList<FlatTuple>();
while (result.hasNext()) {
// Each call to getNext() actually returns a reference to the same tuple object
tuples.add(result.getNext());
}
// This is wrong!
// The list stores a bunch of references to the same underlying tuple object
for (FlatTuple resultTuple: tuples) {
doSomething(resultTuple);
}

Instead, we recommend processing each tuple immediately before making the next call to getNext:

QueryResult result = conn.query("MATCH (p:person) RETURN p.*");
while (result.hasNext()) {
FlatTuple tuple = result.getNext();
doSomething(tuple);
}

If wish to process the tuples later, you must explicitly make a copy of each tuple:

List<Value> copyFlatTuple(FlatTuple tuple, long tupleLen) throws ObjectRefDestroyedException {
List<Value> ret = new ArrayList<Value>();
for (int i = 0; i < tupleLen; i++) {
ret.add(tuple.getValue(i).clone());
}
return ret;
}
void mainFunction() throws ObjectRefDestroyedException {
QueryResult result = conn.query("MATCH (p:person) RETURN p.*");
List<List<Value>> tuples = new ArrayList<List<Value>>();
while (result.hasNext()) {
FlatTuple tuple = result.getNext();
tuples.add(copyFlatTuple(tuple, result.getNumColumns()));
}
for (List<Value> tuple: tuples) {
doSomething(tuple);
}
}