rish0097
rish0097

Reputation: 1094

Write to a dynamic BigQuery table through Apache Beam

I am getting the BigQuery table name at runtime and I pass that name to the BigQueryIO.write operation at the end of my pipeline to write to that table.

The code that I've written for it is:

rows.apply("write to BigQuery", BigQueryIO
    .writeTableRows()
    .withSchema(schema)
    .to("projectID:DatasetID."+tablename)
    .withWriteDisposition(WriteDisposition.WRITE_TRUNCATE)
    .withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED));

With this syntax I always get an error,

Exception in thread "main" java.lang.IllegalArgumentException: Table reference is not in [project_id]:[dataset_id].[table_id] format

How to pass the table name with the correct format when I don't know before hand which table it should put the data in? Any suggestions?

Thank You

Upvotes: 4

Views: 2115

Answers (1)

Campey
Campey

Reputation: 1158

Very late to the party on this however. I suspect the issue is you were passing in a string not a table reference.

If you created a table reference I suspect you'd have no issues with the above code.

com.google.api.services.bigquery.model.TableReference table = new TableReference()
            .setProjectId(projectID)
            .setDatasetId(DatasetID)
            .setTableId(tablename);

rows.apply("write to BigQuery", BigQueryIO
    .writeTableRows()
    .withSchema(schema)
    .to(table)
    .withWriteDisposition(WriteDisposition.WRITE_TRUNCATE)
    .withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED));

Upvotes: 3

Related Questions