当前位置: 首页>>代码示例>>Java>>正文


Java DatabaseId类代码示例

本文整理汇总了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);
}
 
开发者ID:apache,项目名称:beam,代码行数:20,代码来源:SpannerConfig.java

示例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));
}
 
开发者ID:apache,项目名称:beam,代码行数:26,代码来源:SpannerWriteIT.java

示例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() + "]");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:22,代码来源:SpannerSample.java

示例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());
}
 
开发者ID:apache,项目名称:beam,代码行数:10,代码来源:FakeServiceFactory.java

示例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();
}
 
开发者ID:apache,项目名称:beam,代码行数:41,代码来源:SpannerReadIT.java

示例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;
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:8,代码来源:SpannerClient.java

示例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");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:8,代码来源:SpannerSample.java

示例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");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:8,代码来源:SpannerSample.java

示例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");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:9,代码来源:SpannerSample.java

示例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");
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:30,代码来源:SpannerSample.java

示例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();
    }
  }
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:31,代码来源:QuickstartSample.java

示例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());
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:9,代码来源:SpannerSampleIT.java

示例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();
	}
}
 
开发者ID:DanielHawes,项目名称:AsteroidEndpointTest,代码行数:45,代码来源:SpannerTools.java

示例14: getDatabaseId

import com.google.cloud.spanner.DatabaseId; //导入依赖的package包/类
@Override
protected DatabaseId getDatabaseId() {
  return DatabaseId.of(properties.getProjectId(), properties.getInstanceId(), properties.getDatabase());
}
 
开发者ID:saturnism,项目名称:spring-data-spanner,代码行数:5,代码来源:SpannerAutoConfiguration.java

示例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();
  }
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:53,代码来源:SpannerSample.java


注:本文中的com.google.cloud.spanner.DatabaseId类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。