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));
}
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()));
}
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);
}
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;
}
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);
}
}
}
Aggregations