Search in sources :

Example 11 with Builder

use of com.google.bigtable.v2.MutateRowsRequest.Builder in project java-bigtable by googleapis.

the class BigtableDataClientFactoryTest method testCreateWithRefreshingChannel.

@Test
public void testCreateWithRefreshingChannel() throws Exception {
    String[] tableIds = { "fake-table1", "fake-table2" };
    int poolSize = 3;
    BigtableDataSettings.Builder builder = BigtableDataSettings.newBuilderForEmulator(port).setProjectId(DEFAULT_PROJECT_ID).setInstanceId(DEFAULT_INSTANCE_ID).setAppProfileId(DEFAULT_APP_PROFILE_ID).setPrimingTableIds(tableIds).setRefreshingChannel(true);
    builder.stubSettings().setCredentialsProvider(credentialsProvider).setStreamWatchdogProvider(watchdogProvider).setExecutorProvider(executorProvider);
    InstantiatingGrpcChannelProvider channelProvider = (InstantiatingGrpcChannelProvider) builder.stubSettings().getTransportChannelProvider();
    InstantiatingGrpcChannelProvider.Builder channelProviderBuilder = channelProvider.toBuilder();
    channelProviderBuilder.setPoolSize(poolSize);
    builder.stubSettings().setTransportChannelProvider(channelProviderBuilder.build());
    BigtableDataClientFactory factory = BigtableDataClientFactory.create(builder.build());
    factory.createDefault();
    factory.createForAppProfile("other-appprofile");
    factory.createForInstance("other-project", "other-instance");
    // Make sure that only 1 instance is created for all clients
    Mockito.verify(credentialsProvider, Mockito.times(1)).getCredentials();
    Mockito.verify(executorProvider, Mockito.times(1)).getExecutor();
    Mockito.verify(watchdogProvider, Mockito.times(1)).getWatchdog();
    // Make sure that the clients are sharing the same ChannelPool
    assertThat(setUpAttributes).hasSize(poolSize);
    // Make sure that prime requests were sent only once per table per connection
    assertThat(service.readRowsRequests).hasSize(poolSize * tableIds.length);
    List<ReadRowsRequest> expectedRequests = new LinkedList<>();
    for (String tableId : tableIds) {
        for (int i = 0; i < poolSize; i++) {
            expectedRequests.add(ReadRowsRequest.newBuilder().setTableName(String.format("projects/%s/instances/%s/tables/%s", DEFAULT_PROJECT_ID, DEFAULT_INSTANCE_ID, tableId)).setAppProfileId(DEFAULT_APP_PROFILE_ID).setRows(RowSet.newBuilder().addRowKeys(ByteString.copyFromUtf8("nonexistent-priming-row"))).setFilter(RowFilter.newBuilder().setBlockAllFilter(true).build()).setRowsLimit(1).build());
        }
    }
    assertThat(service.readRowsRequests).containsExactly(expectedRequests.toArray());
    // Wait for all the connections to close asynchronously
    factory.close();
    long sleepTimeMs = 1000;
    Thread.sleep(sleepTimeMs);
    // Verify that all the channels are closed
    assertThat(terminateAttributes).hasSize(poolSize);
}
Also used : InstantiatingGrpcChannelProvider(com.google.api.gax.grpc.InstantiatingGrpcChannelProvider) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) ByteString(com.google.protobuf.ByteString) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with Builder

use of com.google.bigtable.v2.MutateRowsRequest.Builder in project java-bigtable by googleapis.

the class ReadRowsResumptionStrategy method getResumeRequest.

/**
 * {@inheritDoc}
 *
 * <p>Given a request, this implementation will narrow that request to exclude all row keys and
 * ranges that would produce rows that come before {@link #lastKey}. Furthermore this
 * implementation takes care to update the row limit of the request to account for all of the
 * received rows.
 */
@Override
public ReadRowsRequest getResumeRequest(ReadRowsRequest originalRequest) {
    // so resume with the original request object.
    if (lastKey.isEmpty()) {
        return originalRequest;
    }
    RowSet remaining = RowSetUtil.split(originalRequest.getRows(), lastKey).getRight();
    // for more details.
    if (remaining == null || (originalRequest.getRowsLimit() > 0 && originalRequest.getRowsLimit() == numProcessed)) {
        return ReadRowsRetryCompletedCallable.FULFILLED_REQUEST_MARKER;
    }
    Builder builder = originalRequest.toBuilder().setRows(remaining);
    if (originalRequest.getRowsLimit() > 0) {
        Preconditions.checkState(originalRequest.getRowsLimit() > numProcessed, "Detected too many rows for the current row limit during a retry.");
        builder.setRowsLimit(originalRequest.getRowsLimit() - numProcessed);
    }
    return builder.build();
}
Also used : Builder(com.google.bigtable.v2.ReadRowsRequest.Builder) RowSet(com.google.bigtable.v2.RowSet)

Example 13 with Builder

use of com.google.bigtable.v2.MutateRowsRequest.Builder in project YCSB by brianfrankcooper.

the class GoogleBigtableClient method read.

@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    if (debug) {
        System.out.println("Doing read from Bigtable columnfamily " + new String(columnFamilyBytes));
        System.out.println("Doing read for key: " + key);
    }
    setTable(table);
    RowFilter filter = RowFilter.newBuilder().setFamilyNameRegexFilterBytes(ByteStringer.wrap(columnFamilyBytes)).build();
    if (fields != null && fields.size() > 0) {
        Builder filterChain = RowFilter.Chain.newBuilder();
        filterChain.addFilters(filter);
        filterChain.addFilters(RowFilter.newBuilder().setCellsPerColumnLimitFilter(1).build());
        int count = 0;
        // usually "field#" so pre-alloc
        final StringBuilder regex = new StringBuilder(fields.size() * 6);
        for (final String field : fields) {
            if (count++ > 0) {
                regex.append("|");
            }
            regex.append(field);
        }
        filterChain.addFilters(RowFilter.newBuilder().setColumnQualifierRegexFilter(ByteStringer.wrap(regex.toString().getBytes()))).build();
        filter = RowFilter.newBuilder().setChain(filterChain.build()).build();
    }
    final ReadRowsRequest.Builder rrr = ReadRowsRequest.newBuilder().setTableNameBytes(ByteStringer.wrap(lastTableBytes)).setFilter(filter).setRows(RowSet.newBuilder().addRowKeys(ByteStringer.wrap(key.getBytes())));
    List<Row> rows;
    try {
        rows = client.readRowsAsync(rrr.build()).get();
        if (rows == null || rows.isEmpty()) {
            return Status.NOT_FOUND;
        }
        for (final Row row : rows) {
            for (final Family family : row.getFamiliesList()) {
                if (Arrays.equals(family.getNameBytes().toByteArray(), columnFamilyBytes)) {
                    for (final Column column : family.getColumnsList()) {
                        // we should only have a single cell per column
                        result.put(column.getQualifier().toString(UTF8_CHARSET), new ByteArrayByteIterator(column.getCells(0).getValue().toByteArray()));
                        if (debug) {
                            System.out.println("Result for field: " + column.getQualifier().toString(UTF8_CHARSET) + " is: " + column.getCells(0).getValue().toString(UTF8_CHARSET));
                        }
                    }
                }
            }
        }
        return Status.OK;
    } catch (InterruptedException e) {
        System.err.println("Interrupted during get: " + e);
        Thread.currentThread().interrupt();
        return Status.ERROR;
    } catch (ExecutionException e) {
        System.err.println("Exception during get: " + e);
        return Status.ERROR;
    }
}
Also used : Builder(com.google.bigtable.v2.RowFilter.Chain.Builder) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) ByteString(com.google.protobuf.ByteString) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) RowFilter(com.google.bigtable.v2.RowFilter) Column(com.google.bigtable.v2.Column) Family(com.google.bigtable.v2.Family) Row(com.google.bigtable.v2.Row) DeleteFromRow(com.google.bigtable.v2.Mutation.DeleteFromRow) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with Builder

use of com.google.bigtable.v2.MutateRowsRequest.Builder in project java-bigtable-hbase by googleapis.

the class TestRowMutationsAdapter method toMutateRowRequest.

private MutateRowRequest toMutateRowRequest(byte[] rowKey, com.google.cloud.bigtable.data.v2.models.Mutation mutation) {
    RowMutation rowMutation = toRowMutationModel(rowKey, mutation);
    MutateRowRequest.Builder builder = rowMutation.toProto(REQUEST_CONTEXT).toBuilder();
    return builder.build();
}
Also used : MutateRowRequest(com.google.bigtable.v2.MutateRowRequest) RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation)

Example 15 with Builder

use of com.google.bigtable.v2.MutateRowsRequest.Builder in project java-bigtable-hbase by googleapis.

the class TestRowRangeAdapter method testLesserOrEqThan.

@Test
public void testLesserOrEqThan() {
    ByteString key = ByteString.copyFromUtf8("hi");
    RowSet in = RowSet.newBuilder().addRowRanges(RowRange.newBuilder().setEndKeyClosed(key)).build();
    RangeSet<RowKeyWrapper> out = adapter.rowSetToRangeSet(in);
    RangeSet<RowKeyWrapper> expected = ImmutableRangeSet.<RowKeyWrapper>builder().add(Range.atMost(new RowKeyWrapper(key))).build();
    assertEquals(expected, out);
    adapter.rangeSetToByteStringRange(out, query);
    assertEquals(in, query.toProto(requestContext).getRows());
}
Also used : ByteString(com.google.protobuf.ByteString) RowSet(com.google.bigtable.v2.RowSet) RowKeyWrapper(com.google.cloud.bigtable.hbase.util.RowKeyWrapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 RowSet (com.google.bigtable.v2.RowSet)14 ByteString (com.google.protobuf.ByteString)13 RowKeyWrapper (com.google.cloud.bigtable.hbase.util.RowKeyWrapper)12 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)6 Builder (com.google.bigtable.v2.ReadRowsRequest.Builder)4 Column (com.google.bigtable.v2.Column)3 Family (com.google.bigtable.v2.Family)3 MutateRowsRequest (com.google.bigtable.v2.MutateRowsRequest)2 Builder (com.google.bigtable.v2.MutateRowsRequest.Builder)2 DeleteFromRow (com.google.bigtable.v2.Mutation.DeleteFromRow)2 Row (com.google.bigtable.v2.Row)2 RowFilter (com.google.bigtable.v2.RowFilter)2 Builder (com.google.bigtable.v2.RowFilter.Chain.Builder)2 MutateRowsException (com.google.cloud.bigtable.data.v2.models.MutateRowsException)2 FailedMutation (com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation)2 ExecutionException (java.util.concurrent.ExecutionException)2 ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)2 InternalApi (com.google.api.core.InternalApi)1 InstantiatingGrpcChannelProvider (com.google.api.gax.grpc.InstantiatingGrpcChannelProvider)1