下面列出了怎么用org.apache.hadoop.hbase.filter.TimestampsFilter的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testGetSeek() throws IOException {
StoreFileScanner.instrument();
prepareRegion();
Get g = new Get(RK_BYTES);
final TimestampsFilter timestampsFilter = new TimestampsFilter(ImmutableList.of(5L), true);
g.setFilter(timestampsFilter);
final long initialSeekCount = StoreFileScanner.getSeekCount();
region.get(g);
final long finalSeekCount = StoreFileScanner.getSeekCount();
/*
Make sure there's more than one.
Aka one seek to get to the row, and one to get to the time.
*/
assertTrue(finalSeekCount >= initialSeekCount + 3 );
}
@Test
@Deprecated
public void testCheckAndMutateWithTimestampFilterForOldApi() throws Throwable {
AsyncTable<?> table = getTable.get();
// Put with specifying the timestamp
table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a"))).get();
// Put with success
boolean ok = table.checkAndMutate(row, new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(100L))
))
.thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))
.get();
assertTrue(ok);
Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(row, new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(101L))
))
.thenPut(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))
.get();
assertFalse(ok);
assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
@Test
public void testCheckAndMutateWithTimestampFilter() throws Throwable {
AsyncTable<?> table = getTable.get();
// Put with specifying the timestamp
table.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a"))).get();
// Put with success
boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
.ifMatches(new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(100L))))
.build(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")))).get();
assertTrue(ok);
Result result = table.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B"))).get();
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(CheckAndMutate.newBuilder(row)
.ifMatches(new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(101L))))
.build(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")))).get();
assertFalse(ok);
assertFalse(table.exists(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).get());
}
@Test
@Deprecated
public void testCheckAndMutateWithTimestampFilterForOldApi() throws Throwable {
try (Table table = createTable()) {
// Put with specifying the timestamp
table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
// Put with success
boolean ok = table.checkAndMutate(ROWKEY, new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(100L))
))
.thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b")));
assertTrue(ok);
Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(ROWKEY, new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(101L))
))
.thenPut(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
assertFalse(ok);
assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
}
}
@Test
public void testCheckAndMutateWithTimestampFilter() throws Throwable {
try (Table table = createTable()) {
// Put with specifying the timestamp
table.put(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
// Put with success
boolean ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
.ifMatches(new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(100L))))
.build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
assertTrue(ok);
Result result = table.get(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("B")));
assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
// Put with failure
ok = table.checkAndMutate(CheckAndMutate.newBuilder(ROWKEY)
.ifMatches(new FilterList(
new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(FAMILY)),
new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes("A"))),
new TimestampsFilter(Collections.singletonList(101L))))
.build(new Put(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
assertFalse(ok);
assertFalse(table.exists(new Get(ROWKEY).addColumn(FAMILY, Bytes.toBytes("C"))));
}
}
/**
* Uses the TimestampFilter on a Get to request a specified list of
* versions for the row/column specified by rowIdx & colIdx.
*
*/
private Cell[] getNVersions(Table ht, byte[] cf, int rowIdx,
int colIdx, List<Long> versions)
throws IOException {
byte row[] = Bytes.toBytes("row:" + rowIdx);
byte column[] = Bytes.toBytes("column:" + colIdx);
Filter filter = new TimestampsFilter(versions);
Get get = new Get(row);
get.addColumn(cf, column);
get.setFilter(filter);
get.readAllVersions();
Result result = ht.get(get);
return result.rawCells();
}
/**
* Uses the TimestampFilter on a Scan to request a specified list of
* versions for the rows from startRowIdx to endRowIdx (both inclusive).
*/
private Result[] scanNVersions(Table ht, byte[] cf, int startRowIdx,
int endRowIdx, List<Long> versions)
throws IOException {
byte startRow[] = Bytes.toBytes("row:" + startRowIdx);
byte endRow[] = Bytes.toBytes("row:" + endRowIdx + 1); // exclusive
Filter filter = new TimestampsFilter(versions);
Scan scan = new Scan().withStartRow(startRow).withStopRow(endRow);
scan.setFilter(filter);
scan.readAllVersions();
ResultScanner scanner = ht.getScanner(scan);
return scanner.next(endRowIdx - startRowIdx + 1);
}
@Test
public void testGetDoesntSeekWithNoHint() throws IOException {
StoreFileScanner.instrument();
prepareRegion();
Get g = new Get(RK_BYTES);
g.setFilter(new TimestampsFilter(ImmutableList.of(5L)));
final long initialSeekCount = StoreFileScanner.getSeekCount();
region.get(g);
final long finalSeekCount = StoreFileScanner.getSeekCount();
assertTrue(finalSeekCount >= initialSeekCount );
assertTrue(finalSeekCount < initialSeekCount + 3);
}
private List<Get> createGetList(List<GetTraceInfo> getTraceInfoList, byte[] columnFamily, Filter defaultFilter) {
if (CollectionUtils.isEmpty(getTraceInfoList)) {
return Collections.emptyList();
}
final List<Get> getList = new ArrayList<>(getTraceInfoList.size());
for (GetTraceInfo getTraceInfo : getTraceInfoList) {
final SpanHint hint = getTraceInfo.getHint();
final TimestampsFilter timeStampFilter = getTimeStampFilter(hint);
Filter filter = getFilter(defaultFilter, timeStampFilter);
final Get get = createGet(getTraceInfo.getTransactionId(), columnFamily, filter);
getList.add(get);
}
return getList;
}
private TimestampsFilter getTimeStampFilter(SpanHint hint) {
final long collectorAcceptorTime = hint.getCollectorAcceptorTime();
if (collectorAcceptorTime >= 0) {
return new TimestampsFilter(Arrays.asList(collectorAcceptorTime));
} else {
return null;
}
}
void addFilterByMapping( FilterList fl, CompareFilter.CompareOp comp, Class<?> comparatorClass, Object comparator,
Mapping.TupleMapping tupleMapping )
throws NoSuchMethodException, InstantiationException, IllegalAccessException,
java.lang.reflect.InvocationTargetException {
switch ( tupleMapping ) {
case KEY: {
addFilter( RowFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case FAMILY: {
addFilter( FamilyFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case COLUMN: {
//TODO Check if ColumnPrefixFilter works faster and suit more
addFilter( QualifierFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case VALUE: {
addFilter( ValueFilter.class, fl, comp, comparatorClass, comparator );
return;
}
case TIMESTAMP: {
addFilter( TimestampsFilter.class, fl, comp, comparatorClass, comparator );
// Constructor<TimestampsFilter> columnFilterConstructor =
// TimestampsFilter.class.getConstructor( CompareFilter.CompareOp.class, comparatorClass );
// TimestampsFilter scf = columnFilterConstructor.newInstance( comp, comparator );
// fl.addFilter( scf );
return;
}
}
}
@Test
public void testMultiColumns() throws Exception {
final byte [] TABLE = Bytes.toBytes(name.getMethodName());
byte [] FAMILY = Bytes.toBytes("event_log");
byte [][] FAMILIES = new byte[][] { FAMILY };
// create table; set versions to max...
Table ht = TEST_UTIL.createTable(TableName.valueOf(TABLE), FAMILIES, Integer.MAX_VALUE);
Put p = new Put(Bytes.toBytes("row"));
p.addColumn(FAMILY, Bytes.toBytes("column0"), 3L, Bytes.toBytes("value0-3"));
p.addColumn(FAMILY, Bytes.toBytes("column1"), 3L, Bytes.toBytes("value1-3"));
p.addColumn(FAMILY, Bytes.toBytes("column2"), 1L, Bytes.toBytes("value2-1"));
p.addColumn(FAMILY, Bytes.toBytes("column2"), 2L, Bytes.toBytes("value2-2"));
p.addColumn(FAMILY, Bytes.toBytes("column2"), 3L, Bytes.toBytes("value2-3"));
p.addColumn(FAMILY, Bytes.toBytes("column3"), 2L, Bytes.toBytes("value3-2"));
p.addColumn(FAMILY, Bytes.toBytes("column4"), 1L, Bytes.toBytes("value4-1"));
p.addColumn(FAMILY, Bytes.toBytes("column4"), 2L, Bytes.toBytes("value4-2"));
p.addColumn(FAMILY, Bytes.toBytes("column4"), 3L, Bytes.toBytes("value4-3"));
ht.put(p);
ArrayList<Long> timestamps = new ArrayList<>();
timestamps.add(new Long(3));
TimestampsFilter filter = new TimestampsFilter(timestamps);
Get g = new Get(Bytes.toBytes("row"));
g.setFilter(filter);
g.readAllVersions();
g.addColumn(FAMILY, Bytes.toBytes("column2"));
g.addColumn(FAMILY, Bytes.toBytes("column4"));
Result result = ht.get(g);
for (Cell kv : result.listCells()) {
System.out.println("found row " + Bytes.toString(CellUtil.cloneRow(kv)) +
", column " + Bytes.toString(CellUtil.cloneQualifier(kv)) + ", value "
+ Bytes.toString(CellUtil.cloneValue(kv)));
}
assertEquals(2, result.listCells().size());
assertTrue(CellUtil.matchingValue(result.listCells().get(0), Bytes.toBytes("value2-3")));
assertTrue(CellUtil.matchingValue(result.listCells().get(1), Bytes.toBytes("value4-3")));
ht.close();
}