Class Table
- All Implemented Interfaces:
Serializable
Objects of this class are immutable. Operations that modify the table like update(com.google.cloud.bigquery.BigQuery.TableOption...)
return a new object. To get a Table object with the most recent information use reload(com.google.cloud.bigquery.BigQuery.TableOption...). Table adds a layer of service-related functionality over TableInfo.
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptioncopy(TableId destinationTable, BigQuery.JobOption... options) Starts a BigQuery Job to copy the current table to the provided destination table.copy(String destinationDataset, String destinationTable, BigQuery.JobOption... options) Starts a BigQuery Job to copy the current table to the provided destination table.booleandelete()Deletes this table.final booleanbooleanexists()Checks if this table exists.extract(String format, String destinationUri, BigQuery.JobOption... options) Starts a BigQuery Job to extract the current table to the provided destination URI.extract(String format, List<String> destinationUris, BigQuery.JobOption... options) Starts a BigQuery Job to extract the current table to the provided destination URIs.Returns the table'sBigQueryobject used to issue requests.final inthashCode()Insert rows into the table.insert(Iterable<InsertAllRequest.RowToInsert> rows, boolean skipInvalidRows, boolean ignoreUnknownValues) Insert rows into the table.list(BigQuery.TableDataListOption... options) Returns the paginated list rows in this table.list(Schema schema, BigQuery.TableDataListOption... options) Returns the paginated list rows in this table.load(FormatOptions format, String sourceUri, BigQuery.JobOption... options) Starts a BigQuery Job to load data into the current table from the provided source URI.load(FormatOptions format, List<String> sourceUris, BigQuery.JobOption... options) Starts a BigQuery Job to load data into the current table from the provided source URIs.reload(BigQuery.TableOption... options) Fetches current table's latest information.Returns a builder for the table object.update(BigQuery.TableOption... options) Updates the table's information with this table's information.Methods inherited from class com.google.cloud.bigquery.TableInfo
getCloneDefinition, getCreationTime, getDefaultCollation, getDefinition, getDescription, getEncryptionConfiguration, getEtag, getExpirationTime, getFriendlyName, getGeneratedId, getLabels, getLastModifiedTime, getNumActiveLogicalBytes, getNumActivePhysicalBytes, getNumBytes, getNumLongTermBytes, getNumLongTermLogicalBytes, getNumLongTermPhysicalBytes, getNumRows, getNumTimeTravelPhysicalBytes, getNumTotalLogicalBytes, getNumTotalPhysicalBytes, getRequirePartitionFilter, getResourceTags, getSelfLink, getTableConstraints, getTableId, newBuilder, of, toString
-
Method Details
-
exists
public boolean exists()Checks if this table exists.Example of checking if the table exists.
boolean exists = table.exists(); if (exists) { // the table exists } else { // the table was not found }- Returns:
trueif this table exists,falseotherwise- Throws:
BigQueryException- upon failure
-
reload
Fetches current table's latest information. Returnsnullif the table does not exist.Example of fetching the table's latest information, specifying particular table fields to get.
TableField field1 = TableField.LAST_MODIFIED_TIME; TableField field2 = TableField.NUM_ROWS; Table latestTable = table.reload(TableOption.fields(field1, field2)); if (latestTable == null) { // the table was not found }- Parameters:
options- table options- Returns:
- a
Tableobject with latest information ornullif not found - Throws:
BigQueryException- upon failure
-
update
Updates the table's information with this table's information. Dataset's and table's user-defined ids cannot be changed. A newTableobject is returned.Example of updating the table's information.
Table updatedTable = table.toBuilder().setDescription("new description").build().update();- Parameters:
options- dataset options- Returns:
- a
Tableobject with updated information - Throws:
BigQueryException- upon failure
-
delete
public boolean delete()Deletes this table.Example of deleting the table.
boolean deleted = table.delete(); if (deleted) { // the table was deleted } else { // the table was not found }- Returns:
trueif table was deleted,falseif it was not found- Throws:
BigQueryException- upon failure
-
insert
public InsertAllResponse insert(Iterable<InsertAllRequest.RowToInsert> rows) throws BigQueryException Insert rows into the table.Streaming inserts reside temporarily in the streaming buffer, which has different availability characteristics than managed storage. Certain operations do not interact with the streaming buffer, such as
list(TableDataListOption...)andcopy(TableId, JobOption...). As such, recent streaming data will not be present in the destination table or output.Example of inserting rows into the table.
String rowId1 = "rowId1"; String rowId2 = "rowId2"; List<RowToInsert> rows = new ArrayList<>(); Map<String, Object> row1 = new HashMap<>(); row1.put("stringField", "value1"); row1.put("booleanField", true); Map<String, Object> row2 = new HashMap<>(); row2.put("stringField", "value2"); row2.put("booleanField", false); rows.add(RowToInsert.of(rowId1, row1)); rows.add(RowToInsert.of(rowId2, row2)); InsertAllResponse response = table.insert(rows); // do something with response- Parameters:
rows- rows to be inserted- Throws:
BigQueryException- upon failure
-
insert
public InsertAllResponse insert(Iterable<InsertAllRequest.RowToInsert> rows, boolean skipInvalidRows, boolean ignoreUnknownValues) throws BigQueryException Insert rows into the table.Streaming inserts reside temporarily in the streaming buffer, which has different availability characteristics than managed storage. Certain operations do not interact with the streaming buffer, such as
list(TableDataListOption...)andcopy(TableId, JobOption...). As such, recent streaming data will not be present in the destination table or output.Example of inserting rows into the table, ignoring invalid rows.
String rowId1 = "rowId1"; String rowId2 = "rowId2"; List<RowToInsert> rows = new ArrayList<>(); Map<String, Object> row1 = new HashMap<>(); row1.put("stringField", 1); row1.put("booleanField", true); Map<String, Object> row2 = new HashMap<>(); row2.put("stringField", "value2"); row2.put("booleanField", false); rows.add(RowToInsert.of(rowId1, row1)); rows.add(RowToInsert.of(rowId2, row2)); InsertAllResponse response = table.insert(rows, true, true); // do something with response- Parameters:
rows- rows to be insertedskipInvalidRows- whether to insert all valid rows, even if invalid rows exist. If not set the entire insert operation will fail if rows to be inserted contain an invalid rowignoreUnknownValues- whether to accept rows that contain values that do not match the schema. The unknown values are ignored. If not set, rows with unknown values are considered to be invalid- Throws:
BigQueryException- upon failure
-
list
Returns the paginated list rows in this table.Example of listing rows in the table.
// This example reads the result 100 rows per RPC call. If there's no need to limit the number, // simply omit the option. Page<FieldValueList> page = table.list(TableDataListOption.pageSize(100)); for (FieldValueList row : page.iterateAll()) { // do something with the row }- Parameters:
options- table data list options- Throws:
BigQueryException- upon failure
-
list
public TableResult list(Schema schema, BigQuery.TableDataListOption... options) throws BigQueryException Returns the paginated list rows in this table.Example of listing rows in the table given a schema.
Schema schema = ...; String field = "my_field"; Page<FieldValueList> page = table.list(schema); for (FieldValueList row : page.iterateAll()) { row.get(field); }- Parameters:
options- table data list options- Throws:
BigQueryException- upon failure
-
copy
public Job copy(String destinationDataset, String destinationTable, BigQuery.JobOption... options) throws BigQueryException Starts a BigQuery Job to copy the current table to the provided destination table. Returns the startedJobobject.Example of copying the table to a destination table.
String datasetName = "my_dataset"; String tableName = "my_destination_table"; Job job = table.copy(datasetName, tableName); // Wait for the job to complete. try { Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait }- Parameters:
destinationDataset- the user-defined id of the destination datasetdestinationTable- the user-defined id of the destination tableoptions- job options- Throws:
BigQueryException- upon failure
-
copy
Starts a BigQuery Job to copy the current table to the provided destination table. Returns the startedJobobject.Example copying the table to a destination table.
String dataset = "my_dataset"; String tableName = "my_destination_table"; TableId destinationId = TableId.of(dataset, tableName); JobOption options = JobOption.fields(JobField.STATUS, JobField.USER_EMAIL); Job job = table.copy(destinationId, options); // Wait for the job to complete. try { Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully. } else { // Handle error case. } } catch (InterruptedException e) { // Handle interrupted wait }- Parameters:
destinationTable- the destination table of the copy joboptions- job options- Throws:
BigQueryException- upon failure
-
extract
public Job extract(String format, String destinationUri, BigQuery.JobOption... options) throws BigQueryException Starts a BigQuery Job to extract the current table to the provided destination URI. Returns the startedJobobject.Example extracting data to single Google Cloud Storage file.
String format = "CSV"; String gcsUrl = "gs://my_bucket/filename.csv"; Job job = table.extract(format, gcsUrl); // Wait for the job to complete try { Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait }- Parameters:
format- the format of the extracted datadestinationUri- the fully-qualified Google Cloud Storage URI (e.g. gs://bucket/path) where the extracted table should be writtenoptions- job options- Throws:
BigQueryException- upon failure
-
extract
public Job extract(String format, List<String> destinationUris, BigQuery.JobOption... options) throws BigQueryException Starts a BigQuery Job to extract the current table to the provided destination URIs. Returns the startedJobobject.Example of partitioning data to a list of Google Cloud Storage files.
String format = "CSV"; String gcsUrl1 = "gs://my_bucket/PartitionA_*.csv"; String gcsUrl2 = "gs://my_bucket/PartitionB_*.csv"; List<String> destinationUris = new ArrayList<>(); destinationUris.add(gcsUrl1); destinationUris.add(gcsUrl2); Job job = table.extract(format, destinationUris); // Wait for the job to complete try { Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait }- Parameters:
format- the format of the exported datadestinationUris- the fully-qualified Google Cloud Storage URIs (e.g. gs://bucket/path) where the extracted table should be writtenoptions- job options- Throws:
BigQueryException- upon failure
-
load
public Job load(FormatOptions format, String sourceUri, BigQuery.JobOption... options) throws BigQueryException Starts a BigQuery Job to load data into the current table from the provided source URI. Returns the startedJobobject.Example loading data from a single Google Cloud Storage file.
String sourceUri = "gs://my_bucket/filename.csv"; Job job = table.load(FormatOptions.csv(), sourceUri); // Wait for the job to complete try { Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait }- Parameters:
format- the format of the data to loadsourceUri- the fully-qualified Google Cloud Storage URI (e.g. gs://bucket/path) from which to load the dataoptions- job options- Throws:
BigQueryException- upon failure
-
load
public Job load(FormatOptions format, List<String> sourceUris, BigQuery.JobOption... options) throws BigQueryException Starts a BigQuery Job to load data into the current table from the provided source URIs. Returns the startedJobobject.Example loading data from a list of Google Cloud Storage files.
String gcsUrl1 = "gs://my_bucket/filename1.csv"; String gcsUrl2 = "gs://my_bucket/filename2.csv"; List<String> sourceUris = new ArrayList<>(); sourceUris.add(gcsUrl1); sourceUris.add(gcsUrl2); Job job = table.load(FormatOptions.csv(), sourceUris); // Wait for the job to complete try { Job completedJob = job.waitFor(RetryOption.initialRetryDelay(Duration.ofSeconds(1)), RetryOption.totalTimeout(Duration.ofMinutes(3))); if (completedJob != null && completedJob.getStatus().getError() == null) { // Job completed successfully } else { // Handle error case } } catch (InterruptedException e) { // Handle interrupted wait }- Parameters:
format- the format of the exported datasourceUris- the fully-qualified Google Cloud Storage URIs (e.g. gs://bucket/path) from which to load the dataoptions- job options- Throws:
BigQueryException- upon failure
-
getBigQuery
Returns the table'sBigQueryobject used to issue requests. -
toBuilder
Description copied from class:TableInfoReturns a builder for the table object. -
equals
-
hashCode
public final int hashCode()
-