use of com.google.api.ads.admanager.axis.v202111.Row in project beam by apache.
the class BigtableIOTest method testGetSplitPointsConsumed.
@Test
public void testGetSplitPointsConsumed() throws Exception {
final String table = "TEST-TABLE";
final int numRows = 100;
int splitPointsConsumed = 0;
makeTableData(table, numRows);
BigtableSource source = new BigtableSource(config.withTableId(StaticValueProvider.of(table)), BigtableReadOptions.builder().setKeyRanges(ALL_KEY_RANGE).build(), null);
BoundedReader<Row> reader = source.createReader(TestPipeline.testingPipelineOptions());
reader.start();
// Started, 0 split points consumed
assertEquals("splitPointsConsumed starting", splitPointsConsumed, reader.getSplitPointsConsumed());
// Split points consumed increases for each row read
while (reader.advance()) {
assertEquals("splitPointsConsumed advancing", ++splitPointsConsumed, reader.getSplitPointsConsumed());
}
// Reader marked as done, 100 split points consumed
assertEquals("splitPointsConsumed done", numRows, reader.getSplitPointsConsumed());
reader.close();
}
use of com.google.api.ads.admanager.axis.v202111.Row in project beam by apache.
the class BigtableIOTest method testReadingWithKeyRange.
/**
* Tests reading all rows using key ranges. Tests a prefix [), a suffix (], and a restricted range
* [] and that some properties hold across them.
*/
@Test
public void testReadingWithKeyRange() throws Exception {
final String table = "TEST-KEY-RANGE-TABLE";
final int numRows = 1001;
List<Row> testRows = makeTableData(table, numRows);
ByteKey startKey = ByteKey.copyFrom("key000000100".getBytes(StandardCharsets.UTF_8));
ByteKey endKey = ByteKey.copyFrom("key000000300".getBytes(StandardCharsets.UTF_8));
service.setupSampleRowKeys(table, numRows / 10, "key000000100".length());
// Test prefix: [beginning, startKey).
final ByteKeyRange prefixRange = ByteKeyRange.ALL_KEYS.withEndKey(startKey);
List<Row> prefixRows = filterToRange(testRows, prefixRange);
runReadTest(defaultRead.withTableId(table).withKeyRange(prefixRange), prefixRows);
// Test suffix: [startKey, end).
final ByteKeyRange suffixRange = ByteKeyRange.ALL_KEYS.withStartKey(startKey);
List<Row> suffixRows = filterToRange(testRows, suffixRange);
runReadTest(defaultRead.withTableId(table).withKeyRange(suffixRange), suffixRows);
// Test restricted range: [startKey, endKey).
final ByteKeyRange middleRange = ByteKeyRange.of(startKey, endKey);
List<Row> middleRows = filterToRange(testRows, middleRange);
runReadTest(defaultRead.withTableId(table).withKeyRange(middleRange), middleRows);
// ////// Size and content sanity checks //////////
// Prefix, suffix, middle should be non-trivial (non-zero,non-all).
assertThat(prefixRows, allOf(hasSize(lessThan(numRows)), hasSize(greaterThan(0))));
assertThat(suffixRows, allOf(hasSize(lessThan(numRows)), hasSize(greaterThan(0))));
assertThat(middleRows, allOf(hasSize(lessThan(numRows)), hasSize(greaterThan(0))));
// Prefix + suffix should be exactly all rows.
List<Row> union = Lists.newArrayList(prefixRows);
union.addAll(suffixRows);
assertThat("prefix + suffix = total", union, containsInAnyOrder(testRows.toArray(new Row[] {})));
// Suffix should contain the middle.
assertThat(suffixRows, hasItems(middleRows.toArray(new Row[] {})));
}
use of com.google.api.ads.admanager.axis.v202111.Row in project beam by apache.
the class BigtableServiceImplTest method testRead.
/**
* This test ensures that protobuf creation and interactions with {@link BigtableDataClient} work
* as expected.
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testRead() throws IOException {
ByteKey start = ByteKey.copyFrom("a".getBytes(StandardCharsets.UTF_8));
ByteKey end = ByteKey.copyFrom("b".getBytes(StandardCharsets.UTF_8));
when(mockBigtableSource.getRanges()).thenReturn(Arrays.asList(ByteKeyRange.of(start, end)));
when(mockBigtableSource.getTableId()).thenReturn(StaticValueProvider.of(TABLE_ID));
@SuppressWarnings("unchecked") ResultScanner<Row> mockResultScanner = Mockito.mock(ResultScanner.class);
Row expectedRow = Row.newBuilder().setKey(ByteString.copyFromUtf8("a")).build();
when(mockResultScanner.next()).thenReturn(expectedRow).thenReturn(null);
when(mockBigtableDataClient.readRows(any(ReadRowsRequest.class))).thenReturn(mockResultScanner);
BigtableService.Reader underTest = new BigtableServiceImpl.BigtableReaderImpl(mockSession, mockBigtableSource);
underTest.start();
Assert.assertEquals(expectedRow, underTest.getCurrentRow());
Assert.assertFalse(underTest.advance());
underTest.close();
verify(mockResultScanner, times(1)).close();
verifyMetricWasSet("google.bigtable.v2.ReadRows", "ok", 1);
}
use of com.google.api.ads.admanager.axis.v202111.Row in project YCSB by brianfrankcooper.
the class GoogleBigtableClient method scan.
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
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 RowRange range = RowRange.newBuilder().setStartKeyClosed(ByteStringer.wrap(startkey.getBytes())).build();
final RowSet rowSet = RowSet.newBuilder().addRowRanges(range).build();
final ReadRowsRequest.Builder rrr = ReadRowsRequest.newBuilder().setTableNameBytes(ByteStringer.wrap(lastTableBytes)).setFilter(filter).setRows(rowSet);
List<Row> rows;
try {
rows = client.readRowsAsync(rrr.build()).get();
if (rows == null || rows.isEmpty()) {
return Status.NOT_FOUND;
}
int numResults = 0;
for (final Row row : rows) {
final HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>(fields != null ? fields.size() : 10);
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
rowResult.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));
}
}
}
}
result.add(rowResult);
numResults++;
if (numResults >= recordcount) {
// if hit recordcount, bail out
break;
}
}
return Status.OK;
} catch (InterruptedException e) {
System.err.println("Interrupted during scan: " + e);
Thread.currentThread().interrupt();
return Status.ERROR;
} catch (ExecutionException e) {
System.err.println("Exception during scan: " + e);
return Status.ERROR;
}
}
use of com.google.api.ads.admanager.axis.v202111.Row in project beam by apache.
the class BigtableIOTest method testReadingWithFilter.
/**
* Tests reading all rows using a filter.
*/
@Test
public void testReadingWithFilter() throws Exception {
final String table = "TEST-FILTER-TABLE";
final int numRows = 1001;
List<Row> testRows = makeTableData(table, numRows);
String regex = ".*17.*";
final KeyMatchesRegex keyPredicate = new KeyMatchesRegex(regex);
Iterable<Row> filteredRows = testRows.stream().filter(input -> {
verifyNotNull(input, "input");
return keyPredicate.apply(input.getKey());
}).collect(Collectors.toList());
RowFilter filter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(regex)).build();
service.setupSampleRowKeys(table, 5, 10L);
runReadTest(defaultRead.withTableId(table).withRowFilter(filter), Lists.newArrayList(filteredRows));
}
Aggregations