# Interface ReadContext (6.118.0)

6.118.0 (latest) 6.116.0 6.111.1 6.108.0 6.107.0 6.103.0 6.102.1 6.101.1 6.100.0 6.99.0 6.98.1 6.97.1 6.96.1 6.95.1 6.94.0 6.93.0 6.89.0 6.88.0 6.87.0 6.86.0 6.85.0 6.83.0 6.82.0 6.80.1 6.79.0 6.77.0 6.74.1 6.72.0 6.71.0 6.69.0 6.68.0 6.66.0 6.65.1 6.62.0 6.60.0 6.58.0 6.57.0 6.56.0 6.55.0 6.54.0 6.53.0 6.52.1 6.51.0 6.50.1 6.49.0 6.25.1 6.24.0 6.23.4 6.22.0 6.21.2 6.20.0 6.19.1 6.18.0 6.17.4 6.14.1

    public interface ReadContext extends AutoCloseable

A concurrency context in which to run a read or SQL statement. All `ReadContext`s are
implicitly bound to a [Session](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.spanner.v1.Session) and therefore a particular [Database](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.spanner.admin.database.v1.Database).

## Implements

[AutoCloseable](https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html)

## Methods

### analyzeQuery(Statement statement, ReadContext.QueryAnalyzeMode queryMode)

    public abstract ResultSet analyzeQuery(Statement statement, ReadContext.QueryAnalyzeMode queryMode)

Analyzes a query and returns query plan and/or query execution statistics information.

The query plan and query statistics information is contained in [com.google.spanner.v1.ResultSetStats](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.spanner.v1.ResultSetStats) that can be accessed by calling [ResultSet#getStats()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.spanner.v1.ResultSet#com_google_spanner_v1_ResultSet_getStats__) on the returned `ResultSet`.


     ReadContext rc = dbClient.singleUse();
     ResultSet resultSet =
         rc.analyzeQuery(
             Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"),
             ReadContext.QueryAnalyzeMode.PROFILE);
     while (resultSet.next()) {
       // Discard the results. We're only processing because getStats() below requires it.
       resultSet.getCurrentRowAsStruct();
     }
     ResultSetStats stats = resultSet.getStats();
     
| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `statement` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Statement` the query statement to execute |
| `queryMode` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ReadContext.QueryAnalyzeMode` the mode in which to execute the query |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet` |   |

### close()

    public abstract void close()

Closes this read context and frees up the underlying resources.

### executeQuery(Statement statement, Options.QueryOption\[\] options)

    public abstract ResultSet executeQuery(Statement statement, Options.QueryOption[] options)

Executes a query against the database. Can also execute a DML statement with returning clause
in a read/write transaction.

Implementations may or may not block in the initial `executeQuery(...)` call; for
those that do not, the remote call will be initiated immediately but blocking on the response
is deferred to the first [ResultSet#next()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet#com_google_cloud_spanner_ResultSet_next__) call. Regardless of blocking behavior, any
[SpannerException](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.SpannerException) is deferred to the first or subsequent [ResultSet#next()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet#com_google_cloud_spanner_ResultSet_next__) call.


     // Rows without an explicit value for MarketingBudget will have a MarketingBudget equal to
     // null.
     ReadContext readContext = dbClient.singleUse();
     ResultSet resultSet =
         readContext.executeQuery(
             Statement.of(
                 "SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime FROM Albums"));
     
| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `statement` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Statement` the query statement to execute |
| `options` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Options.QueryOption[]` the options to configure the query |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet` |   |

### executeQueryAsync(Statement statement, Options.QueryOption\[\] options)

    public abstract AsyncResultSet executeQueryAsync(Statement statement, Options.QueryOption[] options)

Same as [#executeQuery(Statement, QueryOption...)](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ReadContext#com_google_cloud_spanner_ReadContext_executeQuery_), but is guaranteed to be non-blocking
and returns its results as an [AsyncResultSet](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.AsyncResultSet).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `statement` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Statement` |
| `options` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Options.QueryOption[]` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.AsyncResultSet` |   |

### read(String table, KeySet keys, Iterable\<String\> columns, Options.ReadOption\[\] options)

    public abstract ResultSet read(String table, KeySet keys, Iterable<String> columns, Options.ReadOption[] options)

Reads zero or more rows from a database.

Implementations may or may not block in the initial `read(...)` call; for those that
do not, the remote call will be initiated immediately but blocking on the response is deferred
to the first [ResultSet#next()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet#com_google_cloud_spanner_ResultSet_next__) call. Regardless of blocking behavior, any [SpannerException](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.SpannerException) is deferred to the first or subsequent [ResultSet#next()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet#com_google_cloud_spanner_ResultSet_next__) call.


     ReadContext readContext = dbClient.singleUse();
     ResultSet resultSet =
         readContext.read(
             "Albums",
             // KeySet.all() can be used to read all rows in a table. KeySet exposes other
             // methods to read only a subset of the table.
             KeySet.all(),
             Arrays.asList("SingerId", "AlbumId", "AlbumTitle"));
     
| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the table to read |
| `keys` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.KeySet` the keys and ranges of rows to read. Regardless of ordering in `keys`, rows are returned in their natural key order. |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` the columns to read |
| `options` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Options.ReadOption[]` the options to configure the read |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet` |   |

### readAsync(String table, KeySet keys, Iterable\<String\> columns, Options.ReadOption\[\] options)

    public abstract AsyncResultSet readAsync(String table, KeySet keys, Iterable<String> columns, Options.ReadOption[] options)

Same as [#read(String, KeySet, Iterable, ReadOption...)](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ReadContext#com_google_cloud_spanner_ReadContext_read_), but is guaranteed to be
non-blocking and will return the results as an [AsyncResultSet](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.AsyncResultSet).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |
| `keys` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.KeySet` |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` |
| `options` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Options.ReadOption[]` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.AsyncResultSet` |   |

### readRow(String table, Key key, Iterable\<String\> columns)

    public abstract Struct readRow(String table, Key key, Iterable<String> columns)

Reads a single row from a database, returning `null` if the row does not exist.


     ReadContext readContext = dbClient.singleUse();
     Struct row =
         readContext.readRow("Albums", Key.of(2, 1), Arrays.asList("MarketingBudget"));
     
| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the table to read |
| `key` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Key` the row to read |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` the columns to return |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Struct` |   |

### readRowAsync(String table, Key key, Iterable\<String\> columns)

    public abstract ApiFuture<Struct> readRowAsync(String table, Key key, Iterable<String> columns)

Same as [#readRow(String, Key, Iterable)](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ReadContext#com_google_cloud_spanner_ReadContext_readRow_java_lang_String_com_google_cloud_spanner_Key_java_lang_Iterable_java_lang_String__), but is guaranteed to be non-blocking.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |
| `key` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Key` |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFuture.html<https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Struct>` |   |

### readRowUsingIndex(String table, String index, Key key, Iterable\<String\> columns)

    public abstract Struct readRowUsingIndex(String table, String index, Key key, Iterable<String> columns)

Reads a single row from a database using an index, returning `null` if the row does not
exist.


     ReadContext readContext = dbClient.singleUse();
     Struct row =
         readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
             Arrays.asList("AlbumId", "AlbumTitle"));
     
| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the table to read |
| `index` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the index on `table` to use |
| `key` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Key` the index row to read |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` the columns to return |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Struct` |   |

### readRowUsingIndexAsync(String table, String index, Key key, Iterable\<String\> columns)

    public abstract ApiFuture<Struct> readRowUsingIndexAsync(String table, String index, Key key, Iterable<String> columns)

Same as [#readRowUsingIndex(String, String, Key, Iterable)](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ReadContext#com_google_cloud_spanner_ReadContext_readRowUsingIndex_java_lang_String_java_lang_String_com_google_cloud_spanner_Key_java_lang_Iterable_java_lang_String__), but is guaranteed to be
non-blocking.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |
| `index` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |
| `key` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Key` |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFuture.html<https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Struct>` |   |

### readUsingIndex(String table, String index, KeySet keys, Iterable\<String\> columns, Options.ReadOption\[\] options)

    public abstract ResultSet readUsingIndex(String table, String index, KeySet keys, Iterable<String> columns, Options.ReadOption[] options)

Reads zero or more rows from a database using an index.

Implementations may or may not block in the initial `read(...)` call; for those that
do not, the remote call will be initiated immediately but blocking on the response is deferred
to the first [ResultSet#next()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet#com_google_cloud_spanner_ResultSet_next__) call. Regardless of blocking behavior, any [SpannerException](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.SpannerException) is deferred to the first or subsequent [ResultSet#next()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet#com_google_cloud_spanner_ResultSet_next__) call.


     ReadContext readContext = dbClient.singleUse();
     Struct row =
         readContext.readRowUsingIndex("Albums", "AlbumsByAlbumId", Key.of(1, "Green"),
             Arrays.asList("AlbumId", "AlbumTitle"));
     
| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the table to read |
| `index` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` the name of the index on `table` to use |
| `keys` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.KeySet` the keys and ranges of index rows to read. Regardless of ordering in `keys`, rows are returned in the natural key order of the index. |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` the columns to read |
| `options` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Options.ReadOption[]` the options to configure the read |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ResultSet` |   |

### readUsingIndexAsync(String table, String index, KeySet keys, Iterable\<String\> columns, Options.ReadOption\[\] options)

    public abstract AsyncResultSet readUsingIndexAsync(String table, String index, KeySet keys, Iterable<String> columns, Options.ReadOption[] options)

Same as [#readUsingIndex(String, String, KeySet, Iterable, ReadOption...)](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.ReadContext#com_google_cloud_spanner_ReadContext_readUsingIndex_), but is
guaranteed to be non-blocking and will return its results as an [AsyncResultSet](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.AsyncResultSet).

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |
| `index` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |
| `keys` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.KeySet` |
| `columns` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>` |
| `options` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.Options.ReadOption[]` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.AsyncResultSet` |   |