JVM - Generic
For use with the official BigQuery Java client
JVM/Java does not support a direct environment variable override for the host like Python/Node. However we show here a simple snippet that can be adapted to your needs, giving the exact same functionality with the same semantics. We recommend , if you are not already doing it as a best practice, that your BigQuery client initialisation code is kept in one place and referenced throughout your codebase.
As with Python and Node, ensure that the environment variable is set as such:
BIGQUERY_EMULATOR_HOST=https://<your proxy>.alvin.ai
Here is the code snippet (it's in Java, but modern editors from i.e jetbrains will automatically convert it to Kotlin if pasted).
package org.example;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
public class Main {
public static void main(String[] args) {
String host = System.getenv("BIGQUERY_EMULATOR_HOST");
if (host == null) {
host = "https://bigquery.googleapis.com";
}
BigQuery bigquery = BigQueryOptions.newBuilder()
.setProjectId("bigquery-public-data")
.setHost(host).build().getService();
// try to list datasets to validate it working
bigquery.listDatasets().iterateAll().forEach(dataset -> {
System.out.println(dataset.getDatasetId().getDataset());
});
}
}Last updated