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
ConstructorsConstructorDescriptionBigQueryTemplate(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 theBigQuerytemplate. -
Method Summary
Modifier and TypeMethodDescriptioncom.google.cloud.bigquery.TablecreateTable(String tableName, com.google.cloud.bigquery.Schema schema) getBigQueryJsonDataWriter(com.google.cloud.bigquery.storage.v1.TableName parentTable) com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsResponsegetCommitResponse(com.google.cloud.bigquery.storage.v1.TableName parentTable, BigQueryJsonDataWriter writer) intgetWriteApiResponse(String tableName, InputStream jsonInputStream) voidsetAutoDetectSchema(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.voidsetCreateDisposition(com.google.cloud.bigquery.JobInfo.CreateDisposition createDisposition) Sets theJobInfo.CreateDispositionwhich specifies whether a new table may be created in BigQuery if needed.voidsetJobPollInterval(Duration jobPollInterval) Sets theDurationamount of time to wait between successive polls on the status of a BigQuery job.voidsetWriteDisposition(com.google.cloud.bigquery.JobInfo.WriteDisposition writeDisposition) Sets theJobInfo.WriteDispositionwhich 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 theBigQuerytemplate.- 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- theTaskSchedulerused 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.WriteDispositionwhich 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_APPENDto append data to a table.
-
setCreateDisposition
public void setCreateDisposition(com.google.cloud.bigquery.JobInfo.CreateDisposition createDisposition) Sets theJobInfo.CreateDispositionwhich 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 theDurationamount of time to wait between successive polls on the status of a BigQuery job.- Parameters:
jobPollInterval- theDurationpoll 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:BigQueryOperationsWrites data to a specified BigQuery table.- Specified by:
writeDataToTablein 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:
CompletableFuturecontaining 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:BigQueryOperationsWrites 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:
writeDataToTablein 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:
CompletableFuturecontaining 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:
writeJsonStreamin interfaceBigQueryOperations- Parameters:
tableName- name of the table to write tojsonInputStream- input stream of the json file to be written- Returns:
CompletableFuturecontaining 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:
writeJsonStreamin interfaceBigQueryOperations- Parameters:
tableName- name of the table to write tojsonInputStream- input stream of the json file to be written- Returns:
CompletableFuturecontaining 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.DescriptorValidationExceptionIOExceptionInterruptedException
-
getWriteApiResponse
public WriteApiResponse getWriteApiResponse(String tableName, InputStream jsonInputStream) throws com.google.protobuf.Descriptors.DescriptorValidationException, IOException, InterruptedException - Throws:
com.google.protobuf.Descriptors.DescriptorValidationExceptionIOExceptionInterruptedException
-
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()
-