下面列出了org.apache.hadoop.hbase.client.Delete#addColumn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void deleteCell(HBaseCellId cell, Map<Table,List<Mutation>> mutations) throws IOException, InterruptedException {
Delete delete = new Delete(cell.getRow());
delete.addColumn(cell.getFamily(), cell.getQualifier(), cell.getTimestamp());
Table table = cell.getTable().getHTable();
List<Mutation> tableMutations = mutations.get(table);
if (tableMutations == null) {
ArrayList<Mutation> newList = new ArrayList<>();
newList.add(delete);
mutations.put(table, newList);
} else {
tableMutations.add(delete);
if (tableMutations.size() > MAX_DELETE_BATCH_SIZE) {
flushMutations(table, tableMutations);
mutations.remove(table);
}
}
}
private static Delete addDeleteMark(Delete d, DeleteMark mark, long now) {
switch (mark) {
case ROW:
break;
case FAMILY:
d.addFamily(fam);
break;
case FAMILY_VERSION:
d.addFamilyVersion(fam, now);
break;
case COLUMN:
d.addColumns(fam, qual);
break;
case CELL:
d.addColumn(fam, qual);
break;
default:
break;
}
return d;
}
@Test(timeOut = 60_000)
public void testTombstonesAreCleanedUpCase5() throws Exception {
String TEST_TABLE = "testTombstonesAreCleanedUpCase5";
createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY));
TTable txTable = new TTable(connection, TEST_TABLE);
HBaseTransaction tx1 = (HBaseTransaction) tm.begin();
byte[] rowId = Bytes.toBytes("case5");
Delete d = new Delete(rowId);
d.addColumn(fam, qual);
txTable.delete(tx1, d);
tm.commit(tx1);
HBaseTransaction lwmTx = (HBaseTransaction) tm.begin();
compactWithLWM(lwmTx.getStartTimestamp(), TEST_TABLE);
TTableCellGetterAdapter getter = new TTableCellGetterAdapter(txTable);
assertFalse(CellUtils.hasCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Delete cell shouldn't be there");
assertFalse(CellUtils.hasShadowCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Delete shadow cell shouldn't be there");
}
private Delete constructDelete(Map.Entry<String, Boolean> entry) {
boolean isUnique = entry.getValue();
Delete delete = new Delete(graph.getVertexIndexModel().serializeForWrite(vertex, entry.getValue(), entry.getKey()));
if (ts != null) {
delete.addColumn(Constants.DEFAULT_FAMILY_BYTES, Constants.CREATED_AT_BYTES, ts);
} else {
delete.addColumns(Constants.DEFAULT_FAMILY_BYTES, Constants.CREATED_AT_BYTES);
}
if (isUnique) {
if (ts != null) {
delete.addColumn(Constants.DEFAULT_FAMILY_BYTES, Constants.VERTEX_ID_BYTES, ts);
} else {
delete.addColumns(Constants.DEFAULT_FAMILY_BYTES, Constants.VERTEX_ID_BYTES);
}
}
return delete;
}
/**
* 数据删除
* @param tableName 表名
* @param rowKey 行健
* @param family 列族
* @param qualifier 列
* @return
*/
public static void delete(String tableName, String rowKey, String family,
String qualifier) {
if (null == tableName ||tableName.length()==0) {
return;
}
if( null == rowKey || rowKey.length() == 0){
return;
}
Table t = null;
try {
t = getConnection().getTable(TableName.valueOf(tableName));
Delete del = new Delete(Bytes.toBytes(rowKey));
// 如果列族不为空
if (null != family && family.length() > 0) {
// 如果列不为空
if (null != qualifier && qualifier.length() > 0) {
del.addColumn(Bytes.toBytes(family),
Bytes.toBytes(qualifier));
} else {
del.addFamily(Bytes.toBytes(family));
}
}
t.delete(del);
} catch (IOException e) {
System.out.println("删除失败!");
e.printStackTrace();
} finally {
close();
}
}
@Test(timeOut = 60_000)
public void testACellDeletedNonTransactionallyDoesNotAppearWhenAMajorCompactionOccurs() throws Throwable {
String TEST_TABLE = "testACellDeletedNonTransactionallyDoesNotAppearWhenAMajorCompactionOccurs";
createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY));
TTable txTable = new TTable(connection, TEST_TABLE);
Table table = txTable.getHTable();
// Write first a value transactionally
HBaseTransaction tx0 = (HBaseTransaction) tm.begin();
byte[] rowId = Bytes.toBytes("row1");
Put p0 = new Put(rowId);
p0.addColumn(fam, qual, Bytes.toBytes("testValue-0"));
txTable.put(tx0, p0);
tm.commit(tx0);
// Then perform a non-transactional Delete
Delete d = new Delete(rowId);
d.addColumn(fam, qual);
table.delete(d);
// Trigger a major compaction
HBaseTransaction lwmTx = (HBaseTransaction) tm.begin();
compactWithLWM(lwmTx.getStartTimestamp(), TEST_TABLE);
// Then perform a non-tx (raw) scan...
Scan scan = new Scan();
scan.setRaw(true);
ResultScanner scannerResults = table.getScanner(scan);
// ...and test the deleted cell is not there anymore
assertNull(scannerResults.next(), "There should be no results in scan results");
table.close();
}
/**
* @return a bunch of {@link WALEdit}s that test a range of serialization possibilities.
*/
private List<WALEdit> getEdits() {
// Build up a couple of edits
List<WALEdit> edits = new ArrayList<WALEdit>();
Put p = new Put(ROW);
p.addColumn(FAMILY, null, Bytes.toBytes("v1"));
WALEdit withPut = new WALEdit();
addMutation(withPut, p, FAMILY);
edits.add(withPut);
Delete d = new Delete(ROW);
d.addColumn(FAMILY, null);
WALEdit withDelete = new WALEdit();
addMutation(withDelete, d, FAMILY);
edits.add(withDelete);
WALEdit withPutsAndDeletes = new WALEdit();
addMutation(withPutsAndDeletes, d, FAMILY);
addMutation(withPutsAndDeletes, p, FAMILY);
edits.add(withPutsAndDeletes);
WALEdit justIndexUpdates = new WALEdit();
byte[] table = Bytes.toBytes("targetTable");
IndexedKeyValue ikv = IndexedKeyValue.newIndexedKeyValue(table, p);
justIndexUpdates.add(ikv);
edits.add(justIndexUpdates);
WALEdit mixed = new WALEdit();
addMutation(mixed, d, FAMILY);
mixed.add(ikv);
addMutation(mixed, p, FAMILY);
edits.add(mixed);
return edits;
}
@Test(timeOut = 60_000)
public void testTombstonesAreCleanedUpCase1() throws Exception {
String TEST_TABLE = "testTombstonesAreCleanedUpCase1";
createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY));
TTable txTable = new TTable(connection, TEST_TABLE);
HBaseTransaction tx1 = (HBaseTransaction) tm.begin();
byte[] rowId = Bytes.toBytes("case1");
Put p = new Put(rowId);
p.addColumn(fam, qual, Bytes.toBytes("testValue"));
txTable.put(tx1, p);
tm.commit(tx1);
HBaseTransaction lwmTx = (HBaseTransaction) tm.begin();
setCompactorLWM(lwmTx.getStartTimestamp(), TEST_TABLE);
HBaseTransaction tx2 = (HBaseTransaction) tm.begin();
Delete d = new Delete(rowId);
d.addColumn(fam, qual);
txTable.delete(tx2, d);
tm.commit(tx2);
TTableCellGetterAdapter getter = new TTableCellGetterAdapter(txTable);
assertTrue(CellUtils.hasCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Put cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Put shadow cell should be there");
assertTrue(CellUtils.hasCell(rowId, fam, qual, tx2.getStartTimestamp(), getter),
"Delete cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, tx2.getStartTimestamp(), getter),
"Delete shadow cell should be there");
}
@Test
public void testRowMutation() throws IOException {
final TableName tableName = TableName.valueOf(TEST_TABLE.getNameAsString() + "." + name.getMethodName());
Table table = util.createTable(tableName, new byte[][] { A, B, C });
try {
verifyMethodResult(SimpleRegionObserver.class,
new String[] { "hadPreGet", "hadPostGet", "hadPrePut", "hadPostPut", "hadDeleted" },
tableName, new Boolean[] { false, false, false, false, false });
Put put = new Put(ROW);
put.addColumn(A, A, A);
put.addColumn(B, B, B);
put.addColumn(C, C, C);
Delete delete = new Delete(ROW);
delete.addColumn(A, A);
delete.addColumn(B, B);
delete.addColumn(C, C);
RowMutations arm = new RowMutations(ROW);
arm.add(put);
arm.add(delete);
table.mutateRow(arm);
verifyMethodResult(SimpleRegionObserver.class,
new String[] { "hadPreGet", "hadPostGet", "hadPrePut", "hadPostPut", "hadDeleted" },
tableName, new Boolean[] { false, false, true, true, true });
} finally {
util.deleteTable(tableName);
table.close();
}
}
@Test(timeOut = 60_000)
public void testTombstonesAreCleanedUpCase4() throws Exception {
String TEST_TABLE = "testTombstonesAreCleanedUpCase4";
createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY));
TTable txTable = new TTable(connection, TEST_TABLE);
HBaseTransaction tx1 = (HBaseTransaction) tm.begin();
byte[] rowId = Bytes.toBytes("case4");
Put p = new Put(rowId);
p.addColumn(fam, qual, Bytes.toBytes("testValue"));
txTable.put(tx1, p);
tm.commit(tx1);
HBaseTransaction lwmTx = (HBaseTransaction) tm.begin();
HBaseTransaction tx2 = (HBaseTransaction) tm.begin();
Delete d = new Delete(rowId);
d.addColumn(fam, qual);
txTable.delete(tx2, d);
compactWithLWM(lwmTx.getStartTimestamp(), TEST_TABLE);
TTableCellGetterAdapter getter = new TTableCellGetterAdapter(txTable);
assertTrue(CellUtils.hasCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Put cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Put shadow cell shouldn't be there");
assertTrue(CellUtils.hasCell(rowId, fam, qual,tx2.getStartTimestamp(), getter),
"Delete cell should be there");
assertFalse(CellUtils.hasShadowCell(rowId, fam, qual, tx2.getStartTimestamp(), getter),
"Delete shadow cell shouldn't be there");
}
@Test(timeOut = 60_000)
public void testTombstonesAreCleanedUpCase6() throws Exception {
String TEST_TABLE = "testTombstonesAreCleanedUpCase6";
createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY));
TTable txTable = new TTable(connection, TEST_TABLE);
byte[] rowId = Bytes.toBytes("case6");
HBaseTransaction tx1 = (HBaseTransaction) tm.begin();
Delete d = new Delete(rowId);
d.addColumn(fam, qual);
txTable.delete(tx1, d);
tm.commit(tx1);
HBaseTransaction tx2 = (HBaseTransaction) tm.begin();
Put p = new Put(rowId);
p.addColumn(fam, qual, Bytes.toBytes("testValue"));
txTable.put(tx2, p);
tm.commit(tx2);
HBaseTransaction lwmTx = (HBaseTransaction) tm.begin();
compactWithLWM(lwmTx.getStartTimestamp(), TEST_TABLE);
TTableCellGetterAdapter getter = new TTableCellGetterAdapter(txTable);
assertFalse(CellUtils.hasCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Delete cell shouldn't be there");
assertFalse(CellUtils.hasShadowCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Delete shadow cell shouldn't be there");
assertTrue(CellUtils.hasCell(rowId, fam, qual, tx2.getStartTimestamp(), getter),
"Put cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, tx2.getStartTimestamp(), getter),
"Put shadow cell shouldn't be there");
}
@Test
public void testVisibilityLabelsWithDeleteColumnExactVersion() throws Exception {
setAuths();
final TableName tableName = TableName.valueOf(testName.getMethodName());
long[] ts = new long[] { 123L, 125L };
try (
Table table = createTableAndWriteDataWithLabels(ts, CONFIDENTIAL + "|" + TOPSECRET, SECRET)) {
PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
try (Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName)) {
Delete d = new Delete(row1);
d.setCellVisibility(new CellVisibility(TOPSECRET + "|" + CONFIDENTIAL));
d.addColumn(fam, qual, 123L);
table.delete(d);
} catch (Throwable t) {
throw new IOException(t);
}
return null;
}
};
SUPERUSER.runAs(actiona);
TEST_UTIL.getAdmin().flush(tableName);
Scan s = new Scan();
s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL));
ResultScanner scanner = table.getScanner(s);
Result[] next = scanner.next(3);
assertTrue(next.length == 1);
CellScanner cellScanner = next[0].cellScanner();
cellScanner.advance();
Cell current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row2, 0, row2.length));
}
}
private void fillTable() throws IOException, InterruptedException {
Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILIES, 3,
Bytes.toBytes("row0"), Bytes.toBytes("row99"), NUM_RS);
Random rand = new Random(19387129L);
for (int iStoreFile = 0; iStoreFile < 4; ++iStoreFile) {
for (int iRow = 0; iRow < 100; ++iRow) {
final byte[] row = Bytes.toBytes("row" + iRow);
Put put = new Put(row);
Delete del = new Delete(row);
for (int iCol = 0; iCol < 10; ++iCol) {
final byte[] cf = rand.nextBoolean() ? CF1 : CF2;
final long ts = Math.abs(rand.nextInt());
final byte[] qual = Bytes.toBytes("col" + iCol);
if (rand.nextBoolean()) {
final byte[] value = Bytes.toBytes("value_for_row_" + iRow +
"_cf_" + Bytes.toStringBinary(cf) + "_col_" + iCol + "_ts_" +
ts + "_random_" + rand.nextLong());
put.addColumn(cf, qual, ts, value);
} else if (rand.nextDouble() < 0.8) {
del.addColumn(cf, qual, ts);
} else {
del.addColumn(cf, qual, ts);
}
}
table.put(put);
table.delete(del);
}
}
TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
}
/**
* Runs a major compaction, and then waits until the compaction is complete before returning.
*
* @param tableName name of the table to be compacted
*/
public static void doMajorCompaction(Connection conn, String tableName) throws Exception {
tableName = SchemaUtil.normalizeIdentifier(tableName);
// We simply write a marker row, request a major compaction, and then wait until the marker
// row is gone
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), tableName));
ConnectionQueryServices services = conn.unwrap(PhoenixConnection.class).getQueryServices();
MutationState mutationState = pconn.getMutationState();
if (table.isTransactional()) {
mutationState.startTransaction(table.getTransactionProvider());
}
try (Table htable = mutationState.getHTable(table)) {
byte[] markerRowKey = Bytes.toBytes("TO_DELETE");
Put put = new Put(markerRowKey);
put.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
htable.put(put);
Delete delete = new Delete(markerRowKey);
delete.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
htable.delete(delete);
htable.close();
if (table.isTransactional()) {
mutationState.commit();
}
Admin hbaseAdmin = services.getAdmin();
hbaseAdmin.flush(TableName.valueOf(tableName));
hbaseAdmin.majorCompact(TableName.valueOf(tableName));
hbaseAdmin.close();
boolean compactionDone = false;
while (!compactionDone) {
Thread.sleep(6000L);
Scan scan = new Scan();
scan.setStartRow(markerRowKey);
scan.setStopRow(Bytes.add(markerRowKey, new byte[] { 0 }));
scan.setRaw(true);
try (Table htableForRawScan = services.getTable(Bytes.toBytes(tableName))) {
ResultScanner scanner = htableForRawScan.getScanner(scan);
List<Result> results = Lists.newArrayList(scanner);
LOGGER.info("Results: " + results);
compactionDone = results.isEmpty();
scanner.close();
}
LOGGER.info("Compaction done: " + compactionDone);
// need to run compaction after the next txn snapshot has been written so that compaction can remove deleted rows
if (!compactionDone && table.isTransactional()) {
hbaseAdmin = services.getAdmin();
hbaseAdmin.flush(TableName.valueOf(tableName));
hbaseAdmin.majorCompact(TableName.valueOf(tableName));
hbaseAdmin.close();
}
}
}
}
@Test(timeOut = 10_000)
public void runTestDeleteColumn(ITestContext context) throws Exception {
TransactionManager tm = newTransactionManager(context);
TTable tt = new TTable(connection, TEST_TABLE);
Transaction t1 = tm.begin();
LOG.info("Transaction created " + t1);
int rowsWritten = 10;
FamCol famColA = new FamCol(famA, colA);
FamCol famColB = new FamCol(famA, colB);
writeRows(tt, t1, rowsWritten, famColA, famColB);
tm.commit(t1);
Transaction t2 = tm.begin();
Delete d = new Delete(modrow);
d.addColumn(famA, colA);
tt.delete(t2, d);
Transaction tscan = tm.begin();
ResultScanner rs = tt.getScanner(tscan, new Scan());
Map<FamCol, Integer> count = countColsInRows(rs, famColA, famColB);
assertEquals((int) count.get(famColA), rowsWritten, "ColA count should be equal to rowsWritten");
assertEquals((int) count.get(famColB), rowsWritten, "ColB count should be equal to rowsWritten");
if (getClient(context).isLowLatency()) {
return;
}
tm.commit(t2);
tscan = tm.begin();
rs = tt.getScanner(tscan, new Scan());
count = countColsInRows(rs, famColA, famColB);
assertEquals((int) count.get(famColA), (rowsWritten - 1), "ColA count should be equal to rowsWritten - 1");
assertEquals((int) count.get(famColB), rowsWritten, "ColB count should be equal to rowsWritten");
}
@Test(timeOut = 60_000)
public void testTombstonesAreNotCleanedUpWhenMinorCompactionOccurs() throws Throwable {
String TEST_TABLE = "testTombstonesAreNotCleanedUpWhenMinorCompactionOccurs";
createTableIfNotExists(TEST_TABLE, Bytes.toBytes(TEST_FAMILY));
TTable txTable = new TTable(connection, TEST_TABLE);
// Configure the environment to create a minor compaction
HBaseTransaction tx0 = (HBaseTransaction) tm.begin();
byte[] rowId = Bytes.toBytes("case1");
Put p = new Put(rowId);
p.addColumn(fam, qual, Bytes.toBytes("testValue-0"));
txTable.put(tx0, p);
tm.commit(tx0);
// create the first hfile
manualFlush(TEST_TABLE);
// Create the tombstone
HBaseTransaction deleteTx = (HBaseTransaction) tm.begin();
Delete d = new Delete(rowId);
d.addColumn(fam, qual);
txTable.delete(deleteTx, d);
tm.commit(deleteTx);
// create the second hfile
manualFlush(TEST_TABLE);
HBaseTransaction tx1 = (HBaseTransaction) tm.begin();
Put p1 = new Put(rowId);
p1.addColumn(fam, qual, Bytes.toBytes("testValue-11"));
txTable.put(tx1, p1);
tm.commit(tx1);
// create the third hfile
manualFlush(TEST_TABLE);
HBaseTransaction lastTx = (HBaseTransaction) tm.begin();
Put p2 = new Put(rowId);
p2.addColumn(fam, qual, Bytes.toBytes("testValue-222"));
txTable.put(lastTx, p2);
tm.commit(lastTx);
// Trigger the minor compaction
HBaseTransaction lwmTx = (HBaseTransaction) tm.begin();
setCompactorLWM(lwmTx.getStartTimestamp(), TEST_TABLE);
admin.compact(TableName.valueOf(TEST_TABLE));
Thread.sleep(5000);
// Checks on results after compaction
TTableCellGetterAdapter getter = new TTableCellGetterAdapter(txTable);
assertFalse(CellUtils.hasCell(rowId, fam, qual, tx0.getStartTimestamp(), getter), "Put cell should be there");
assertFalse(CellUtils.hasShadowCell(rowId, fam, qual, tx0.getStartTimestamp(), getter),
"Put shadow cell should be there");
assertTrue(CellUtils.hasCell(rowId, fam, qual, tx1.getStartTimestamp(), getter), "Put cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, tx1.getStartTimestamp(), getter),
"Put shadow cell should be there");
assertTrue(CellUtils.hasCell(rowId, fam, qual, deleteTx.getStartTimestamp(), getter),
"Delete cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, deleteTx.getStartTimestamp(), getter),
"Delete shadow cell should be there");
assertTrue(CellUtils.hasCell(rowId, fam, qual, lastTx.getStartTimestamp(), getter),
"Put cell should be there");
assertTrue(CellUtils.hasShadowCell(rowId, fam, qual, lastTx.getStartTimestamp(), getter),
"Put shadow cell should be there");
}
@Override
protected boolean doRollback() throws Exception {
try {
// pre-size arraylist of deletes
int size = 0;
for (Set<ActionChange> cs : changeSets.values()) {
size += cs.size();
}
List<Delete> rollbackDeletes = new ArrayList<>(size);
for (Map.Entry<Long, Set<ActionChange>> entry : changeSets.entrySet()) {
long transactionTimestamp = entry.getKey();
for (ActionChange change : entry.getValue()) {
byte[] row = change.getRow();
byte[] family = change.getFamily();
byte[] qualifier = change.getQualifier();
Delete rollbackDelete = new Delete(row);
makeRollbackOperation(rollbackDelete);
switch (conflictLevel) {
case ROW:
case NONE:
// issue family delete for the tx write pointer
rollbackDelete.addFamilyVersion(change.getFamily(), transactionTimestamp);
break;
case COLUMN:
if (family != null && qualifier == null) {
rollbackDelete.addFamilyVersion(family, transactionTimestamp);
} else if (family != null && qualifier != null) {
rollbackDelete.addColumn(family, qualifier, transactionTimestamp);
}
break;
default:
throw new IllegalStateException("Unknown conflict detection level: " + conflictLevel);
}
rollbackDeletes.add(rollbackDelete);
}
}
hTable.delete(rollbackDeletes);
return true;
} finally {
tx = null;
changeSets.clear();
}
}
@Test
public void testDeleteColumnWithLatestTimeStampUsingMultipleVersions() throws Exception {
setAuths();
final TableName tableName = TableName.valueOf(testName.getMethodName());
try (Table table = doPuts(tableName)) {
TEST_UTIL.getAdmin().flush(tableName);
PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
try (Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName)) {
Delete d = new Delete(row1);
d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
d.addColumn(fam, qual);
table.delete(d);
} catch (Throwable t) {
throw new IOException(t);
}
return null;
}
};
SUPERUSER.runAs(actiona);
TEST_UTIL.getAdmin().flush(tableName);
Scan s = new Scan();
s.readVersions(5);
s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
ResultScanner scanner = table.getScanner(s);
Result[] next = scanner.next(3);
assertTrue(next.length == 2);
CellScanner cellScanner = next[0].cellScanner();
cellScanner.advance();
Cell current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(127L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(126L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(124L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(123L, current.getTimestamp());
cellScanner = next[1].cellScanner();
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row2, 0, row2.length));
}
}
@Test
public void testReadPointAndPartialResults() throws Exception {
final TableName tableName = TableName.valueOf(name.getMethodName());
int numRows = 5;
int numFamilies = 5;
int numQualifiers = 5;
byte[][] rows = HTestConst.makeNAscii(Bytes.toBytes("testRow"), numRows);
byte[][] families = HTestConst.makeNAscii(Bytes.toBytes("testFamily"), numFamilies);
byte[][] qualifiers = HTestConst.makeNAscii(Bytes.toBytes("testQualifier"), numQualifiers);
byte[] value = Bytes.createMaxByteArray(100);
Table tmpTable = createTestTable(tableName, rows, families, qualifiers, value);
// Open scanner before deletes
ResultScanner scanner =
tmpTable.getScanner(new Scan().setMaxResultSize(1).setAllowPartialResults(true));
// now the openScanner will also fetch data and will be executed lazily, i.e, only openScanner
// when you call next, so here we need to make a next call to open scanner. The maxResultSize
// limit can make sure that we will not fetch all the data at once, so the test sill works.
int scannerCount = scanner.next().rawCells().length;
Delete delete1 = new Delete(rows[0]);
delete1.addColumn(families[0], qualifiers[0], 0);
tmpTable.delete(delete1);
Delete delete2 = new Delete(rows[1]);
delete2.addColumn(families[1], qualifiers[1], 1);
tmpTable.delete(delete2);
// Should see all cells because scanner was opened prior to deletes
scannerCount += countCellsFromScanner(scanner);
int expectedCount = numRows * numFamilies * numQualifiers;
assertTrue("scannerCount: " + scannerCount + " expectedCount: " + expectedCount,
scannerCount == expectedCount);
// Minus 2 for the two cells that were deleted
scanner = tmpTable.getScanner(new Scan().setMaxResultSize(1).setAllowPartialResults(true));
scannerCount = countCellsFromScanner(scanner);
expectedCount = numRows * numFamilies * numQualifiers - 2;
assertTrue("scannerCount: " + scannerCount + " expectedCount: " + expectedCount,
scannerCount == expectedCount);
scanner = tmpTable.getScanner(new Scan().setMaxResultSize(1).setAllowPartialResults(true));
scannerCount = scanner.next().rawCells().length;
// Put in 2 new rows. The timestamps differ from the deleted rows
Put put1 = new Put(rows[0]);
put1.add(new KeyValue(rows[0], families[0], qualifiers[0], 1, value));
tmpTable.put(put1);
Put put2 = new Put(rows[1]);
put2.add(new KeyValue(rows[1], families[1], qualifiers[1], 2, value));
tmpTable.put(put2);
// Scanner opened prior to puts. Cell count shouldn't have changed
scannerCount += countCellsFromScanner(scanner);
expectedCount = numRows * numFamilies * numQualifiers - 2;
assertTrue("scannerCount: " + scannerCount + " expectedCount: " + expectedCount,
scannerCount == expectedCount);
// Now the scanner should see the cells that were added by puts
scanner = tmpTable.getScanner(new Scan().setMaxResultSize(1).setAllowPartialResults(true));
scannerCount = countCellsFromScanner(scanner);
expectedCount = numRows * numFamilies * numQualifiers;
assertTrue("scannerCount: " + scannerCount + " expectedCount: " + expectedCount,
scannerCount == expectedCount);
TEST_UTIL.deleteTable(tableName);
}
@Test
public void testDeleteColumnWithSpecificTimeStampUsingMultipleVersionsUnMatchingVisExpression()
throws Exception {
setAuths();
final TableName tableName = TableName.valueOf(testName.getMethodName());
try (Table table = doPuts(tableName)) {
TEST_UTIL.getAdmin().flush(tableName);
PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
try (Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tableName)) {
Delete d = new Delete(row1);
d.setCellVisibility(new CellVisibility(
"(" + PRIVATE + "&" + CONFIDENTIAL + ")|(" + SECRET + "&" + TOPSECRET + ")"));
d.addColumn(fam, qual, 125L);
table.delete(d);
} catch (Throwable t) {
throw new IOException(t);
}
return null;
}
};
SUPERUSER.runAs(actiona);
TEST_UTIL.getAdmin().flush(tableName);
Scan s = new Scan();
s.readVersions(5);
s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
ResultScanner scanner = table.getScanner(s);
Result[] next = scanner.next(3);
assertTrue(next.length == 2);
CellScanner cellScanner = next[0].cellScanner();
cellScanner.advance();
Cell current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(127L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(126L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(125L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(124L, current.getTimestamp());
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row1, 0, row1.length));
assertEquals(123L, current.getTimestamp());
cellScanner = next[1].cellScanner();
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
row2, 0, row2.length));
}
}