Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.beam;

import com.google.cloud.bigtable.hbase.BigtableExtendedScan;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -271,16 +272,19 @@ Builder withTableId(ValueProvider<String> tableId) {
*/
@Override
public CloudBigtableScanConfiguration build() {
RowSet rowSet = null;
if (request == null) {
ReadHooks readHooks = new DefaultReadHooks();
if (scan == null) {
scan = new Scan();
}
ReadRowsRequest.Builder builder = Adapters.SCAN_ADAPTER.adapt(scan, readHooks);
request = StaticValueProvider.of(readHooks.applyPreSendHook(builder.build()));
BigtableExtendedScan extendedScan = new BigtableExtendedScan();
extendedScan.addRange(scan.getStartRow(), scan.getStopRow());
extendedScan.setMaxVersions(scan.getMaxVersions());
extendedScan.setFilter(scan.getFilter());
rowSet = extendedScan.getRowSet();
}
return new CloudBigtableScanConfiguration(projectId, instanceId, tableId,
request, additionalConfiguration);
request, rowSet, additionalConfiguration);
}
}

Expand All @@ -295,23 +299,38 @@ private static class RequestWithTableNameValueProvider
private final ValueProvider<String> projectId;
private final ValueProvider<String> instanceId;
private final ValueProvider<String> tableId;
private final ValueProvider<ReadRowsRequest> request;
ValueProvider<ReadRowsRequest> request;
private final RowSet rowSet;
private ReadRowsRequest cachedRequest;

RequestWithTableNameValueProvider(
ValueProvider<String> projectId,
ValueProvider<String> instanceId,
ValueProvider<String> tableId,
ValueProvider<ReadRowsRequest> request) {
RowSet rowSet) {
this.projectId = projectId;
this.instanceId = instanceId;
this.tableId = tableId;
this.request = request;
this.rowSet = rowSet;
}

@Override
public ReadRowsRequest get() {
if (cachedRequest == null) {
if (request == null) {
ReadHooks readHooks = new DefaultReadHooks();
BigtableExtendedScan scan = new BigtableExtendedScan();
if (rowSet != null) {
for (RowRange range : rowSet.getRowRangesList()) {
scan.addRange(range);
}
for (ByteString rowKey : rowSet.getRowKeysList()) {
scan.addRowKey(ByteStringer.extract(rowKey));
}
}
ReadRowsRequest.Builder builder = Adapters.SCAN_ADAPTER.adapt(scan, readHooks);
request = StaticValueProvider.of(readHooks.applyPreSendHook(builder.build()));
}
if (request.get().getTableName().isEmpty()) {
BigtableInstanceName bigtableInstanceName =
new BigtableInstanceName(projectId.get(), instanceId.get());
Expand All @@ -328,8 +347,7 @@ public ReadRowsRequest get() {
public boolean isAccessible() {
return projectId.isAccessible()
&& instanceId.isAccessible()
&& tableId.isAccessible()
&& request.isAccessible();
&& tableId.isAccessible();
}

@Override
Expand All @@ -348,16 +366,21 @@ public String toString() {
* @param instanceId The instance ID.
* @param tableId The table to connect to in the instance.
* @param request The {@link ReadRowsRequest} that will be used to filter the table.
* @Param rowSet The {@link RowSet} it may contain {@link RowRange} from {@link Scan}.
* @param additionalConfiguration A {@link Map} with additional connection configuration.
*/
protected CloudBigtableScanConfiguration(
ValueProvider<String> projectId,
ValueProvider<String> instanceId,
ValueProvider<String> tableId,
ValueProvider<ReadRowsRequest> request,
RowSet rowSet,
Map<String, ValueProvider<String>> additionalConfiguration) {
super(projectId, instanceId, tableId, additionalConfiguration);
this.request = new RequestWithTableNameValueProvider(projectId, instanceId, tableId, request);
if (rowSet != null) {
request = new RequestWithTableNameValueProvider(projectId, instanceId, tableId, rowSet);
}
this.request = request;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.beam;

import java.util.Arrays;
import org.apache.beam.sdk.options.ValueProvider.StaticValueProvider;
import org.apache.beam.sdk.util.SerializableUtils;
import org.apache.hadoop.hbase.client.Scan;
Expand Down Expand Up @@ -138,4 +139,23 @@ public void testRegularAndRuntimeParametersAreEqualWithRequest() {
.build();
Assert.assertEquals(withRegularParameters, withRuntimeParameters);
}

@Test
public void testRowKeys(){
byte[] PREFIX = "PRE".getBytes();
Scan scan = new Scan()
.setStartRow(START_ROW)
.setStopRow(STOP_ROW)
.setRowPrefixFilter(PREFIX);
CloudBigtableScanConfiguration scanConfiguration =
new CloudBigtableScanConfiguration.Builder()
.withTableId(StaticValueProvider.of(TABLE))
.withProjectId(StaticValueProvider.of(PROJECT))
.withInstanceId(StaticValueProvider.of(INSTANCE))
.withScan(scan)
.withConfiguration("somekey", StaticValueProvider.of("somevalue"))
.build();
Assert.assertTrue(Arrays.equals(PREFIX, scanConfiguration.getStartRow()));
Assert.assertFalse(Arrays.equals(PREFIX, scanConfiguration.getStopRow()));
}
}