Search in sources :

Example 6 with KeyOffset

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

the class TestSampledRowKeysAdapter method testEmptyRowList.

@Test
public void testEmptyRowList() {
    List<KeyOffset> rowKeys = new ArrayList<>();
    List<HRegionLocation> locations = adapter.adaptResponse(rowKeys);
    Assert.assertEquals(1, locations.size());
    HRegionLocation location = locations.get(0);
    Assert.assertArrayEquals(HConstants.EMPTY_START_ROW, location.getRegionInfo().getStartKey());
    Assert.assertArrayEquals(HConstants.EMPTY_END_ROW, location.getRegionInfo().getEndKey());
    Assert.assertEquals("host", location.getHostname());
    Assert.assertEquals(123, location.getPort());
}
Also used : KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with KeyOffset

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

the class TestSampledRowKeysAdapter method testOneRow.

@Test
public void testOneRow() {
    byte[] rowKey = Bytes.toBytes("row");
    List<KeyOffset> responses = new ArrayList<>();
    responses.add(KeyOffset.create(ByteString.copyFrom(rowKey), 0));
    List<HRegionLocation> locations = adapter.adaptResponse(responses);
    Assert.assertEquals(2, locations.size());
    HRegionLocation location = locations.get(0);
    Assert.assertArrayEquals(HConstants.EMPTY_START_ROW, location.getRegionInfo().getStartKey());
    Assert.assertArrayEquals(rowKey, location.getRegionInfo().getEndKey());
    location = locations.get(1);
    Assert.assertArrayEquals(rowKey, location.getRegionInfo().getStartKey());
    Assert.assertArrayEquals(HConstants.EMPTY_END_ROW, location.getRegionInfo().getEndKey());
}
Also used : KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with KeyOffset

use of com.google.bigtable.repackaged.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.bigtable.repackaged.com.google.cloud.bigtable.data.v2.models.KeyOffset in project java-bigtable-hbase by googleapis.

the class TestAbstractBigtableConnection method testRegionLocator.

@Test
public void testRegionLocator() throws IOException {
    assertEquals(1, connection.getAllRegionInfos(TABLE_NAME).size());
    assertEquals(regionInfo, connection.getAllRegionInfos(TABLE_NAME).get(0));
    List<HRegionLocation> expectedRegionLocations = ImmutableList.of(new HRegionLocation(regionInfo, ServerName.valueOf(HOST_NAME, server.getPort(), 0)));
    when(mockSampledAdapter.adaptResponse(Mockito.<List<KeyOffset>>any())).thenReturn(expectedRegionLocations);
    RegionLocator regionLocator = connection.getRegionLocator(TABLE_NAME);
    assertEquals(expectedRegionLocations, regionLocator.getAllRegionLocations());
}
Also used : HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) KeyOffset(com.google.cloud.bigtable.data.v2.models.KeyOffset) Test(org.junit.Test)

Example 10 with KeyOffset

use of com.google.bigtable.repackaged.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)

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 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 SampleRowKeysRequest (com.google.bigtable.v2.SampleRowKeysRequest)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 IOException (java.io.IOException)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1