Skip to content

Instantly share code, notes, and snippets.

@hernanliendo
Created August 26, 2014 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hernanliendo/1bb6160eae0fc37e2510 to your computer and use it in GitHub Desktop.
Save hernanliendo/1bb6160eae0fc37e2510 to your computer and use it in GitHub Desktop.
Hackademy - BigQueryServerFactory
package ar.com.zupcat.lib.bean.audit;
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import java.util.Arrays;
public final class BigQueryServerFactory {
private static final Object LOCK_OBJECT = new Object();
private static BigQueryServerFactory _instace = null;
private final BigQueryClient bigQueryClientFactory;
private BigQueryServerFactory() {
final HttpRequestInitializer httpRequestInitializer;
if (System.getProperty("OAUTH_ACCESS_TOKEN") != null) {
// Good for testing and localhost environments, where no AppIdentity is available.
httpRequestInitializer = new GoogleCredential().setAccessToken(System.getenv("OAUTH_ACCESS_TOKEN"));
} else {
httpRequestInitializer = new AppIdentityCredential(Arrays.asList(BigqueryScopes.BIGQUERY, BigqueryScopes.BIGQUERY_INSERTDATA));
}
final String appName = AppIdentityServiceFactory.getAppIdentityService().getServiceAccountName().split("@")[0];
final Bigquery bq = new Bigquery.Builder(new UrlFetchTransport(), new JacksonFactory(), httpRequestInitializer).setApplicationName(appName).build();
bigQueryClientFactory = new BigQueryClient(appName, bq);
}
public static BigQueryServerFactory instance() {
if (_instace == null) {
synchronized (LOCK_OBJECT) {
if (_instace == null) {
_instace = new BigQueryServerFactory();
}
}
}
return _instace;
}
public BigQueryClient getBigQueryClientFactory() {
return bigQueryClientFactory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment