Search in sources :

Example 6 with Partition

use of com.google.cloud.spanner.Partition in project beam by apache.

the class SpannerIOReadTest method runReadWithPriority.

@Test
public void runReadWithPriority() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    Read read = SpannerIO.read().withSpannerConfig(spannerConfig).withTable("users").withColumns("id", "name").withTimestampBound(timestampBound).withLowPriority();
    PCollection<Struct> one = pipeline.apply("read q", read);
    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")), any(ReadQueryUpdateTransactionOption.class))).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);
    assertEquals(RpcPriority.LOW, read.getSpannerConfig().getRpcPriority().get());
    pipeline.run();
}
Also used : Read(org.apache.beam.sdk.io.gcp.spanner.SpannerIO.Read) Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 7 with Partition

use of com.google.cloud.spanner.Partition in project beam by apache.

the class SpannerIOReadTest method readPipeline.

@Test
public void readPipeline() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    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")), any(ReadQueryUpdateTransactionOption.class))).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();
}
Also used : Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 8 with Partition

use of com.google.cloud.spanner.Partition in project beam by apache.

the class SpannerIOReadTest method runQuery.

@Test
public void runQuery() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    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")), any(ReadQueryUpdateTransactionOption.class))).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();
}
Also used : Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 9 with Partition

use of com.google.cloud.spanner.Partition in project beam by apache.

the class SpannerIOReadTest method testReadMetricsSucceed.

@Test
public void testReadMetricsSucceed() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    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")), any(ReadQueryUpdateTransactionOption.class))).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)));
    pipeline.run();
    verifyMetricWasSet("test", "aaa", "123", "ok", null, 3);
}
Also used : Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 10 with Partition

use of com.google.cloud.spanner.Partition in project google-cloud-java by GoogleCloudPlatform.

the class BatchClientSnippets method partitionRead.

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]
}
Also used : Partition(com.google.cloud.spanner.Partition) BatchReadOnlyTransaction(com.google.cloud.spanner.BatchReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Aggregations

Partition (com.google.cloud.spanner.Partition)20 Timestamp (com.google.cloud.Timestamp)11 BatchTransactionId (com.google.cloud.spanner.BatchTransactionId)11 FakeBatchTransactionId (com.google.cloud.spanner.FakeBatchTransactionId)11 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)11 PartitionOptions (com.google.cloud.spanner.PartitionOptions)11 TimestampBound (com.google.cloud.spanner.TimestampBound)11 Test (org.junit.Test)11 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)8 Struct (com.google.cloud.spanner.Struct)7 ResultSet (com.google.cloud.spanner.ResultSet)5 PreparedStatement (java.sql.PreparedStatement)3 BatchClient (com.google.cloud.spanner.BatchClient)2 ReadContext (com.google.cloud.spanner.ReadContext)2 SpannerException (com.google.cloud.spanner.SpannerException)2 JSQLParserException (net.sf.jsqlparser.JSQLParserException)2 TokenMgrException (net.sf.jsqlparser.parser.TokenMgrException)2 Statement (net.sf.jsqlparser.statement.Statement)2 Select (net.sf.jsqlparser.statement.select.Select)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2