public final class SpannerResourceManager
extends java.lang.Object
implements org.apache.beam.it.common.ResourceManager
The class supports one instance, one database, and multiple tables per manager object. The instance and database are created when the first table is created.
The instance and database ids are formed using testId. The database id will be {testId}, with some extra formatting. The instance id will be "{testId}-{ISO8601 time, microsecond precision}", with additional formatting. Note: If testId is more than 30 characters, a new testId will be formed for naming: {first 21 chars of long testId} + “-” + {8 char hash of testId}.
The class is thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
SpannerResourceManager.Builder
Builder for
SpannerResourceManager . |
Modifier and Type | Method and Description |
---|---|
static SpannerResourceManager.Builder |
builder(java.lang.String testId,
java.lang.String projectId,
java.lang.String region) |
static SpannerResourceManager.Builder |
builder(java.lang.String testId,
java.lang.String projectId,
java.lang.String region,
com.google.cloud.spanner.Dialect dialect) |
static SpannerResourceManager.Builder |
builder(java.lang.String testId,
java.lang.String projectId,
java.lang.String region,
int nodeCount) |
void |
cleanupAll()
Deletes all created resources (instance, database, and tables) and cleans up all Spanner
sessions, making the manager object unusable.
|
void |
executeDdlStatement(java.lang.String statement)
Executes a DDL statement.
|
java.lang.String |
getDatabaseId()
Return the dataset ID this Resource Manager uses to create and manage tables in.
|
java.lang.String |
getInstanceId()
Return the instance ID this Resource Manager uses to create and manage tables in.
|
com.google.common.collect.ImmutableList<com.google.cloud.spanner.Struct> |
readTableRecords(java.lang.String tableId,
java.lang.Iterable<java.lang.String> columnNames)
Reads all the rows in a table.This method requires
executeDdlStatement(String) to be called for the target table
beforehand. |
com.google.common.collect.ImmutableList<com.google.cloud.spanner.Struct> |
readTableRecords(java.lang.String tableId,
java.lang.String... columnNames)
Reads all the rows in a table.
|
void |
write(java.lang.Iterable<com.google.cloud.spanner.Mutation> tableRecords)
Writes a collection of table records into one or more tables.
|
void |
write(com.google.cloud.spanner.Mutation tableRecord)
Writes a given record into a table.
|
public static SpannerResourceManager.Builder builder(java.lang.String testId, java.lang.String projectId, java.lang.String region)
public static SpannerResourceManager.Builder builder(java.lang.String testId, java.lang.String projectId, java.lang.String region, int nodeCount)
public static SpannerResourceManager.Builder builder(java.lang.String testId, java.lang.String projectId, java.lang.String region, com.google.cloud.spanner.Dialect dialect)
public java.lang.String getInstanceId()
public java.lang.String getDatabaseId()
public void executeDdlStatement(java.lang.String statement) throws java.lang.IllegalStateException
Note: Implementations may do instance creation and database creation here.
statement
- The DDL statement.java.lang.IllegalStateException
- if method is called after resources have been cleaned up.public void write(com.google.cloud.spanner.Mutation tableRecord) throws java.lang.IllegalStateException
executeDdlStatement(String)
to be called for the target table
beforehand.tableRecord
- A mutation object representing the table record.java.lang.IllegalStateException
- if method is called after resources have been cleaned up or if
the manager object has no instance or database.public void write(java.lang.Iterable<com.google.cloud.spanner.Mutation> tableRecords) throws java.lang.IllegalStateException
executeDdlStatement(String)
to be called for the target table
beforehand.tableRecords
- A collection of mutation objects representing table records.java.lang.IllegalStateException
- if method is called after resources have been cleaned up or if
the manager object has no instance or database.public com.google.common.collect.ImmutableList<com.google.cloud.spanner.Struct> readTableRecords(java.lang.String tableId, java.lang.String... columnNames) throws java.lang.IllegalStateException
executeDdlStatement(String)
to be called for the target table
beforehand.tableId
- The id of the table to read rows from.columnNames
- The table's column names.java.lang.IllegalStateException
- if method is called after resources have been cleaned up or if
the manager object has no instance or database.public com.google.common.collect.ImmutableList<com.google.cloud.spanner.Struct> readTableRecords(java.lang.String tableId, java.lang.Iterable<java.lang.String> columnNames) throws java.lang.IllegalStateException
executeDdlStatement(String)
to be called for the target table
beforehand.tableId
- The id of table to read rows from.columnNames
- A collection of the table's column names.java.lang.IllegalStateException
- if method is called after resources have been cleaned up or if
the manager object has no instance or database.public void cleanupAll()
cleanupAll
in interface org.apache.beam.it.common.ResourceManager