Java Code Examples for com.google.cloud.spanner.DatabaseId#of()

The following examples show how to use com.google.cloud.spanner.DatabaseId#of() . 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: CreateDatabase.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();

  try {

    DatabaseId db = DatabaseId.of(options.getProjectId(), args[0], args[1]);
    // [END init_client]
    // This will return the default project id based on the environment.
    String clientProject = spanner.getOptions().getProjectId();
    if (!db.getInstanceId().getProject().equals(clientProject)) {
      System.err.println("Invalid project specified. Project in the database id should match"
          + "the project name set in the environment variable GCLOUD_PROJECT. Expected: "
          + clientProject);
    }
    // [START init_client]
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    createDatabase(dbAdminClient, db);
  } finally {
    spanner.close();
  }
  
}
 
Example 2
Source File: HelloSpanner.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
DatabaseClient get() throws Throwable {
  if (!initialized) {
    synchronized (lock) {
      if (!initialized) {
        try {
          DatabaseId db =
              DatabaseId.of(
                  SpannerOptions.getDefaultProjectId(),
                  SPANNER_INSTANCE_ID,
                  SPANNER_DATABASE_ID);
          client = createSpanner().getDatabaseClient(db);
        } catch (Throwable t) {
          error = t;
        }
        initialized = true;
      }
    }
  }
  if (error != null) {
    throw error;
  }
  return client;
}
 
Example 3
Source File: Poller.java    From spanner-event-exporter with Apache License 2.0 6 votes vote down vote up
private DatabaseClient configureDb() {
  final SpannerOptions options = SpannerOptions.newBuilder().build();
  spanner = options.getService();
  final DatabaseId db = DatabaseId.of(PROJECT_ID, instanceName, dbName);
  final String clientProject = spanner.getOptions().getProjectId();

  if (!db.getInstanceId().getProject().equals(clientProject)) {
    log.error(
        "Invalid project specified. Project in the database id should match"
            + "the project name set in the environment variable GCLOUD_PROJECT. Expected: "
            + clientProject);

    stop();
    System.exit(1);
  }

  final DatabaseClient dbClient = spanner.getDatabaseClient(db);

  return dbClient;
}
 
Example 4
Source File: App.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (!(args.length == 3 || args.length == 4)) {
    printUsageAndExit();
  }
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  try {
    String command = args[0];
    DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    switch (command) {
      case "create":
        create(dbAdminClient, db);
        break;
      default:
        printUsageAndExit();
    }
  } finally {
    spanner.close();
  }
  System.out.println("Closed client");
}
 
Example 5
Source File: JdbcExamplesIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void createTestDatabase() throws Exception {
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  dbClient = spanner.getDatabaseAdminClient();
  if (instanceId == null) {
    Iterator<Instance> iterator =
        spanner.getInstanceAdminClient().listInstances().iterateAll().iterator();
    if (iterator.hasNext()) {
      instanceId = iterator.next().getId().getInstance();
    }
  }
  dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
  dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase());
  dbClient.createDatabase(instanceId, databaseId, Collections.emptyList()).get();
  CreateTableExample.createTable(options.getProjectId(), instanceId, databaseId);
}
 
Example 6
Source File: SpannerSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
BatchClient getBatchClient() {
  // [START get_batch_client]
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  final String project = "test-project";
  final String instance = "test-instance";
  final String database = "example-db";
  DatabaseId db = DatabaseId.of(project, instance, database);
  BatchClient batchClient = spanner.getBatchClient(db);
  // [END get_batch_client]

  return batchClient;
}
 
Example 7
Source File: SpannerDatabaseAdminTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	this.databaseAdminClient = mock(DatabaseAdminClient.class);
	this.databaseClient = mock(DatabaseClient.class);
	this.mockDatabasePage = mock(Page.class);
	this.databaseId = DatabaseId.of("fakeproject", "fakeinstance", "fakedb");
	this.spannerDatabaseAdminTemplate = new SpannerDatabaseAdminTemplate(
			this.databaseAdminClient, () -> this.databaseClient, () -> this.databaseId);
	this.ddlList = new ArrayList<>();
	this.ddlList.add("describe Something");

	when(this.databaseAdminClient.listDatabases("fakeinstance")).thenReturn(this.mockDatabasePage);
}
 
Example 8
Source File: SpannerSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
DatabaseClient getDatabaseClient() {
  // [START get_db_client]
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  final String project = "test-project";
  final String instance = "test-instance";
  final String database = "example-db";
  DatabaseId db = DatabaseId.of(project, instance, database);
  DatabaseClient dbClient = spanner.getDatabaseClient(db);
  // [END get_db_client]

  return dbClient;
}
 
Example 9
Source File: SpannerSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 3) {
    printUsageAndExit();
  }
  // [START init_client]
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  try {
    String command = args[0];
    DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
    // [END init_client]
    // This will return the default project id based on the environment.
    String clientProject = spanner.getOptions().getProjectId();
    if (!db.getInstanceId().getProject().equals(clientProject)) {
      System.err.println(
          "Invalid project specified. Project in the database id should match the"
              + "project name set in the environment variable GOOGLE_CLOUD_PROJECT. Expected: "
              + clientProject);
      printUsageAndExit();
    }
    // Generate a backup id for the sample database.
    String backupName =
        String.format(
            "%s_%02d",
            db.getDatabase(), LocalDate.now().get(ChronoField.ALIGNED_WEEK_OF_YEAR));
    BackupId backup = BackupId.of(db.getInstanceId(), backupName);

    // [START init_client]
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
    // Use client here...
    // [END init_client]
    run(dbClient, dbAdminClient, instanceAdminClient, command, db, backup);
  } finally {
    spanner.close();
  }
  // [END init_client]
  System.out.println("Closed client");
}
 
Example 10
Source File: SpannerSampleIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  SpannerOptions options = SpannerOptions.newBuilder().build();
  spanner = options.getService();
  dbClient = spanner.getDatabaseAdminClient();
  dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
  dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase());
  dbClient.dropDatabase(
      dbId.getInstanceId().getInstance(), SpannerSample.createRestoredSampleDbId(dbId));
}
 
Example 11
Source File: AppTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  dbClient = spanner.getDatabaseAdminClient();
  dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
  dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase());
}
 
Example 12
Source File: App.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (!(args.length == 3 || args.length == 4)) {
    printUsageAndExit();
  }
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  try {
    String command = args[0];
    DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    switch (command) {
      case "create":
        create(dbAdminClient, db);
        break;
      case "insert":
        String insertType;
        try {
          insertType = args[3];
        } catch (ArrayIndexOutOfBoundsException exception) {
          insertType = "";
        }
        insert(dbClient, insertType);
        break;
      default:
        printUsageAndExit();
    }
  } finally {
    spanner.close();
  }
  System.out.println("Closed client");
}
 
Example 13
Source File: App.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (!(args.length == 3 || args.length == 4)) {
    printUsageAndExit();
  }
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  try {
    String command = args[0];
    DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    switch (command) {
      case "create":
        create(dbAdminClient, db);
        break;
      case "insert":
        String insertType;
        try {
          insertType = args[3];
        } catch (ArrayIndexOutOfBoundsException exception) {
          insertType = "";
        }
        insert(dbClient, insertType);
        break;
      case "query":
        if (args.length == 4) {
          int timespan = 0;
          try {
            timespan = Integer.parseInt(args[3]);
          } catch (NumberFormatException e) {
            System.err.println("query command's 'timespan' parameter must be a valid integer.");
            System.exit(1);
          }
          query(dbClient, timespan);
        } else {
          query(dbClient);
        }
        break;
      case "delete":
        delete(dbAdminClient, db);
        break;
      default:
        printUsageAndExit();
    }
  } finally {
    spanner.close();
  }
  System.out.println("Closed client");
}
 
Example 14
Source File: App.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (!(args.length == 3 || args.length == 4)) {
    printUsageAndExit();
  }
  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();
  try {
    String command = args[0];
    DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
    DatabaseClient dbClient = spanner.getDatabaseClient(db);
    DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
    switch (command) {
      case "create":
        create(dbAdminClient, db);
        break;
      case "insert":
        String insertType;
        try {
          insertType = args[3];
        } catch (ArrayIndexOutOfBoundsException exception) {
          insertType = "";
        }
        insert(dbClient, insertType);
        break;
      case "query":
        if (args.length == 4) {
          int timespan = 0;
          try {
            timespan = Integer.parseInt(args[3]);
          } catch (NumberFormatException e) {
            System.err.println("query command's 'timespan' parameter must be a valid integer.");
            System.exit(1);
          }
          query(dbClient, timespan);
        } else {
          query(dbClient);
        }
        break;
      default:
        printUsageAndExit();
    }
  } finally {
    spanner.close();
  }
  System.out.println("Closed client");
}
 
Example 15
Source File: GcpSpannerAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public DatabaseIdProvider databaseId() {
	return () -> DatabaseId.of(this.projectId, this.instanceId, this.databaseName);
}
 
Example 16
Source File: IntegrationTestConfiguration.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public DatabaseId databaseId() {
	return DatabaseId.of(getProjectId(), this.instanceId, this.databaseName);
}
 
Example 17
Source File: SpannerRepositoryMultiDatabaseTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
public DatabaseIdProvider databaseIdProvider(SpannerOptions spannerOptions) {
	return () -> DatabaseId.of(spannerOptions.getProjectId(), this.instanceId,
			databaseFlipper ? "db1" : "db2");
}