com.google.cloud.spanner.Options Java Examples

The following examples show how to use com.google.cloud.spanner.Options. 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: SpannerReadOptions.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * In many cases a {@link SpannerReadOptions} class instance could be compatible with {@link SpannerQueryOptions}.
 * The method executes such conversion or throws an exception if it's impossible.
 * @return query-parameters
 * @throws IllegalArgumentException when {@link SpannerQueryOptions} can't be converted to {@link SpannerQueryOptions}.
 */
public SpannerQueryOptions toQueryOptions() {
	SpannerQueryOptions query = new SpannerQueryOptions();
	query.setAllowPartialRead(this.isAllowPartialRead());
	query.setIncludeProperties(this.getIncludeProperties());
	query.setTimestampBound(this.getTimestampBound());

	for (ReadOption ro : this.getOptions()) {
		if (ro instanceof Options.ReadAndQueryOption) {
			query.addQueryOption((Options.ReadAndQueryOption) ro);
		}
		else {
			throw new IllegalArgumentException(String.format("Can't convert %s to SpannerQueryOptions ", this));
		}
	}
	return query;
}
 
Example #2
Source File: SpannerSample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
static void listBackupOperations(InstanceAdminClient instanceAdminClient, DatabaseId databaseId) {
  Instance instance = instanceAdminClient.getInstance(databaseId.getInstanceId().getInstance());
  // Get create backup operations for the sample database.
  String filter =
      String.format(
          "(metadata.database:%s) AND "
              + "(metadata.@type:type.googleapis.com/"
              + "google.spanner.admin.database.v1.CreateBackupMetadata)",
          databaseId.getName());
  Page<Operation> operations = instance.listBackupOperations(Options.filter(filter));
  for (Operation op : operations.iterateAll()) {
    try {
      CreateBackupMetadata metadata = op.getMetadata().unpack(CreateBackupMetadata.class);
      System.out.println(
          String.format(
              "Backup %s on database %s pending: %d%% complete",
              metadata.getName(),
              metadata.getDatabase(),
              metadata.getProgress().getProgressPercent()));
    } catch (InvalidProtocolBufferException e) {
      // The returned operation does not contain CreateBackupMetadata.
      System.err.println(e.getMessage());
    }
  }
}
 
Example #3
Source File: SpannerSample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
static void listDatabaseOperations(
    InstanceAdminClient instanceAdminClient,
    DatabaseAdminClient dbAdminClient,
    InstanceId instanceId) {
  Instance instance = instanceAdminClient.getInstance(instanceId.getInstance());
  // Get optimize restored database operations.
  String filter = "(metadata.@type:type.googleapis.com/"
                  + "google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata)";
  for (Operation op : instance.listDatabaseOperations(Options.filter(filter)).iterateAll()) {
    try {
      OptimizeRestoredDatabaseMetadata metadata =
          op.getMetadata().unpack(OptimizeRestoredDatabaseMetadata.class);
      System.out.println(String.format(
          "Database %s restored from backup is %d%% optimized",
          metadata.getName(),
          metadata.getProgress().getProgressPercent()));
    } catch (InvalidProtocolBufferException e) {
      // The returned operation does not contain OptimizeRestoredDatabaseMetadata.
      System.err.println(e.getMessage());
    }
  }
}
 
Example #4
Source File: DatabaseAdminClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example to get the list of Cloud Spanner database in the given instance. */
// [TARGET listDatabases(String, ListOption...)]
// [VARIABLE my_instance_id]
public List<Database> listDatabases(String instanceId) {
  // [START listDatabases]
  Page<Database> page = dbAdminClient.listDatabases(instanceId, Options.pageSize(1));
  List<Database> dbs = new ArrayList<>();
  while (page != null) {
    Database db = Iterables.getOnlyElement(page.getValues());
    dbs.add(db);
    page = page.getNextPage();
  }
  // [END listDatabases]
  return dbs;
}
 
Example #5
Source File: InstanceAdminClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example to list instance configs. */
public List<InstanceConfig> listInstanceConfigs() {
  // [START instance_admin_client_list_configs]
  List<InstanceConfig> configs =
      Lists.newArrayList(
          instanceAdminClient.listInstanceConfigs(Options.pageSize(1)).iterateAll());
  // [END instance_admin_client_list_configs]
  return configs;
}
 
Example #6
Source File: InstanceAdminClientSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example to list instances. */
public List<Instance> listInstances() {
  // [START instance_admin_client_list_instances]
  List<Instance> instances =
      Lists.newArrayList(instanceAdminClient.listInstances(Options.pageSize(1)).iterateAll());
  // [END instance_admin_client_list_instances]
  return instances;
}
 
Example #7
Source File: SpannerPageableQueryOptions.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Override
public SpannerPageableQueryOptions addQueryOption(Options.QueryOption queryOption) {
	super.addQueryOption(queryOption);
	return this;
}