'using bigquery in spring boot microservice
I am referring to this documentation for running a query in my spring boot app: https://docs.spring.io/spring-cloud-gcp/docs/current/reference/html/bigquery.html
I am thinking of going on with something similar to this example they mentioned:
// BigQuery client object provided by our autoconfiguration.
@Autowired
BigQuery bigquery;
public void runQuery() throws InterruptedException {
String query = "SELECT column FROM table;";
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(query).build();
// Run the query using the BigQuery object
for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) {
for (FieldValue val : row) {
System.out.println(val);
}
}
}
However, there are some questions I have, they mentioned that The GcpBigQueryAutoConfiguration class configures an instance of BigQuery for you by inferring your credentials and Project ID from the machine’s environment, and also in the configuration section they mentioned spring.cloud.gcp.bigquery.datasetName is The BigQuery dataset that the BigQueryTemplate and BigQueryFileMessageHandler is scoped to and that it's required.
but what if I am having different projects and different datasets I want to use based on different conditions in my code, will it be enough to define the credential in the machine's environment, and use and in the part in which the query is defined, I can concentenate the project id and dataset name to the table to be something like this:
String query = "SELECT column FROM <project_id>+<dataset>+<table>;";
is this correct or should I do with better way?
EDIT: and the second question is, what if I need to apply where condition in the query should it will be easily done by string concatenation enough also? for example like this:
because I found some resource is dealing with it like this:
String query = "SELECT column FROM <project_id>+<dataset>+<table> where id =<ID>;";
final var queryJobConfiguration = QueryJobConfiguration
.newBuilder("SELECT * FROM " + tableId.getTable() + " WHERE id=@id")
.addNamedParameter("id", QueryParameterValue.numeric(BigDecimal.valueOf(userId)))
.setDefaultDataset(dataset)
.build();
I don't know why they used ```.addNamedParameter````
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
