public final class BigQueryResourceManager
extends java.lang.Object
implements org.apache.beam.it.common.ResourceManager
The class supports one dataset, and multiple tables per dataset object.
The class is thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
BigQueryResourceManager.Builder
Builder for
BigQueryResourceManager . |
Modifier and Type | Method and Description |
---|---|
static BigQueryResourceManager.Builder |
builder(java.lang.String testId,
java.lang.String projectId,
com.google.auth.Credentials credentials) |
void |
cleanupAll()
Deletes all created resources (dataset and tables) and cleans up the BigQuery client, making
the manager object unusable.
|
java.lang.String |
createDataset(java.lang.String region)
Create a BigQuery dataset in which all tables will exist.
|
com.google.cloud.bigquery.TableId |
createTable(java.lang.String tableName,
com.google.cloud.bigquery.Schema schema)
Creates a table within the current dataset given a table name and schema.
|
com.google.cloud.bigquery.TableId |
createTable(java.lang.String tableName,
com.google.cloud.bigquery.Schema schema,
java.lang.Long expirationTimeMillis)
Creates a table within the current dataset given a table name and schema.
|
java.lang.String |
getDatasetId()
Return the dataset ID this Resource Manager uses to create and manage tables in.
|
java.lang.String |
getProjectId() |
java.lang.Long |
getRowCount(java.lang.String table)
Gets the number of rows in the table.
|
com.google.cloud.bigquery.TableResult |
readTable(java.lang.String tableName)
Reads all the rows in a table and returns a TableResult containing a JSON string
representation.
|
com.google.cloud.bigquery.TableResult |
readTable(java.lang.String tableName,
int numRows)
Reads number of rows in a table and returns a TableResult containing a JSON string
representation.
|
com.google.cloud.bigquery.TableResult |
readTable(com.google.cloud.bigquery.TableId table)
Reads all the rows in a table and returns a TableResult containing a JSON string
representation.
|
com.google.cloud.bigquery.TableResult |
readTable(com.google.cloud.bigquery.TableId table,
int numRows)
Reads number of rows in a table and returns a TableResult containing a JSON string
representation.
|
com.google.cloud.bigquery.TableResult |
runQuery(java.lang.String query)
Runs the specified query.
|
void |
write(java.lang.String tableName,
com.google.cloud.bigquery.InsertAllRequest.RowToInsert row)
Writes a given row into a table.
|
void |
write(java.lang.String tableName,
java.util.List<com.google.cloud.bigquery.InsertAllRequest.RowToInsert> rows)
Writes a collection of table rows into a single table.
|
public static BigQueryResourceManager.Builder builder(java.lang.String testId, java.lang.String projectId, com.google.auth.Credentials credentials)
public java.lang.String getProjectId()
public java.lang.String getDatasetId()
public java.lang.String createDataset(java.lang.String region) throws BigQueryResourceManagerException
region
- the region to store the dataset in.BigQueryResourceManagerException
- if there is an error creating the dataset in BigQuery.public com.google.cloud.bigquery.TableId createTable(java.lang.String tableName, com.google.cloud.bigquery.Schema schema) throws BigQueryResourceManagerException
This table will automatically expire 1 hour after creation if not cleaned up manually or by
calling the cleanupAll()
method.
Note: Implementations may do dataset creation here, if one does not already exist.
tableName
- The name of the table.schema
- A schema object that defines the table.BigQueryResourceManagerException
- if there is an error creating the table in BigQuery.public com.google.cloud.bigquery.TableId createTable(java.lang.String tableName, com.google.cloud.bigquery.Schema schema, java.lang.Long expirationTimeMillis) throws BigQueryResourceManagerException
This table will automatically expire at the time specified by expirationTime
if not
cleaned up manually or by calling the cleanupAll()
method.
Note: Implementations may do dataset creation here, if one does not already exist.
tableName
- The name of the table.schema
- A schema object that defines the table.expirationTimeMillis
- Sets the time when this table expires, in milliseconds since the
epoch.BigQueryResourceManagerException
- if there is an error creating the table in BigQuery.public void write(java.lang.String tableName, com.google.cloud.bigquery.InsertAllRequest.RowToInsert row) throws BigQueryResourceManagerException
createTable(String, Schema)
to be called for the target table
beforehand.tableName
- The name of the table to insert the given row into.row
- A row object representing the table row.BigQueryResourceManagerException
- if method is called after resources have been cleaned
up, if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public void write(java.lang.String tableName, java.util.List<com.google.cloud.bigquery.InsertAllRequest.RowToInsert> rows) throws BigQueryResourceManagerException
createTable(String, Schema)
to be called for the target table
beforehand.tableName
- The name of the table to insert the given rows into.rows
- A collection of table rows.BigQueryResourceManagerException
- if method is called after resources have been cleaned
up, if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public com.google.cloud.bigquery.TableResult runQuery(java.lang.String query)
query
- the query to executepublic java.lang.Long getRowCount(java.lang.String table)
table
- the name of the tablepublic com.google.cloud.bigquery.TableResult readTable(com.google.cloud.bigquery.TableId table) throws BigQueryResourceManagerException
createTable(String,
Schema)
to be called for the target table beforehand.table
- The table reference to read rows from.BigQueryResourceManagerException
- if method is called after resources have been cleaned
up, if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public com.google.cloud.bigquery.TableResult readTable(java.lang.String tableName) throws BigQueryResourceManagerException
createTable(String,
Schema)
to be called for the target table beforehand.tableName
- The name of the table to read rows from.BigQueryResourceManagerException
- if method is called after resources have been cleaned
up, if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public com.google.cloud.bigquery.TableResult readTable(com.google.cloud.bigquery.TableId table, int numRows) throws BigQueryResourceManagerException
createTable(String,
Schema)
to be called for the target table beforehand.table
- The table reference to read rows from.BigQueryResourceManagerException
- if method is called after resources have been cleaned
up, if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public com.google.cloud.bigquery.TableResult readTable(java.lang.String tableName, int numRows) throws BigQueryResourceManagerException
createTable(String,
Schema)
to be called for the target table beforehand.tableName
- The name of the table to read rows from.numRows
- number of rows to read from the tableBigQueryResourceManagerException
- if method is called after resources have been cleaned
up, if the manager object has no dataset, if the table does not exist or if there is an
Exception when attempting to insert the rows.public void cleanupAll() throws BigQueryResourceManagerException
cleanupAll
in interface org.apache.beam.it.common.ResourceManager
BigQueryResourceManagerException
- if there is an error deleting the tables or dataset in
BigQuery.