Reputation: 51
We are using Bigquery API https://github.com/googleapis/java-bigquery I am trying to perform the update operation using the below code snippet:
String dmlQuery = String.format("update dataset.Student set Stud_Name = \"Shruti11\", Stud_Class=\"II\" where Stud_Id = 300", datasetName, tableName);
QueryJobConfiguration dmlQueryConfig =
QueryJobConfiguration.newBuilder(dmlQuery).build();
// Execute the query.
TableResult result = bigquery.query(dmlQueryConfig);
System.out.println("Total rows affected :" + result.getTotalRows());
System.out.println("Table updated successfully using DML");
here in the above code, result.getTotalRows()
does not return any value in case of an update. It returns the no of rows in the select operation.
I am looking for an API that could tell me the number of rows updated in the Bigquery table.
While using the BigQuery V2 API, I was able to trace the number of rows updated using the queryResponse.getNumDmlAffectedRows()
Can you tell me the similar API here? We need to know the number of Affected Rows.
Also, in case of any errors, the BigQuery V2 API used to return the queryResponse.getErrors()
Can you help with this as well?
Env: Java 1.8
Google Cloud Bigquery
Thanks,
Upvotes: 1
Views: 1171
Reputation: 51
I have finally figured out.. how it works.. below is the code snippet.
public void query() throws InterruptedException {
String dmlQuery =
String.format(
"update exploreTest.Student set Stud_Name = \"Shruti11\", Stud_Class=\"II\" where Stud_Id = 300");
QueryJobConfiguration dmlQueryConfig = QueryJobConfiguration.newBuilder(dmlQuery).build();
// Execute the query.
Job job = bigQuery.create(JobInfo.of(dmlQueryConfig));
job = job.waitFor();
JobStatistics.QueryStatistics statistics = job.getStatistics();
TableResult result = job.getQueryResults();
long numDmlAffectedRows = statistics.getNumDmlAffectedRows();
System.out.println(numDmlAffectedRows);
}
Upvotes: 2