use of com.google.cloud.spanner.Spanner in project google-cloud-java by GoogleCloudPlatform.
the class SpannerSnippets method getDatabaseAdminClient.
DatabaseAdminClient getDatabaseAdminClient() {
// [START get_dbadmin_client]
SpannerOptions options = SpannerOptions.newBuilder().build();
Spanner spanner = options.getService();
DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
return dbAdminClient;
}
use of com.google.cloud.spanner.Spanner in project google-cloud-java by GoogleCloudPlatform.
the class SpannerSnippets method getDatabaseClient.
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);
return dbClient;
}
use of com.google.cloud.spanner.Spanner in project spanner-jdbc by olavloite.
the class CloudSpannerDriver method getSpanner.
/**
* Get a {@link Spanner} instance from the pool or create a new one if needed.
*
* @param projectId The projectId to connect to
* @param credentials The credentials to use for the connection
* @param host The host to connect to. Normally this is https://spanner.googleapis.com, but you
* could also use a (local) emulator. If null, no host will be set and the default host of
* Google Cloud Spanner will be used.
* @return The {@link Spanner} instance to use
*/
synchronized Spanner getSpanner(String projectId, Credentials credentials, String host) {
SpannerKey key = SpannerKey.of(host, projectId, credentials);
Spanner spanner = spanners.get(key);
if (spanner == null) {
spanner = createSpanner(key);
spanners.put(key, spanner);
}
return spanner;
}
use of com.google.cloud.spanner.Spanner in project google-cloud-java by GoogleCloudPlatform.
the class RemoteSpannerHelper method create.
/**
* Creates a {@code RemoteSpannerHelper} bound to the given instance ID. All databases created
* using this will be created in the given instance.
*/
public static RemoteSpannerHelper create(InstanceId instanceId) throws Throwable {
SpannerOptions options = SpannerOptions.newBuilder().setProjectId(instanceId.getProject()).build();
Spanner client = options.getService();
return new RemoteSpannerHelper(options, instanceId, client);
}
use of com.google.cloud.spanner.Spanner in project java-docs-samples by GoogleCloudPlatform.
the class QuickstartSample method main.
public static void main(String... args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: QuickStartSample <instance_id> <database_id>");
return;
}
// Instantiates a client
SpannerOptions options = SpannerOptions.newBuilder().build();
Spanner spanner = options.getService();
// Name of your instance & database.
String instanceId = args[0];
String databaseId = args[1];
try {
// Creates a database client
DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));
// Queries the database
ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"));
System.out.println("\n\nResults:");
// Prints the results
while (resultSet.next()) {
System.out.printf("%d\n\n", resultSet.getLong(0));
}
} finally {
// Closes the client which will free up the resources used
spanner.close();
}
}
Aggregations