com.google.cloud.spanner.PartitionOptions Java Examples

The following examples show how to use com.google.cloud.spanner.PartitionOptions. 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: BatchClientSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
void partitionReadUsingIndex() {
  // [START partition_read_using_index]
  final BatchReadOnlyTransaction txn =
      batchClient.batchReadOnlyTransaction(TimestampBound.strong());
  List<Partition> partitions =
      txn.partitionReadUsingIndex(
          PartitionOptions.getDefaultInstance(),
          "Singers",
          "SingerId",
          KeySet.all(),
          Arrays.asList("SingerId", "FirstName", "LastName"));

  for (Partition p : partitions) {
    try (ResultSet results = txn.execute(p)) {
      while (results.next()) {
        long singerId = results.getLong(0);
        String firstName = results.getString(1);
        String lastName = results.getString(2);
        System.out.println("[" + singerId + "] " + firstName + " " + lastName);
      }
    }
  }
  // [END partition_read_using_index]
}
 
Example #2
Source File: BatchClientSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
void partitionRead() {
  // [START partition_read]
  final BatchReadOnlyTransaction txn =
      batchClient.batchReadOnlyTransaction(TimestampBound.strong());
  List<Partition> partitions =
      txn.partitionRead(
          PartitionOptions.getDefaultInstance(),
          "Singers",
          KeySet.all(),
          Arrays.asList("SingerId", "FirstName", "LastName"));
  for (final Partition p : partitions) {
    try (ResultSet results = txn.execute(p)) {
      while (results.next()) {
        long singerId = results.getLong(0);
        String firstName = results.getString(1);
        String lastName = results.getString(2);
        System.out.println("[" + singerId + "] " + firstName + " " + lastName);
      }
    }
  }
  // [END partition_read]
}
 
Example #3
Source File: BatchClientSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
void partitionQuery() {
  // [START partition_query]
  final BatchReadOnlyTransaction txn =
      batchClient.batchReadOnlyTransaction(TimestampBound.strong());
  List<Partition> partitions =
      txn.partitionQuery(
          PartitionOptions.getDefaultInstance(),
          Statement.of("SELECT SingerId, FirstName, LastName FROM Singers"));

  for (final Partition p : partitions) {
    try (ResultSet results = txn.execute(p)) {
      while (results.next()) {
        long singerId = results.getLong(0);
        String firstName = results.getString(1);
        String lastName = results.getString(2);
        System.out.println("[" + singerId + "] " + firstName + " " + lastName);
      }
    }
  }
  // [END partition_query]

}
 
Example #4
Source File: CloudSpannerTransaction.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public List<Partition> partitionReadUsingIndex(PartitionOptions partitionOptions, String table,
    String index, KeySet keys, Iterable<String> columns, ReadOption... options)
    throws SpannerException {
  throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED,
      METHOD_NOT_IMPLEMENTED);
}
 
Example #5
Source File: CloudSpannerTransaction.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public List<Partition> partitionQuery(PartitionOptions partitionOptions, Statement statement,
    QueryOption... options) throws SpannerException {
  checkTransaction();
  if (batchReadOnlyTransaction != null) {
    return batchReadOnlyTransaction.partitionQuery(partitionOptions, statement, options);
  }
  throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION,
      METHOD_ONLY_IN_BATCH_READONLY);
}
 
Example #6
Source File: SpannerIO.java    From beam with Apache License 2.0 4 votes vote down vote up
@Nullable
abstract PartitionOptions getPartitionOptions();
 
Example #7
Source File: BatchSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/**
 * This example showcases how to create a batch client, partition a query, and concurrently read
 * from multiple partitions.
 */
public static void main(String[] args) throws InterruptedException {
  if (args.length != 2) {
    System.err.println("Usage: BatchSample <instance_id> <database_id>");
    return;
  }

  /*
   * CREATE TABLE Singers (
   *   SingerId   INT64 NOT NULL,
   *   FirstName  STRING(1024),
   *   LastName   STRING(1024),
   *   SingerInfo BYTES(MAX),
   * ) PRIMARY KEY (SingerId);
   */

  String instanceId = args[0];
  String databaseId = args[1];

  SpannerOptions options = SpannerOptions.newBuilder().build();
  Spanner spanner = options.getService();

  // [START spanner_batch_client]
  int numThreads = Runtime.getRuntime().availableProcessors();
  ExecutorService executor = Executors.newFixedThreadPool(numThreads);

  // Statistics
  int totalPartitions;
  AtomicInteger totalRecords = new AtomicInteger(0);

  try {
    BatchClient batchClient =
        spanner.getBatchClient(DatabaseId.of(options.getProjectId(), instanceId, databaseId));

    final BatchReadOnlyTransaction txn =
        batchClient.batchReadOnlyTransaction(TimestampBound.strong());

    // A Partition object is serializable and can be used from a different process.
    List<Partition> partitions =
        txn.partitionQuery(
            PartitionOptions.getDefaultInstance(),
            Statement.of("SELECT SingerId, FirstName, LastName FROM Singers"));

    totalPartitions = partitions.size();

    for (final Partition p : partitions) {
      executor.execute(
          () -> {
            try (ResultSet results = txn.execute(p)) {
              while (results.next()) {
                long singerId = results.getLong(0);
                String firstName = results.getString(1);
                String lastName = results.getString(2);
                System.out.println("[" + singerId + "] " + firstName + " " + lastName);
                totalRecords.getAndIncrement();
              }
            }
          });
    }
  } finally {
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.HOURS);
    spanner.close();
  }

  double avgRecordsPerPartition = 0.0;
  if (totalPartitions != 0) {
    avgRecordsPerPartition = (double) totalRecords.get() / totalPartitions;
  }
  System.out.println("totalPartitions=" + totalPartitions);
  System.out.println("totalRecords=" + totalRecords);
  System.out.println("avgRecordsPerPartition=" + avgRecordsPerPartition);
  // [END spanner_batch_client]
}
 
Example #8
Source File: SpannerIOReadTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void readAllPipeline() throws Exception {
  Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
  TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

  SpannerConfig spannerConfig =
      SpannerConfig.create()
          .withProjectId("test")
          .withInstanceId("123")
          .withDatabaseId("aaa")
          .withServiceFactory(serviceFactory);

  PCollectionView<Transaction> tx =
      pipeline.apply(
          "tx",
          SpannerIO.createTransaction()
              .withSpannerConfig(spannerConfig)
              .withTimestampBound(timestampBound));

  PCollection<ReadOperation> reads =
      pipeline.apply(
          Create.of(
              ReadOperation.create().withQuery("SELECT * FROM users"),
              ReadOperation.create().withTable("users").withColumns("id", "name")));

  PCollection<Struct> one =
      reads.apply(
          "read all", SpannerIO.readAll().withSpannerConfig(spannerConfig).withTransaction(tx));

  BatchTransactionId txId = new FakeBatchTransactionId("tx");
  when(mockBatchTx.getBatchTransactionId()).thenReturn(txId);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound))
      .thenReturn(mockBatchTx);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class)))
      .thenReturn(mockBatchTx);

  Partition fakePartition =
      FakePartitionFactory.createFakeReadPartition(ByteString.copyFromUtf8("partition"));
  when(mockBatchTx.partitionQuery(
          any(PartitionOptions.class), eq(Statement.of("SELECT * FROM users"))))
      .thenReturn(Arrays.asList(fakePartition, fakePartition));
  when(mockBatchTx.partitionRead(
          any(PartitionOptions.class),
          eq("users"),
          eq(KeySet.all()),
          eq(Arrays.asList("id", "name"))))
      .thenReturn(Arrays.asList(fakePartition));

  when(mockBatchTx.execute(any(Partition.class)))
      .thenReturn(
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 4)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(4, 6)));

  PAssert.that(one).containsInAnyOrder(FAKE_ROWS);

  pipeline.run();
}
 
Example #9
Source File: SpannerIOReadTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void readPipeline() throws Exception {
  Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
  TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

  SpannerConfig spannerConfig =
      SpannerConfig.create()
          .withProjectId("test")
          .withInstanceId("123")
          .withDatabaseId("aaa")
          .withServiceFactory(serviceFactory);

  PCollection<Struct> one =
      pipeline.apply(
          "read q",
          SpannerIO.read()
              .withSpannerConfig(spannerConfig)
              .withQuery("SELECT * FROM users")
              .withTimestampBound(timestampBound));
  FakeBatchTransactionId txId = new FakeBatchTransactionId("readPipelineTest");
  when(mockBatchTx.getBatchTransactionId()).thenReturn(txId);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound))
      .thenReturn(mockBatchTx);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class)))
      .thenReturn(mockBatchTx);

  Partition fakePartition =
      FakePartitionFactory.createFakeQueryPartition(ByteString.copyFromUtf8("one"));
  when(mockBatchTx.partitionQuery(
          any(PartitionOptions.class), eq(Statement.of("SELECT * FROM users"))))
      .thenReturn(Arrays.asList(fakePartition, fakePartition));

  when(mockBatchTx.execute(any(Partition.class)))
      .thenReturn(
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 6)));

  PAssert.that(one).containsInAnyOrder(FAKE_ROWS);

  pipeline.run();
}
 
Example #10
Source File: SpannerIOReadTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void runReadUsingIndex() throws Exception {
  Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
  TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

  SpannerConfig spannerConfig =
      SpannerConfig.create()
          .withProjectId("test")
          .withInstanceId("123")
          .withDatabaseId("aaa")
          .withServiceFactory(serviceFactory);

  PCollection<Struct> one =
      pipeline.apply(
          "read q",
          SpannerIO.read()
              .withTimestamp(Timestamp.now())
              .withSpannerConfig(spannerConfig)
              .withTable("users")
              .withColumns("id", "name")
              .withIndex("theindex")
              .withTimestampBound(timestampBound));

  FakeBatchTransactionId id = new FakeBatchTransactionId("runReadUsingIndexTest");
  when(mockBatchTx.getBatchTransactionId()).thenReturn(id);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound))
      .thenReturn(mockBatchTx);
  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class)))
      .thenReturn(mockBatchTx);

  Partition fakePartition =
      FakePartitionFactory.createFakeReadPartition(ByteString.copyFromUtf8("one"));

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(id)).thenReturn(mockBatchTx);
  when(mockBatchTx.partitionReadUsingIndex(
          any(PartitionOptions.class),
          eq("users"),
          eq("theindex"),
          eq(KeySet.all()),
          eq(Arrays.asList("id", "name"))))
      .thenReturn(Arrays.asList(fakePartition, fakePartition, fakePartition));

  when(mockBatchTx.execute(any(Partition.class)))
      .thenReturn(
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 4)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(4, 6)));

  PAssert.that(one).containsInAnyOrder(FAKE_ROWS);

  pipeline.run();
}
 
Example #11
Source File: SpannerIOReadTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void runRead() throws Exception {
  Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
  TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

  SpannerConfig spannerConfig =
      SpannerConfig.create()
          .withProjectId("test")
          .withInstanceId("123")
          .withDatabaseId("aaa")
          .withServiceFactory(serviceFactory);

  PCollection<Struct> one =
      pipeline.apply(
          "read q",
          SpannerIO.read()
              .withSpannerConfig(spannerConfig)
              .withTable("users")
              .withColumns("id", "name")
              .withTimestampBound(timestampBound));

  FakeBatchTransactionId id = new FakeBatchTransactionId("runReadTest");
  when(mockBatchTx.getBatchTransactionId()).thenReturn(id);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound))
      .thenReturn(mockBatchTx);
  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class)))
      .thenReturn(mockBatchTx);

  Partition fakePartition =
      FakePartitionFactory.createFakeReadPartition(ByteString.copyFromUtf8("one"));

  when(mockBatchTx.partitionRead(
          any(PartitionOptions.class),
          eq("users"),
          eq(KeySet.all()),
          eq(Arrays.asList("id", "name"))))
      .thenReturn(Arrays.asList(fakePartition, fakePartition, fakePartition));
  when(mockBatchTx.execute(any(Partition.class)))
      .thenReturn(
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 4)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(4, 6)));

  PAssert.that(one).containsInAnyOrder(FAKE_ROWS);

  pipeline.run();
}
 
Example #12
Source File: SpannerIOReadTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void runQuery() throws Exception {
  Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
  TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

  SpannerConfig spannerConfig =
      SpannerConfig.create()
          .withProjectId("test")
          .withInstanceId("123")
          .withDatabaseId("aaa")
          .withServiceFactory(serviceFactory);

  PCollection<Struct> one =
      pipeline.apply(
          "read q",
          SpannerIO.read()
              .withSpannerConfig(spannerConfig)
              .withQuery("SELECT * FROM users")
              .withTimestampBound(timestampBound));

  FakeBatchTransactionId id = new FakeBatchTransactionId("runQueryTest");
  when(mockBatchTx.getBatchTransactionId()).thenReturn(id);

  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound))
      .thenReturn(mockBatchTx);
  when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class)))
      .thenReturn(mockBatchTx);

  Partition fakePartition =
      FakePartitionFactory.createFakeQueryPartition(ByteString.copyFromUtf8("one"));

  when(mockBatchTx.partitionQuery(
          any(PartitionOptions.class), eq(Statement.of("SELECT * FROM users"))))
      .thenReturn(Arrays.asList(fakePartition, fakePartition));
  when(mockBatchTx.execute(any(Partition.class)))
      .thenReturn(
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)),
          ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 6)));

  PAssert.that(one).containsInAnyOrder(FAKE_ROWS);

  pipeline.run();
}
 
Example #13
Source File: SpannerIO.java    From beam with Apache License 2.0 4 votes vote down vote up
public Read withPartitionOptions(PartitionOptions partitionOptions) {
  return withReadOperation(getReadOperation().withPartitionOptions(partitionOptions));
}
 
Example #14
Source File: LocalSpannerIO.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Nullable
abstract PartitionOptions getPartitionOptions();
 
Example #15
Source File: ReadOperation.java    From beam with Apache License 2.0 4 votes vote down vote up
public ReadOperation withPartitionOptions(PartitionOptions partitionOptions) {
  return toBuilder().setPartitionOptions(partitionOptions).build();
}
 
Example #16
Source File: ReadOperation.java    From beam with Apache License 2.0 4 votes vote down vote up
@Nullable
abstract PartitionOptions getPartitionOptions();
 
Example #17
Source File: ReadOperation.java    From beam with Apache License 2.0 4 votes vote down vote up
public static ReadOperation create() {
  return new AutoValue_ReadOperation.Builder()
      .setPartitionOptions(PartitionOptions.getDefaultInstance())
      .setKeySet(KeySet.all())
      .build();
}
 
Example #18
Source File: AbstractCloudSpannerStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
protected List<Partition> partitionQuery(com.google.cloud.spanner.Statement statement) {
  PartitionOptions po = PartitionOptions.getDefaultInstance();
  return connection.getTransaction().partitionQuery(po, statement);
}
 
Example #19
Source File: CloudSpannerTransaction.java    From spanner-jdbc with MIT License 4 votes vote down vote up
@Override
public List<Partition> partitionRead(PartitionOptions partitionOptions, String table, KeySet keys,
    Iterable<String> columns, ReadOption... options) throws SpannerException {
  throw SpannerExceptionFactory.newSpannerException(ErrorCode.UNIMPLEMENTED,
      METHOD_NOT_IMPLEMENTED);
}
 
Example #20
Source File: LocalSpannerIO.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
public Read withPartitionOptions(PartitionOptions partitionOptions) {
  return withReadOperation(getReadOperation().withPartitionOptions(partitionOptions));
}
 
Example #21
Source File: SpannerIO.java    From beam with Apache License 2.0 votes vote down vote up
abstract Builder setPartitionOptions(PartitionOptions partitionOptions); 
Example #22
Source File: ReadOperation.java    From beam with Apache License 2.0 votes vote down vote up
abstract Builder setPartitionOptions(PartitionOptions partitionOptions); 
Example #23
Source File: LocalSpannerIO.java    From DataflowTemplates with Apache License 2.0 votes vote down vote up
abstract Builder setPartitionOptions(PartitionOptions partitionOptions);