本文整理汇总了Java中com.google.cloud.spanner.DatabaseId类的典型用法代码示例。如果您正苦于以下问题:Java DatabaseId类的具体用法?Java DatabaseId怎么用?Java DatabaseId使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DatabaseId类属于com.google.cloud.spanner包,在下文中一共展示了DatabaseId类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connectToSpanner
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
public SpannerAccessor connectToSpanner() {
SpannerOptions.Builder builder = SpannerOptions.newBuilder();
if (getProjectId() != null) {
builder.setProjectId(getProjectId().get());
}
if (getServiceFactory() != null) {
builder.setServiceFactory(this.getServiceFactory());
}
if (getHost() != null) {
builder.setHost(getHost());
}
ReleaseInfo releaseInfo = ReleaseInfo.getReleaseInfo();
builder.setUserAgentPrefix(USER_AGENT_PREFIX + "/" + releaseInfo.getVersion());
SpannerOptions options = builder.build();
Spanner spanner = options.getService();
DatabaseClient databaseClient = spanner.getDatabaseClient(
DatabaseId.of(options.getProjectId(), getInstanceId().get(), getDatabaseId().get()));
return new SpannerAccessor(spanner, databaseClient);
}
示例2: testWrite
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
@Test
public void testWrite() throws Exception {
int numRecords = 100;
p.apply(GenerateSequence.from(0).to(numRecords))
.apply(ParDo.of(new GenerateMutations(options.getTable())))
.apply(
SpannerIO.write()
.withProjectId(project)
.withInstanceId(options.getInstanceId())
.withDatabaseId(databaseName));
p.run();
DatabaseClient databaseClient =
spanner.getDatabaseClient(
DatabaseId.of(
project, options.getInstanceId(), databaseName));
ResultSet resultSet =
databaseClient
.singleUse()
.executeQuery(Statement.of("SELECT COUNT(*) FROM " + options.getTable()));
assertThat(resultSet.next(), is(true));
assertThat(resultSet.getLong(0), equalTo((long) numRecords));
assertThat(resultSet.next(), is(false));
}
示例3: createDatabase
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) {
Operation<Database, CreateDatabaseMetadata> op = dbAdminClient
.createDatabase(
id.getInstanceId().getInstance(),
id.getDatabase(),
Arrays.asList(
"CREATE TABLE Singers (\n"
+ " SingerId INT64 NOT NULL,\n"
+ " FirstName STRING(1024),\n"
+ " LastName STRING(1024),\n"
+ " SingerInfo BYTES(MAX)\n"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Albums (\n"
+ " SingerId INT64 NOT NULL,\n"
+ " AlbumId INT64 NOT NULL,\n"
+ " AlbumTitle STRING(MAX)\n"
+ ") PRIMARY KEY (SingerId, AlbumId),\n"
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"));
Database db = op.waitFor().getResult();
System.out.println("Created database [" + db.getId() + "]");
}
示例4: FakeServiceFactory
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
public FakeServiceFactory() {
synchronized (lock) {
index = count++;
mockSpanners.add(mock(Spanner.class, withSettings().serializable()));
mockDatabaseClients.add(mock(DatabaseClient.class, withSettings().serializable()));
}
when(mockSpanner().getDatabaseClient(Matchers.any(DatabaseId.class)))
.thenReturn(mockDatabaseClient());
}
示例5: testRead
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
@Test
public void testRead() throws Exception {
DatabaseClient databaseClient =
spanner.getDatabaseClient(
DatabaseId.of(
project, options.getInstanceId(), databaseName));
List<Mutation> mutations = new ArrayList<>();
for (int i = 0; i < 5L; i++) {
mutations.add(
Mutation.newInsertOrUpdateBuilder(options.getTable())
.set("key")
.to((long) i)
.set("value")
.to(RandomUtils.randomAlphaNumeric(100))
.build());
}
databaseClient.writeAtLeastOnce(mutations);
SpannerConfig spannerConfig = SpannerConfig.create()
.withProjectId(project)
.withInstanceId(options.getInstanceId())
.withDatabaseId(databaseName);
PCollectionView<Transaction> tx =
p.apply(
SpannerIO.createTransaction()
.withSpannerConfig(spannerConfig)
.withTimestampBound(TimestampBound.strong()));
PCollection<Struct> output =
p.apply(
SpannerIO.read()
.withSpannerConfig(spannerConfig)
.withQuery("SELECT * FROM " + options.getTable())
.withTransaction(tx));
PAssert.thatSingleton(output.apply("Count rows", Count.<Struct>globally())).isEqualTo(5L);
p.run();
}
示例6: getDatabaseClient
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
static DatabaseClient getDatabaseClient() {
if (databaseClient == null) {
databaseClient =
spanner.getDatabaseClient(DatabaseId.of(PROJECT_ID, INSTANCE_ID, DATABASE_ID));
}
return databaseClient;
}
示例7: addMarketingBudget
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
static void addMarketingBudget(DatabaseAdminClient adminClient, DatabaseId dbId) {
adminClient.updateDatabaseDdl(dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"),
null).waitFor();
System.out.println("Added MarketingBudget column");
}
示例8: addIndex
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) {
adminClient.updateDatabaseDdl(dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"),
null).waitFor();
System.out.println("Added AlbumsByAlbumTitle index");
}
示例9: addStoringIndex
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
static void addStoringIndex(DatabaseAdminClient adminClient, DatabaseId dbId) {
adminClient.updateDatabaseDdl(dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
Arrays.asList(
"CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) STORING (MarketingBudget)"),
null).waitFor();
System.out.println("Added AlbumsByAlbumTitle2 index");
}
示例10: main
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
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 GCLOUD_PROJECT. Expected: "
+ clientProject);
printUsageAndExit();
}
// [START init_client]
DatabaseClient dbClient = spanner.getDatabaseClient(db);
DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
// [END init_client]
run(dbClient, dbAdminClient, command, db);
} finally {
spanner.close();
}
System.out.println("Closed client");
}
示例11: main
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
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();
}
}
示例12: setUp
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
@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());
}
示例13: addAsteroid
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
public void addAsteroid(Asteroid newAsteroid)
{
spanner = options.getService();
try
{
db = DatabaseId.of(options.getProjectId(), INSTANCE_ID, DATABASE_ID);
//System.out.println(db);
dbClient = spanner.getDatabaseClient(db);
dbAdminClient = spanner.getDatabaseAdminClient();
/*List<Mutation> mutations = new ArrayList<>();
mutations.add(
Mutation.newInsertBuilder("Asteroids")
.set("Name")
.to(newAsteroid.getName())
.set("Diameter")
.to(newAsteroid.getDiameter())
.set("Dim_L")
.to(newAsteroid.getDimension()[0])
.set("Dim_W")
.to(newAsteroid.getDimension()[1])
.set("Dim_H")
.to(newAsteroid.getDimension()[2])
.set("MeanDistanceFromSun")
.to(newAsteroid.getMeanDFromSun())
.build());
dbClient.write(mutations);*/
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));
}
}
catch (Exception e)
{
System.out.println("SPANNER UNABLE TO WRITE");
} finally
{
spanner.close();
}
}
示例14: getDatabaseId
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
@Override
protected DatabaseId getDatabaseId() {
return DatabaseId.of(properties.getProjectId(), properties.getInstanceId(), properties.getDatabase());
}
示例15: run
import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
static void run(DatabaseClient dbClient, DatabaseAdminClient dbAdminClient, String command,
DatabaseId database) {
switch (command) {
case "createdatabase":
createDatabase(dbAdminClient, database);
break;
case "write":
writeExampleData(dbClient);
break;
case "query":
query(dbClient);
break;
case "read":
read(dbClient);
break;
case "addmarketingbudget":
addMarketingBudget(dbAdminClient, database);
break;
case "update":
update(dbClient);
break;
case "writetransaction":
writeWithTransaction(dbClient);
break;
case "querymarketingbudget":
queryMarketingBudget(dbClient);
break;
case "addindex":
addIndex(dbAdminClient, database);
break;
case "readindex":
readUsingIndex(dbClient);
break;
case "queryindex":
queryUsingIndex(dbClient);
break;
case "addstoringindex":
addStoringIndex(dbAdminClient, database);
break;
case "readstoringindex":
readStoringIndex(dbClient);
break;
case "readonlytransaction":
readOnlyTransaction(dbClient);
break;
case "readstaledata":
readStaleData(dbClient);
break;
default:
printUsageAndExit();
}
}