Class BigQueryTemplate
java.lang.Object
com.google.cloud.spring.bigquery.core.BigQueryTemplate
- All Implemented Interfaces:
BigQueryOperations
Helper class which simplifies common operations done in BigQuery.
- Since:
- 1.2
-
Constructor Summary
ConstructorDescriptionBigQueryTemplate
(com.google.cloud.bigquery.BigQuery bigQuery, com.google.cloud.bigquery.storage.v1.BigQueryWriteClient bigQueryWriteClient, Map<String, Object> bqInitSettings, org.springframework.scheduling.TaskScheduler taskScheduler) A Full constructor which creates theBigQuery
template. -
Method Summary
Modifier and TypeMethodDescriptioncom.google.cloud.bigquery.Table
createTable
(String tableName, com.google.cloud.bigquery.Schema schema) getBigQueryJsonDataWriter
(com.google.cloud.bigquery.storage.v1.TableName parentTable) com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsResponse
getCommitResponse
(com.google.cloud.bigquery.storage.v1.TableName parentTable, BigQueryJsonDataWriter writer) int
getWriteApiResponse
(String tableName, InputStream jsonInputStream) void
setAutoDetectSchema
(boolean autoDetectSchema) Sets whether BigQuery should attempt to autodetect the schema of the data when loading data into an empty table for the first time.void
setCreateDisposition
(com.google.cloud.bigquery.JobInfo.CreateDisposition createDisposition) Sets theJobInfo.CreateDisposition
which specifies whether a new table may be created in BigQuery if needed.void
setJobPollInterval
(Duration jobPollInterval) Sets theDuration
amount of time to wait between successive polls on the status of a BigQuery job.void
setWriteDisposition
(com.google.cloud.bigquery.JobInfo.WriteDisposition writeDisposition) Sets theJobInfo.WriteDisposition
which specifies how data should be inserted into BigQuery tables.CompletableFuture<com.google.cloud.bigquery.Job>
writeDataToTable
(String tableName, InputStream inputStream, com.google.cloud.bigquery.FormatOptions dataFormatOptions) Writes data to a specified BigQuery table.CompletableFuture<com.google.cloud.bigquery.Job>
writeDataToTable
(String tableName, InputStream inputStream, com.google.cloud.bigquery.FormatOptions dataFormatOptions, com.google.cloud.bigquery.Schema schema) Writes data to a specified BigQuery table with a manually-specified table Schema.writeJsonStream
(String tableName, InputStream jsonInputStream) This method uses BigQuery Storage Write API to write new line delimited JSON file to the specified table.writeJsonStream
(String tableName, InputStream jsonInputStream, com.google.cloud.bigquery.Schema schema) This method uses BigQuery Storage Write API to write new line delimited JSON file to the specified table.
-
Constructor Details
-
BigQueryTemplate
public BigQueryTemplate(com.google.cloud.bigquery.BigQuery bigQuery, com.google.cloud.bigquery.storage.v1.BigQueryWriteClient bigQueryWriteClient, Map<String, Object> bqInitSettings, org.springframework.scheduling.TaskScheduler taskScheduler) A Full constructor which creates theBigQuery
template.- Parameters:
bigQuery
- the underlying client object used to interface with BigQuerybigQueryWriteClient
- the underlying BigQueryWriteClient reference use to connect with BigQuery Storage Write ClientbqInitSettings
- Properties required for initialisation of this classtaskScheduler
- theTaskScheduler
used to poll for the status of long-running BigQuery operations
-
-
Method Details
-
setAutoDetectSchema
public void setAutoDetectSchema(boolean autoDetectSchema) Sets whether BigQuery should attempt to autodetect the schema of the data when loading data into an empty table for the first time. If set to false, the schema must be defined explicitly for the table before load.- Parameters:
autoDetectSchema
- whether data schema should be autodetected from the structure of the data. Default is true.
-
setWriteDisposition
public void setWriteDisposition(com.google.cloud.bigquery.JobInfo.WriteDisposition writeDisposition) Sets theJobInfo.WriteDisposition
which specifies how data should be inserted into BigQuery tables.- Parameters:
writeDisposition
- whether to append to or truncate (overwrite) data in the BigQuery table. Default isWriteDisposition.WRITE_APPEND
to append data to a table.
-
setCreateDisposition
public void setCreateDisposition(com.google.cloud.bigquery.JobInfo.CreateDisposition createDisposition) Sets theJobInfo.CreateDisposition
which specifies whether a new table may be created in BigQuery if needed.- Parameters:
createDisposition
- whether to never create a new table in the BigQuery table or only if needed.
-
setJobPollInterval
Sets theDuration
amount of time to wait between successive polls on the status of a BigQuery job.- Parameters:
jobPollInterval
- theDuration
poll interval for BigQuery job status polling
-
writeDataToTable
public CompletableFuture<com.google.cloud.bigquery.Job> writeDataToTable(String tableName, InputStream inputStream, com.google.cloud.bigquery.FormatOptions dataFormatOptions) Description copied from interface:BigQueryOperations
Writes data to a specified BigQuery table.- Specified by:
writeDataToTable
in interfaceBigQueryOperations
- Parameters:
tableName
- name of the table to write toinputStream
- input stream of the table data to writedataFormatOptions
- the format of the data to write- Returns:
CompletableFuture
containing the BigQuery Job indicating completion of operation
-
writeDataToTable
public CompletableFuture<com.google.cloud.bigquery.Job> writeDataToTable(String tableName, InputStream inputStream, com.google.cloud.bigquery.FormatOptions dataFormatOptions, com.google.cloud.bigquery.Schema schema) Description copied from interface:BigQueryOperations
Writes data to a specified BigQuery table with a manually-specified table Schema.Example:
Schema schema = Schema.of( Field.of("CountyId", StandardSQLTypeName.INT64), Field.of("State", StandardSQLTypeName.STRING), Field.of("County", StandardSQLTypeName.STRING) ); CompletableFuture<Job> bigQueryJobFuture = bigQueryTemplate.writeDataToTable( TABLE_NAME, dataFile.getInputStream(), FormatOptions.csv(), schema);
- Specified by:
writeDataToTable
in interfaceBigQueryOperations
- Parameters:
tableName
- name of the table to write toinputStream
- input stream of the table data to writedataFormatOptions
- the format of the data to writeschema
- the schema of the table being loaded- Returns:
CompletableFuture
containing the BigQuery Job indicating completion of operation
-
writeJsonStream
public CompletableFuture<WriteApiResponse> writeJsonStream(String tableName, InputStream jsonInputStream, com.google.cloud.bigquery.Schema schema) This method uses BigQuery Storage Write API to write new line delimited JSON file to the specified table. This method creates a table with the specified schema.- Specified by:
writeJsonStream
in interfaceBigQueryOperations
- Parameters:
tableName
- name of the table to write tojsonInputStream
- input stream of the json file to be written- Returns:
CompletableFuture
containing the WriteApiResponse indicating completion of operation
-
createTable
public com.google.cloud.bigquery.Table createTable(String tableName, com.google.cloud.bigquery.Schema schema) -
writeJsonStream
public CompletableFuture<WriteApiResponse> writeJsonStream(String tableName, InputStream jsonInputStream) This method uses BigQuery Storage Write API to write new line delimited JSON file to the specified table. The Table should already be created as BigQuery Storage Write API doesn't create it automatically.- Specified by:
writeJsonStream
in interfaceBigQueryOperations
- Parameters:
tableName
- name of the table to write tojsonInputStream
- input stream of the json file to be written- Returns:
CompletableFuture
containing the WriteApiResponse indicating completion of operation
-
getBigQueryJsonDataWriter
public BigQueryJsonDataWriter getBigQueryJsonDataWriter(com.google.cloud.bigquery.storage.v1.TableName parentTable) throws com.google.protobuf.Descriptors.DescriptorValidationException, IOException, InterruptedException - Throws:
com.google.protobuf.Descriptors.DescriptorValidationException
IOException
InterruptedException
-
getWriteApiResponse
public WriteApiResponse getWriteApiResponse(String tableName, InputStream jsonInputStream) throws com.google.protobuf.Descriptors.DescriptorValidationException, IOException, InterruptedException - Throws:
com.google.protobuf.Descriptors.DescriptorValidationException
IOException
InterruptedException
-
getCommitResponse
public com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsResponse getCommitResponse(com.google.cloud.bigquery.storage.v1.TableName parentTable, BigQueryJsonDataWriter writer) -
getDatasetName
-
getJsonWriterBatchSize
public int getJsonWriterBatchSize()
-