use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project java-spanner by googleapis.
the class AsyncReadExample method asyncRead.
// Execute a query asynchronously and process the results in a callback.
static void asyncRead(DatabaseClient client) throws InterruptedException, ExecutionException, TimeoutException {
ApiFuture<Void> finished;
ExecutorService executor = Executors.newSingleThreadExecutor();
try (AsyncResultSet resultSet = client.singleUse().readAsync("Albums", // Read all rows in a table.
KeySet.all(), Arrays.asList("SingerId", "AlbumId", "AlbumTitle"))) {
// Setting a callback will automatically start the iteration over the results of the query
// using the specified executor. The callback will be called at least once. The returned
// ApiFuture is done when the callback has returned DONE and all resources used by the
// AsyncResultSet have been released.
finished = resultSet.setCallback(executor, new ReadyCallback() {
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
try {
while (true) {
switch(resultSet.tryNext()) {
// OK: There is a row ready.
case OK:
System.out.printf("%d %d %s%n", resultSet.getLong(0), resultSet.getLong(1), resultSet.getString(2));
break;
// DONE: There are no more rows in the result set.
case DONE:
return CallbackResponse.DONE;
// NOT_READY: There are currently no more rows in the buffer.
case NOT_READY:
return CallbackResponse.CONTINUE;
default:
throw new IllegalStateException();
}
}
} catch (SpannerException e) {
System.out.printf("Error in callback: %s%n", e.getMessage());
return CallbackResponse.DONE;
}
}
});
}
// This ApiFuture is done when the callback has returned DONE and all resources of the
// asynchronous result set have been released.
finished.get(30L, TimeUnit.SECONDS);
executor.shutdown();
}
use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClient method singleReadAsync.
public ApiFuture<Void> singleReadAsync() {
final AsyncResultSet asyncResultSet = dbClient.singleUse().readAsync(TABLE_NAME, READ_KEY_SET, ALL_COLUMNS);
final ApiFuture<Void> readFuture = asyncResultSet.setCallback(executor, new ReadyCallback() {
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
while (true) {
switch(resultSet.tryNext()) {
case OK:
// System.out.println(resultSet.getString("StringValue"));
break;
case NOT_READY:
return CallbackResponse.CONTINUE;
case DONE:
return CallbackResponse.DONE;
default:
throw new IllegalStateException();
}
}
}
});
return readFuture;
}
use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClient method singleQueryAsync.
public ApiFuture<Void> singleQueryAsync() {
final AsyncResultSet asyncResultSet = dbClient.singleUse().executeQueryAsync(Statement.of("SELECT Key,StringValue FROM " + TABLE_NAME + " WHERE -123 != " + uniqueString()));
final ApiFuture<Void> queryFuture = asyncResultSet.setCallback(executor, new ReadyCallback() {
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
while (true) {
switch(resultSet.tryNext()) {
case OK:
// System.out.println(resultSet.getString("StringValue"));
break;
case NOT_READY:
return CallbackResponse.CONTINUE;
case DONE:
return CallbackResponse.DONE;
default:
throw new IllegalStateException();
}
}
}
});
return queryFuture;
}
use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project java-spanner by googleapis.
the class ITAsyncExamplesTest method readUsingIndexAsync.
@Test
public void readUsingIndexAsync() throws Exception {
final SettableApiFuture<List<String>> future = SettableApiFuture.create();
try (AsyncResultSet rs = client.singleUse().readUsingIndexAsync(TABLE_NAME, INDEX_NAME, KeySet.all(), ALL_COLUMNS)) {
rs.setCallback(executor, new ReadyCallback() {
final List<String> values = new LinkedList<>();
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
try {
while (true) {
switch(resultSet.tryNext()) {
case DONE:
future.set(values);
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
case OK:
values.add(resultSet.getString("StringValue"));
break;
}
}
} catch (Throwable t) {
future.setException(t);
return CallbackResponse.DONE;
}
}
});
}
assertThat(future.get()).containsExactlyElementsIn(ALL_VALUES_IN_PK_ORDER);
}
use of com.google.cloud.spanner.AsyncResultSet.ReadyCallback in project java-spanner by googleapis.
the class ITAsyncExamplesTest method executeQueryAsync.
@Test
public void executeQueryAsync() throws Exception {
final ImmutableList<String> keys = ImmutableList.of("k3", "k4");
final SettableApiFuture<List<String>> future = SettableApiFuture.create();
try (AsyncResultSet rs = client.singleUse().executeQueryAsync(Statement.newBuilder("SELECT StringValue FROM TestTable WHERE Key IN UNNEST(@keys)").bind("keys").toStringArray(keys).build())) {
rs.setCallback(executor, new ReadyCallback() {
final List<String> values = new LinkedList<>();
@Override
public CallbackResponse cursorReady(AsyncResultSet resultSet) {
try {
while (true) {
switch(resultSet.tryNext()) {
case DONE:
future.set(values);
return CallbackResponse.DONE;
case NOT_READY:
return CallbackResponse.CONTINUE;
case OK:
values.add(resultSet.getString("StringValue"));
break;
}
}
} catch (Throwable t) {
future.setException(t);
return CallbackResponse.DONE;
}
}
});
}
assertThat(future.get()).containsExactly("v3", "v4");
}
Aggregations