Search in sources :

Example 1 with SpannerClient

use of com.google.cloud.spanner.v1.SpannerClient in project java-spanner by googleapis.

the class RetryOnInvalidatedSessionTest method startStaticServer.

@BeforeClass
public static void startStaticServer() throws IOException {
    mockSpanner = new MockSpannerServiceImpl();
    // We don't want any unpredictable aborted transactions.
    mockSpanner.setAbortProbability(0.0D);
    mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.all(), Collections.singletonList("BAR"), READ_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.singleKey(Key.of()), Collections.singletonList("BAR"), READ_ROW_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.query(SELECT1AND2, SELECT1_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
    String uniqueName = InProcessServerBuilder.generateName();
    server = InProcessServerBuilder.forName(uniqueName).directExecutor().addService(mockSpanner).build().start();
    channelProvider = LocalChannelProvider.create(uniqueName);
    SpannerSettings settings = SpannerSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(NoCredentialsProvider.create()).build();
    spannerClient = SpannerClient.create(settings);
    executor = Executors.newSingleThreadExecutor();
}
Also used : SpannerSettings(com.google.cloud.spanner.v1.SpannerSettings) BeforeClass(org.junit.BeforeClass)

Example 2 with SpannerClient

use of com.google.cloud.spanner.v1.SpannerClient in project java-spanner by googleapis.

the class TransactionManagerAbortedTest method startStaticServer.

@BeforeClass
public static void startStaticServer() throws IOException {
    mockSpanner = new MockSpannerServiceImpl();
    // We don't want any unpredictable aborted transactions.
    mockSpanner.setAbortProbability(0.0D);
    mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.all(), Collections.singletonList("BAR"), READ_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.read("FOO", KeySet.singleKey(Key.of()), Collections.singletonList("BAR"), READ_ROW_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.query(SELECT1AND2, SELECT1AND2_RESULTSET));
    mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
    mockSpanner.putStatementResult(StatementResult.exception(UPDATE_ABORTED_STATEMENT, mockSpanner.createAbortedException(ByteString.copyFromUtf8("test"))));
    String uniqueName = InProcessServerBuilder.generateName();
    server = InProcessServerBuilder.forName(uniqueName).scheduledExecutorService(new ScheduledThreadPoolExecutor(1)).addService(mockSpanner).build().start();
    channelProvider = LocalChannelProvider.create(uniqueName);
    SpannerSettings settings = SpannerSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(NoCredentialsProvider.create()).build();
    spannerClient = SpannerClient.create(settings);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ByteString(com.google.protobuf.ByteString) SpannerSettings(com.google.cloud.spanner.v1.SpannerSettings) BeforeClass(org.junit.BeforeClass)

Example 3 with SpannerClient

use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerClientV1TestCases method prepareTestData.

void prepareTestData() throws InterruptedException {
    int columnBytes = Integer.min(payload, MAX_SIZE_PER_COLUMN);
    int rows = (payload - 1) / columnBytes + 1;
    char[] charArray = new char[columnBytes];
    Arrays.fill(charArray, 'z');
    String colContent = new String(charArray);
    SpannerClient client = getClient();
    Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    long start = System.currentTimeMillis();
    // Clean the existing data.
    BeginTransactionRequest request = BeginTransactionRequest.newBuilder().setSession(session.getName()).setOptions(TransactionOptions.newBuilder().setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance()).build()).build();
    Transaction txn = client.beginTransaction(request);
    client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setDelete(Mutation.Delete.newBuilder().setTable(LARGE_TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
    System.out.println(String.format("\nDeleted the previous large_table in %d ms.", System.currentTimeMillis() - start));
    // Add the payload data.
    start = System.currentTimeMillis();
    for (int i = 0; i < rows; i++) {
        txn = client.beginTransaction(request);
        client.commit(CommitRequest.newBuilder().addMutations(Mutation.newBuilder().setInsertOrUpdate(Mutation.Write.newBuilder().addColumns("id").addColumns("data").addValues(ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("payload" + i)).addValues(Value.newBuilder().setStringValue(colContent)).build()).setTable(LARGE_TABLE).build()).build()).setSession(session.getName()).setTransactionId(txn.getId()).build());
    }
    System.out.println(String.format("Successfully added ColumnBytes: %d, Rows: %d to large_table in %d ms.", columnBytes, rows, System.currentTimeMillis() - start));
    cleanUpClient(client, session.getName());
}
Also used : Transaction(com.google.spanner.v1.Transaction) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) Session(com.google.spanner.v1.Session)

Example 4 with SpannerClient

use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerClientV1TestCases method testExecuteSql.

void testExecuteSql() throws InterruptedException {
    System.out.println("\nTestExecuteSql");
    SpannerClient client = getClient();
    Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    ExecuteSqlRequest request = ExecuteSqlRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + TABLE).build();
    RpcCall<ExecuteSqlRequest, ResultSet> rpcCall = (ExecuteSqlRequest req) -> client.executeSql(req);
    doTestBlocking(request, rpcCall);
    cleanUpClient(client, session.getName());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) PartialResultSet(com.google.spanner.v1.PartialResultSet) ResultSet(com.google.spanner.v1.ResultSet) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) Session(com.google.spanner.v1.Session)

Example 5 with SpannerClient

use of com.google.cloud.spanner.v1.SpannerClient in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerClientV1TestCases method testPartitionQuery.

void testPartitionQuery() throws InterruptedException {
    System.out.println("\nTestPartitionQuery");
    SpannerClient client = getClient();
    Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    TransactionOptions options = TransactionOptions.newBuilder().setReadOnly(TransactionOptions.ReadOnly.getDefaultInstance()).build();
    TransactionSelector selector = TransactionSelector.newBuilder().setBegin(options).build();
    PartitionQueryRequest request = PartitionQueryRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).setTransaction(selector).build();
    RpcCall<PartitionQueryRequest, PartitionResponse> rpcCall = (PartitionQueryRequest req) -> client.partitionQuery(req);
    doTestBlocking(request, rpcCall);
    cleanUpClient(client, session.getName());
}
Also used : PartitionResponse(com.google.spanner.v1.PartitionResponse) TransactionOptions(com.google.spanner.v1.TransactionOptions) TransactionSelector(com.google.spanner.v1.TransactionSelector) PartitionQueryRequest(com.google.spanner.v1.PartitionQueryRequest) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) Session(com.google.spanner.v1.Session)

Aggregations

SpannerClient (com.google.cloud.spanner.v1.SpannerClient)7 Session (com.google.spanner.v1.Session)5 SpannerSettings (com.google.cloud.spanner.v1.SpannerSettings)3 PartialResultSet (com.google.spanner.v1.PartialResultSet)3 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)2 ResultSet (com.google.spanner.v1.ResultSet)2 BeforeClass (org.junit.BeforeClass)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)1 InstantiatingGrpcChannelProvider (com.google.api.gax.grpc.InstantiatingGrpcChannelProvider)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ListSessionsPagedResponse (com.google.cloud.spanner.v1.SpannerClient.ListSessionsPagedResponse)1 GcpManagedChannelBuilder (com.google.grpc.gcp.GcpManagedChannelBuilder)1 ByteString (com.google.protobuf.ByteString)1 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)1 ListSessionsRequest (com.google.spanner.v1.ListSessionsRequest)1 PartitionQueryRequest (com.google.spanner.v1.PartitionQueryRequest)1 PartitionResponse (com.google.spanner.v1.PartitionResponse)1 ReadRequest (com.google.spanner.v1.ReadRequest)1 Transaction (com.google.spanner.v1.Transaction)1 TransactionOptions (com.google.spanner.v1.TransactionOptions)1