Java Code Examples for com.google.cloud.spanner.SpannerOptions#newBuilder()

The following examples show how to use com.google.cloud.spanner.SpannerOptions#newBuilder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudSpannerIT.java    From spanner-jdbc with MIT License 6 votes vote down vote up
public CloudSpannerIT() {
  // generate a unique instance id for this test run
  Random rnd = new Random();
  this.instanceId = "test-instance-" + rnd.nextInt(1000000);
  this.credentialsPath = getKeyFile();
  GoogleCredentials credentials = null;
  try {
    credentials = CloudSpannerConnection.getCredentialsFromFile(credentialsPath);
  } catch (IOException e) {
    throw new RuntimeException("Could not read key file " + credentialsPath, e);
  }
  Builder builder = SpannerOptions.newBuilder();
  builder.setProjectId(getProject());
  builder.setCredentials(credentials);
  builder.setHost(getHost());

  SpannerOptions options = builder.build();
  spanner = options.getService();
}
 
Example 2
Source File: InstanceConfigIT.java    From spanner-jdbc with MIT License 6 votes vote down vote up
@Test
public void testEuropeWestSingleNodeConfig() {
  String credentialsPath = "cloudspanner-emulator-key.json";
  String projectId = "test-project";
  GoogleCredentials credentials = null;
  try {
    credentials = CloudSpannerConnection.getCredentialsFromFile(credentialsPath);
  } catch (IOException e) {
    throw new RuntimeException("Could not read key file " + credentialsPath, e);
  }
  Builder builder = SpannerOptions.newBuilder();
  builder.setProjectId(projectId);
  builder.setCredentials(credentials);
  builder.setHost(CloudSpannerIT.getHost());

  SpannerOptions options = builder.build();
  Spanner spanner = options.getService();

  InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
  InstanceConfig config = instanceAdminClient.getInstanceConfig("regional-europe-west1");
  assertEquals("regional-europe-west1", config.getId().getInstanceConfig());
  spanner.close();
}
 
Example 3
Source File: CloudSpannerDriver.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private Spanner createSpanner(SpannerKey key) {
  Builder builder = SpannerOptions.newBuilder();
  if (key.projectId != null)
    builder.setProjectId(key.projectId);
  if (key.credentials != null)
    builder.setCredentials(key.credentials);
  else if (!hasDefaultCredentials())
    builder.setCredentials(NoCredentials.getInstance());
  if (key.host != null)
    builder.setHost(key.host);
  SpannerOptions options = builder.build();
  return options.getService();
}
 
Example 4
Source File: AbstractSpecificIntegrationTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private static void createSpanner() throws IOException {
  // generate a unique instance id for this test run
  Random rnd = new Random();
  instanceId = "test-instance-" + rnd.nextInt(1000000);
  credentialsPath = getKeyFile();
  projectId = getProject();
  GoogleCredentials credentials = CloudSpannerConnection.getCredentialsFromFile(credentialsPath);
  Builder builder = SpannerOptions.newBuilder();
  builder.setProjectId(projectId);
  builder.setCredentials(credentials);
  builder.setHost(getHost());

  SpannerOptions options = builder.build();
  spanner = options.getService();
}
 
Example 5
Source File: ExposedSpannerAccessor.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
public static ExposedSpannerAccessor create(SpannerConfig spannerConfig) {
  SpannerOptions.Builder builder = SpannerOptions.newBuilder();

  ValueProvider<Duration> commitDeadline = spannerConfig.getCommitDeadline();
  if (commitDeadline != null && commitDeadline.get().getMillis() > 0) {

    // In Spanner API version 1.21 or above, we can set the deadline / total Timeout on an API
    // call using the following code:
    //
    // UnaryCallSettings.Builder commitSettings =
    // builder.getSpannerStubSettingsBuilder().commitSettings();
    // RetrySettings.Builder commitRetrySettings = commitSettings.getRetrySettings().toBuilder()
    // commitSettings.setRetrySettings(
    //     commitRetrySettings.setTotalTimeout(
    //         Duration.ofMillis(getCommitDeadlineMillis().get()))
    //     .build());
    //
    // However, at time of this commit, the Spanner API is at only at v1.6.0, where the only
    // method to set a deadline is with GRPC Interceptors, so we have to use that...
    SpannerInterceptorProvider interceptorProvider =
        SpannerInterceptorProvider.createDefault()
            .with(new CommitDeadlineSettingInterceptor(commitDeadline.get()));
    builder.setInterceptorProvider(interceptorProvider);
  }

  ValueProvider<String> projectId = spannerConfig.getProjectId();
  if (projectId != null) {
    builder.setProjectId(projectId.get());
  }
  ServiceFactory<Spanner, SpannerOptions> serviceFactory = spannerConfig.getServiceFactory();
  if (serviceFactory != null) {
    builder.setServiceFactory(serviceFactory);
  }
  ValueProvider<String> host = spannerConfig.getHost();
  if (host != null) {
    builder.setHost(host.get());
  }
  String userAgentString = USER_AGENT_PREFIX + "/" + ReleaseInfo.getReleaseInfo().getVersion();
  builder.setHeaderProvider(FixedHeaderProvider.create("user-agent", userAgentString));

  SessionPoolOptions.Builder sessionPoolOptions = SessionPoolOptions.newBuilder();
  sessionPoolOptions.setMinSessions(1);
  sessionPoolOptions.setMaxSessions(3);
  builder.setSessionPoolOption(sessionPoolOptions.build());

  SpannerOptions options = builder.build();

  Spanner spanner = options.getService();
  String instanceId = spannerConfig.getInstanceId().get();
  String databaseId = spannerConfig.getDatabaseId().get();
  DatabaseClient databaseClient =
      spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
  BatchClient batchClient =
      spanner.getBatchClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
  DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();

  return new ExposedSpannerAccessor(spanner, databaseClient, databaseAdminClient, batchClient);
}
 
Example 6
Source File: SpannerAccessor.java    From beam with Apache License 2.0 4 votes vote down vote up
private static SpannerAccessor createAndConnect(SpannerConfig spannerConfig) {
  SpannerOptions.Builder builder = SpannerOptions.newBuilder();

  ValueProvider<Duration> commitDeadline = spannerConfig.getCommitDeadline();
  if (commitDeadline != null && commitDeadline.get().getMillis() > 0) {

    // Set the GRPC deadline on the Commit API call.
    UnaryCallSettings.Builder commitSettings =
        builder.getSpannerStubSettingsBuilder().commitSettings();
    RetrySettings.Builder commitRetrySettings = commitSettings.getRetrySettings().toBuilder();
    commitSettings.setRetrySettings(
        commitRetrySettings
            .setTotalTimeout(org.threeten.bp.Duration.ofMillis(commitDeadline.get().getMillis()))
            .setMaxRpcTimeout(org.threeten.bp.Duration.ofMillis(commitDeadline.get().getMillis()))
            .setInitialRpcTimeout(
                org.threeten.bp.Duration.ofMillis(commitDeadline.get().getMillis()))
            .build());
  }

  ValueProvider<String> projectId = spannerConfig.getProjectId();
  if (projectId != null) {
    builder.setProjectId(projectId.get());
  }
  ServiceFactory<Spanner, SpannerOptions> serviceFactory = spannerConfig.getServiceFactory();
  if (serviceFactory != null) {
    builder.setServiceFactory(serviceFactory);
  }
  ValueProvider<String> host = spannerConfig.getHost();
  if (host != null) {
    builder.setHost(host.get());
  }
  String userAgentString = USER_AGENT_PREFIX + "/" + ReleaseInfo.getReleaseInfo().getVersion();
  builder.setHeaderProvider(FixedHeaderProvider.create("user-agent", userAgentString));
  SpannerOptions options = builder.build();

  Spanner spanner = options.getService();
  String instanceId = spannerConfig.getInstanceId().get();
  String databaseId = spannerConfig.getDatabaseId().get();
  DatabaseClient databaseClient =
      spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
  BatchClient batchClient =
      spanner.getBatchClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
  DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();

  return new SpannerAccessor(
      spanner, databaseClient, databaseAdminClient, batchClient, spannerConfig);
}