Search in sources :

Example 6 with KeyOffset

use of com.google.cloud.bigtable.data.v2.models.KeyOffset in project java-bigtable-hbase by googleapis.

the class BigtableAsyncConnection method getAllRegionInfos.

@Override
public List<HRegionInfo> getAllRegionInfos(TableName tableName) throws IOException {
    ServerName serverName = ServerName.valueOf(settings.getDataHost(), settings.getPort(), 0);
    SampleRowKeysRequest.Builder request = SampleRowKeysRequest.newBuilder();
    request.setTableName(NameUtil.formatTableName(settings.getProjectId(), settings.getInstanceId(), tableName.getNameAsString()));
    List<KeyOffset> sampleRowKeyResponse = FutureUtil.unwrap(this.bigtableApi.getDataClient().sampleRowKeysAsync(tableName.getNameAsString()));
    return getSampledRowKeysAdapter(tableName, serverName).adaptResponse(sampleRowKeyResponse).stream().map(HRegionLocation::getRegionInfo).collect(Collectors.toCollection(CopyOnWriteArrayList::new));
}
Also used : KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) SampleRowKeysRequest(com.google.bigtable.v2.SampleRowKeysRequest) ServerName(org.apache.hadoop.hbase.ServerName)

Example 7 with KeyOffset

use of com.google.cloud.bigtable.data.v2.models.KeyOffset in project java-bigtable-hbase by googleapis.

the class TestBigtableAsyncTableRegionLocator method testGetRegionLocation.

@Test
public void testGetRegionLocation() throws ExecutionException, InterruptedException {
    List<KeyOffset> keyOffsets = ImmutableList.of(KeyOffset.create(ByteString.copyFromUtf8("a"), 100L), KeyOffset.create(ByteString.copyFromUtf8("b"), 100L), KeyOffset.create(ByteString.copyFromUtf8("y"), 100L), KeyOffset.create(ByteString.copyFromUtf8("z"), 100L));
    when(mockDataClient.sampleRowKeysAsync(TABLE_NAME.getNameAsString())).thenReturn(ApiFutures.immediateFuture(keyOffsets));
    HRegionLocation regionLocationFuture = regionLocator.getRegionLocation(Bytes.toBytes("rowKey"), 1, false).get();
    assertEquals("b", Bytes.toString(regionLocationFuture.getRegion().getStartKey()));
    assertEquals("y", Bytes.toString(regionLocationFuture.getRegion().getEndKey()));
    assertEquals(TABLE_NAME, regionLocator.getName());
    regionLocationFuture = regionLocator.getRegionLocation(Bytes.toBytes("1")).get();
    assertEquals("", Bytes.toString(regionLocationFuture.getRegion().getStartKey()));
    assertEquals("a", Bytes.toString(regionLocationFuture.getRegion().getEndKey()));
    regionLocationFuture = regionLocator.getRegionLocation(Bytes.toBytes("a")).get();
    assertEquals("a", Bytes.toString(regionLocationFuture.getRegion().getStartKey()));
    assertEquals("b", Bytes.toString(regionLocationFuture.getRegion().getEndKey()));
    regionLocationFuture = regionLocator.getRegionLocation(Bytes.toBytes("z")).get();
    assertEquals("z", Bytes.toString(regionLocationFuture.getRegion().getStartKey()));
    assertEquals("", Bytes.toString(regionLocationFuture.getRegion().getEndKey()));
    regionLocationFuture = regionLocator.getRegionLocation(Bytes.toBytes("zzz")).get();
    assertEquals("z", Bytes.toString(regionLocationFuture.getRegion().getStartKey()));
    assertEquals("", Bytes.toString(regionLocationFuture.getRegion().getEndKey()));
}
Also used : KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) Test(org.junit.Test)

Example 8 with KeyOffset

use of com.google.cloud.bigtable.data.v2.models.KeyOffset in project java-bigtable by googleapis.

the class SampleRowsIT method test.

@Test
public void test() throws InterruptedException, ExecutionException, TimeoutException {
    BigtableDataClient client = testEnvRule.env().getDataClient();
    String rowPrefix = UUID.randomUUID().toString();
    // Create some data so that sample row keys has something to show
    List<ApiFuture<?>> futures = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        ApiFuture<Void> future = client.mutateRowAsync(RowMutation.create(testEnvRule.env().getTableId(), rowPrefix + "-" + i).setCell(testEnvRule.env().getFamilyId(), "", "value"));
        futures.add(future);
    }
    ApiFutures.allAsList(futures).get(1, TimeUnit.MINUTES);
    ApiFuture<List<KeyOffset>> future = client.sampleRowKeysAsync(testEnvRule.env().getTableId());
    List<KeyOffset> results = future.get(1, TimeUnit.MINUTES);
    assertThat(results).isNotEmpty();
    assertThat(results.get(results.size() - 1).getOffsetBytes()).isGreaterThan(0L);
}
Also used : ApiFuture(com.google.api.core.ApiFuture) KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) List(java.util.List) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) Test(org.junit.Test)

Example 9 with KeyOffset

use of com.google.cloud.bigtable.data.v2.models.KeyOffset in project java-bigtable-hbase by googleapis.

the class SampledRowKeysAdapter method adaptResponse.

/**
 * adaptResponse.
 *
 * @param responses a {@link java.util.List} object.
 * @return a {@link java.util.List} object.
 */
public List<HRegionLocation> adaptResponse(List<KeyOffset> responses) {
    List<HRegionLocation> regions = new ArrayList<>();
    // Starting by the first possible row, iterate over the sorted sampled row keys and create
    // regions.
    byte[] startKey = HConstants.EMPTY_START_ROW;
    for (KeyOffset response : responses) {
        byte[] endKey = response.getKey().toByteArray();
        // Avoid empty regions.
        if (Bytes.equals(startKey, endKey)) {
            continue;
        }
        regions.add(createRegionLocation(startKey, endKey));
        startKey = endKey;
    }
    // Create one last region if the last region doesn't reach the end or there are no regions.
    byte[] endKey = HConstants.EMPTY_END_ROW;
    if (regions.isEmpty() || !Bytes.equals(startKey, endKey)) {
        regions.add(createRegionLocation(startKey, endKey));
    }
    return regions;
}
Also used : HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) ArrayList(java.util.ArrayList)

Example 10 with KeyOffset

use of com.google.cloud.bigtable.data.v2.models.KeyOffset in project java-bigtable-hbase by googleapis.

the class CloudBigtableIOIntegrationTest method testEstimatedAndSplitForLargeTable.

@Test
public void testEstimatedAndSplitForLargeTable() throws Exception {
    try (Admin admin = connection.getAdmin()) {
        LOG.info("Creating table in testEstimatedAndSplitForLargeTable()");
        TableName tableName = createNewTable(admin);
        final int rowCount = 1000;
        LOG.info("Adding %d rows in testEstimatedAndSplitForLargeTable()", rowCount);
        try (BufferedMutator mutator = connection.getBufferedMutator(tableName)) {
            for (int i = 0; i < rowCount; i++) {
                byte[] largeValue = Bytes.toBytes(RandomStringUtils.randomAlphanumeric(LARGE_VALUE_SIZE));
                mutator.mutate(new Put(Bytes.toBytes("row" + i)).addColumn(COLUMN_FAMILY, QUALIFIER1, largeValue));
            }
        }
        try {
            LOG.info("Getting Source in testEstimatedAndSplitForLargeTable()");
            CloudBigtableIO.Source source = (CloudBigtableIO.Source) CloudBigtableIO.read(createScanConfig(tableName));
            List<KeyOffset> sampleRowKeys = source.getSampleRowKeys();
            LOG.info("Getting estimated size in testEstimatedAndSplitForLargeTable()");
            long estimatedSizeBytes = source.getEstimatedSizeBytes(null);
            KeyOffset lastSample = sampleRowKeys.get(sampleRowKeys.size() - 1);
            Assert.assertEquals(lastSample.getOffsetBytes(), estimatedSizeBytes);
            LOG.info("Getting Bundles in testEstimatedAndSplitForLargeTable()");
            List<? extends BoundedSource<Result>> bundles = source.split(sampleRowKeys.get(0).getOffsetBytes() / 2, null);
            // The last sample includes the EMPTY_END_ROW key, which cannot be split.
            Assert.assertEquals(sampleRowKeys.size() * 2 - 1, bundles.size());
            final AtomicInteger count = new AtomicInteger();
            LOG.info("Reading Bundles in testEstimatedAndSplitForLargeTable()");
            ExecutorService es = Executors.newCachedThreadPool();
            try {
                for (final BoundedSource<Result> bundle : bundles) {
                    es.submit(() -> {
                        try (BoundedReader<Result> reader = bundle.createReader(null)) {
                            reader.start();
                            while (reader.getCurrent() != null) {
                                count.incrementAndGet();
                                reader.advance();
                            }
                        } catch (IOException e) {
                            LOG.warn("Could not read bundle: %s", e, bundle);
                        }
                    });
                }
            } finally {
                LOG.info("Shutting down executor in testEstimatedAndSplitForLargeTable()");
                es.shutdown();
                while (!es.isTerminated()) {
                    es.awaitTermination(1, TimeUnit.SECONDS);
                }
            }
            Assert.assertSame(sampleRowKeys, source.getSampleRowKeys());
            Assert.assertEquals(rowCount, count.intValue());
        } finally {
            LOG.info("Deleting table in testEstimatedAndSplitForLargeTable()");
            admin.deleteTable(tableName);
        }
    }
}
Also used : KeyOffset(com.google.bigtable.repackaged.com.google.cloud.bigtable.data.v2.models.KeyOffset) BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator) IOException(java.io.IOException) Admin(org.apache.hadoop.hbase.client.Admin) Put(org.apache.hadoop.hbase.client.Put) BoundedSource(org.apache.beam.sdk.io.BoundedSource) Result(org.apache.hadoop.hbase.client.Result) TableName(org.apache.hadoop.hbase.TableName) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 KeyOffset (com.google.cloud.bigtable.data.v2.models.KeyOffset)8 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)6 ArrayList (java.util.ArrayList)5 KeyOffset (com.google.bigtable.repackaged.com.google.cloud.bigtable.data.v2.models.KeyOffset)3 BoundedSource (org.apache.beam.sdk.io.BoundedSource)3 SampleRowKeysRequest (com.google.bigtable.v2.SampleRowKeysRequest)2 List (java.util.List)2 TableName (org.apache.hadoop.hbase.TableName)2 Admin (org.apache.hadoop.hbase.client.Admin)2 Put (org.apache.hadoop.hbase.client.Put)2 Result (org.apache.hadoop.hbase.client.Result)2 ApiFuture (com.google.api.core.ApiFuture)1 SampleRowKeysResponse (com.google.bigtable.v2.SampleRowKeysResponse)1 AbstractSource (com.google.cloud.bigtable.beam.CloudBigtableIO.AbstractSource)1 Source (com.google.cloud.bigtable.beam.CloudBigtableIO.Source)1 SourceWithKeys (com.google.cloud.bigtable.beam.CloudBigtableIO.SourceWithKeys)1 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)1 HeaderTracerUnaryCallable (com.google.cloud.bigtable.data.v2.stub.metrics.HeaderTracerUnaryCallable)1 StatsHeadersUnaryCallable (com.google.cloud.bigtable.data.v2.stub.metrics.StatsHeadersUnaryCallable)1