Search in sources :

Example 6 with SpannerClient

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

the class SpannerClientV1TestCases method testMaxConcurrentStream.

void testMaxConcurrentStream() throws InterruptedException {
    System.out.println("\nTestMaxConcurrentStream");
    SpannerClient client = getClient();
    Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    // Warm up.
    ExecuteSqlRequest request = ExecuteSqlRequest.newBuilder().setSession(session.getName()).setSql("select * FROM " + LARGE_TABLE).build();
    for (int i = 0; i < NUM_WARMUP; i++) {
        Iterator<PartialResultSet> iter = client.executeStreamingSqlCallable().call(request).iterator();
        while (iter.hasNext()) {
            iter.next();
        }
    }
    // Start concurrent rpc calls.
    List<Iterator<PartialResultSet>> responses = new ArrayList<>();
    long start = System.currentTimeMillis();
    for (int i = 0; i < numOfRpcs; i++) {
        responses.add(client.executeStreamingSqlCallable().call(request).iterator());
    }
    System.out.println(String.format("Started %d ExecuteStreamingSql calls in %d ms", numOfRpcs, System.currentTimeMillis() - start));
    // Start another rpc call using a new thread.
    Thread t = new Thread(() -> listSessionsSingleCall(client));
    t.start();
    System.out.println("I'm sleeping and will wake up after 2000ms zzzZZZZ.");
    Thread.sleep(2000);
    System.out.println("Good morning!");
    // Free one call.
    while (responses.get(0).hasNext()) {
        responses.get(0).next();
    }
    System.out.println(String.format("Freed one call in %dms.", System.currentTimeMillis() - start));
    // Free all the calls.
    for (int i = 1; i < responses.size(); i++) {
        Iterator<PartialResultSet> iter = responses.get(i);
        while (iter.hasNext()) {
            iter.next();
        }
    }
    System.out.println(String.format("Freed %d call(s) in %dms.", numOfRpcs, System.currentTimeMillis() - start));
    t.join();
    cleanUpClient(client, session.getName());
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) PartialResultSet(com.google.spanner.v1.PartialResultSet) Session(com.google.spanner.v1.Session)

Example 7 with SpannerClient

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

the class SpannerClientV1TestCases method testRead.

void testRead() throws InterruptedException {
    System.out.println("\nTestRead");
    SpannerClient client = getClient();
    Session session = client.createSession(CreateSessionRequest.newBuilder().setDatabase(database).build());
    ReadRequest request = ReadRequest.newBuilder().setSession(session.getName()).setTable(TABLE).setKeySet(KeySet.newBuilder().setAll(true).build()).addColumns("users").addColumns("firstname").addColumns("lastname").build();
    RpcCall<ReadRequest, ResultSet> rpcCall = (ReadRequest req) -> client.read(req);
    doTestBlocking(request, rpcCall);
    cleanUpClient(client, session.getName());
}
Also used : 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) ReadRequest(com.google.spanner.v1.ReadRequest)

Example 8 with SpannerClient

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

the class SpannerClientV1TestCases method testListSessions.

void testListSessions() throws InterruptedException {
    System.out.println("\nTestListSessions");
    SpannerClient client = getClient();
    ListSessionsRequest request = ListSessionsRequest.newBuilder().setDatabase(database).build();
    for (int i = 0; i < NUM_WARMUP; i++) {
        client.listSessions(request);
    }
    // for (Session element : spannerClient.listSessions(request).iterateAll())
    RpcCall<ListSessionsRequest, ListSessionsPagedResponse> rpcCall = (ListSessionsRequest req) -> client.listSessions(req);
    doTestBlocking(request, rpcCall);
    client.shutdown();
    client.awaitTermination(5, TimeUnit.SECONDS);
}
Also used : ListSessionsPagedResponse(com.google.cloud.spanner.v1.SpannerClient.ListSessionsPagedResponse) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) ListSessionsRequest(com.google.spanner.v1.ListSessionsRequest)

Example 9 with SpannerClient

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

the class SpannerClientV1TestCases method getClient.

private SpannerClient getClient() {
    // Set up credentials.
    GoogleCredentials creds;
    try {
        creds = GoogleCredentials.getApplicationDefault();
    } catch (Exception e) {
        return null;
    }
    ImmutableList<String> requiredScopes = ImmutableList.of(OAUTH_SCOPE);
    creds = creds.createScoped(requiredScopes);
    FixedCredentialsProvider provider = FixedCredentialsProvider.create(creds);
    // Set up the Spanner client.
    InstantiatingGrpcChannelProvider.Builder channelBuilder = InstantiatingGrpcChannelProvider.newBuilder().setPoolSize(DEFAULT_CHANNEL_POOL);
    if (isGrpcGcp) {
        File configFile = new File(SpannerTestCases.class.getClassLoader().getResource(API_FILE).getFile());
        ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> apiFunction = (ManagedChannelBuilder builder) -> (GcpManagedChannelBuilder.forDelegateBuilder(builder).withApiConfigJsonFile(configFile));
        channelBuilder = channelBuilder.setPoolSize(1).setChannelConfigurator(apiFunction);
    }
    SpannerClient client = null;
    try {
        SpannerSettings.Builder spannerSettingsBuilder = SpannerSettings.newBuilder();
        spannerSettingsBuilder.getStubSettingsBuilder().setTransportChannelProvider(channelBuilder.build()).setCredentialsProvider(provider);
        client = SpannerClient.create(spannerSettingsBuilder.build());
    } catch (IOException e) {
        System.out.println("Failed to create the client.");
    }
    return client;
}
Also used : InstantiatingGrpcChannelProvider(com.google.api.gax.grpc.InstantiatingGrpcChannelProvider) IOException(java.io.IOException) IOException(java.io.IOException) SpannerSettings(com.google.cloud.spanner.v1.SpannerSettings) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) GcpManagedChannelBuilder(com.google.grpc.gcp.GcpManagedChannelBuilder) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) SpannerClient(com.google.cloud.spanner.v1.SpannerClient) File(java.io.File)

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