Skip to content

Commit

Permalink
0004271: allow lazy load of google bigquery
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Feb 5, 2020
1 parent 6b86543 commit 6a95cce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
Expand Up @@ -24,7 +24,6 @@
import static org.apache.commons.lang.StringUtils.isNotBlank;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
Expand All @@ -47,7 +46,7 @@
import org.jumpmind.db.platform.DatabaseNamesConstants;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.platform.bigquery.BigQueryPlatform;
import org.jumpmind.db.platform.bigquery.BigQueryPlatformFactory;
import org.jumpmind.db.platform.cassandra.CassandraPlatform;
import org.jumpmind.db.platform.generic.GenericJdbcDatabasePlatform;
import org.jumpmind.db.platform.kafka.KafkaPlatform;
Expand Down Expand Up @@ -86,11 +85,6 @@
import org.springframework.jndi.JndiObjectFactoryBean;
import org.xml.sax.InputSource;

import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.http.HttpTransportOptions;

/**
* Represents the client portion of a SymmetricDS engine. This class can be used
* to embed SymmetricDS into another application.
Expand Down Expand Up @@ -326,22 +320,10 @@ public static IDatabasePlatform createDatabasePlatform(ApplicationContext spring
} else if (dbDriver != null && dbDriver.contains("kafka")) {
return new KafkaPlatform(createSqlTemplateSettings(properties));
} else if (dbUrl != null && dbUrl.startsWith("bigquery://")) {
try {
HttpTransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions();
transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000)
.build();

BigQuery bigquery = BigQueryOptions.newBuilder()
.setProjectId(properties.get(ParameterConstants.GOOGLE_BIG_QUERY_PROJECT_ID))
.setLocation(properties.get(ParameterConstants.GOOGLE_BIG_QUERY_LOCATION, "US"))
.setCredentials(ServiceAccountCredentials.fromStream(
new FileInputStream(properties.get(ParameterConstants.GOOGLE_BIG_QUERY_SECURITY_CREDENTIALS_PATH))))
.setTransportOptions(transportOptions)
.build().getService();
return new BigQueryPlatform(createSqlTemplateSettings(properties), bigquery);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new BigQueryPlatformFactory().createDatabasePlatform(createSqlTemplateSettings(properties),
properties.get(ParameterConstants.GOOGLE_BIG_QUERY_PROJECT_ID),
properties.get(ParameterConstants.GOOGLE_BIG_QUERY_LOCATION, "US"),
properties.get(ParameterConstants.GOOGLE_BIG_QUERY_SECURITY_CREDENTIALS_PATH));
}
}
String jndiName = properties.getProperty(ParameterConstants.DB_JNDI_NAME);
Expand Down
@@ -0,0 +1,32 @@
package org.jumpmind.db.platform.bigquery;

import java.io.FileInputStream;

import org.jumpmind.db.sql.SqlTemplateSettings;

import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.http.HttpTransportOptions;

public class BigQueryPlatformFactory {

public BigQueryPlatformFactory() {
}

public BigQueryPlatform createDatabasePlatform(SqlTemplateSettings settings, String projectId, String location, String filePath) {
try {
HttpTransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions();
transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();

BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId(projectId).setLocation(location)
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(filePath)))
.setTransportOptions(transportOptions).build().getService();

return new BigQueryPlatform(settings, bigquery);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 6a95cce

Please sign in to comment.