0

Is it possible to delete a table available in bigQuery using Apache beam using Java?

p.apply("Delete Table name", BigQueryIO.readTableRows().fromQuery("DELETE FROM Table_name where condition"));
Tamir Klein
  • 3,514
  • 1
  • 20
  • 38
Pirama
  • 11
  • 5

3 Answers3

1

I resolved this using BQ API.

BigQuery bigquery = BigQueryOptions.newBuilder().setCredentials(credential).setProjectId(ProjectName).build().getService(); QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(Query).setUseLegacySql(false).build(); JobId jobId = JobId.of(UUID.randomUUID().toString()); Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());

Pirama
  • 11
  • 5
0

import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions

# Define the project ID and table ID
project_id = 'your-project-id'
dataset_id = 'your-dataset-id'
table_id = 'your-table-id'

# Create a PipelineOptions object
options = PipelineOptions()
options.view_as(beam.options.pipeline_options.GoogleCloudOptions).project = project_id

# Delete the BigQuery table
with beam.Pipeline(options=options) as p:
    # Define a dummy element to create a PCollection
    dummy_element = p | 'Create dummy element' >> beam.Create([None])
    
    # Delete the BigQuery table
    dummy_element | 'Delete BigQuery table' >> beam.io.WriteToBigQuery(
        table=table_id,
        dataset=dataset_id,
        project=project_id,
        create_disposition=beam.io.BigQueryDisposition.CREATE_NEVER,
        write_disposition=beam.io.BigQueryDisposition.WRITE_EMPTY
    )
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 24 '23 at 12:46
0

import apache_beam as beam

project_id = 'your-project-id'
dataset_id = 'your-dataset-id'
table_id = 'your-table-id'

table_spec = f'{project_id}:{dataset_id}.{table_id}'

# Define a pipeline
pipeline = beam.Pipeline()

# Delete the BigQuery table
(
    pipeline
    | 'Create dummy data' >> beam.Create([{}])
    | 'Delete BigQuery table' >> beam.io.WriteToBigQuery(
        table_spec,
        write_disposition=beam.io.BigQueryDisposition.DELETE,
        create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED
    )
)

# Run the pipeline
pipeline.run()