To use the Google Java client to load data into an existing BQ table, follow these steps:
Include the necessary libraries in your Java project by adding the following dependencies to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>1.35.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.32.1</version>
</dependency>
Create a BigQuery instance using the BigQueryOptions
class:
BigQuery bigquery = BigQueryOptions.newBuilder()
.setProjectId(projectId)
.build().getService();
Replace projectId
with the ID of the project containing the BQ table you want to load data into.
Create a TableId
instance for the table you want to load data into:
TableId tableId = TableId.of(projectId, datasetId, tableId);
Replace datasetId
with the ID of the dataset containing the table, and tableId
with the ID of the table.
Create a TableDataWriteChannel
instance to write data to the table:
TableDataWriteChannel writer = bigquery.writer().write(tableId);
OutputStream stream = Channels.newOutputStream(writer);
Serialize your data and write it to the OutputStream
:
String data = "[{\"name\":\"John\", \"age\": 30}, {\"name\":\"Jane\", \"age\": 25}]";
stream.write(data.getBytes());
Replace data
with the JSON representation of your data.
Close the channel to complete the upload:
writer.close();
Your data should now be uploaded to the BQ table.
Asked: 2022-11-18 11:00:00 +0000
Seen: 10 times
Last updated: Jan 16 '22