use of org.apache.hadoop.hbase.client.RowMutations in project hbase by apache.
the class TestRegionServerReadRequestMetrics method testReadRequestsCountNotFiltered.
@Test
public void testReadRequestsCountNotFiltered() throws Exception {
int resultCount;
Scan scan;
Append append;
Put put;
Increment increment;
Get get;
// test for scan
scan = new Scan();
try (ResultScanner scanner = table.getScanner(scan)) {
resultCount = 0;
for (Result ignore : scanner) {
resultCount++;
}
testReadRequests(resultCount, 3, 0);
}
// test for scan
scan = new Scan().withStartRow(ROW2).withStopRow(ROW3);
try (ResultScanner scanner = table.getScanner(scan)) {
resultCount = 0;
for (Result ignore : scanner) {
resultCount++;
}
testReadRequests(resultCount, 1, 0);
}
// test for get
get = new Get(ROW2);
Result result = table.get(get);
resultCount = result.isEmpty() ? 0 : 1;
testReadRequests(resultCount, 1, 0);
// test for increment
increment = new Increment(ROW1);
increment.addColumn(CF1, COL3, 1);
result = table.increment(increment);
resultCount = result.isEmpty() ? 0 : 1;
testReadRequests(resultCount, 1, 0);
// test for checkAndPut
put = new Put(ROW1);
put.addColumn(CF1, COL2, VAL2);
boolean checkAndPut = table.checkAndMutate(ROW1, CF1).qualifier(COL2).ifEquals(VAL2).thenPut(put);
resultCount = checkAndPut ? 1 : 0;
testReadRequests(resultCount, 1, 0);
// test for append
append = new Append(ROW1);
append.addColumn(CF1, COL2, VAL2);
result = table.append(append);
resultCount = result.isEmpty() ? 0 : 1;
testReadRequests(resultCount, 1, 0);
// test for checkAndMutate
put = new Put(ROW1);
put.addColumn(CF1, COL1, VAL1);
RowMutations rm = new RowMutations(ROW1);
rm.add(put);
boolean checkAndMutate = table.checkAndMutate(ROW1, CF1).qualifier(COL1).ifEquals(VAL1).thenMutate(rm);
resultCount = checkAndMutate ? 1 : 0;
testReadRequests(resultCount, 1, 0);
}
use of org.apache.hadoop.hbase.client.RowMutations in project hbase by apache.
the class TestAtomicOperation method testRowMutationMultiThreads.
/**
* Test multi-threaded row mutations.
*/
@Test
public void testRowMutationMultiThreads() throws IOException {
LOG.info("Starting test testRowMutationMultiThreads");
initHRegion(tableName, name.getMethodName(), fam1);
// create 10 threads, each will alternate between adding and
// removing a column
int numThreads = 10;
int opsPerThread = 250;
AtomicOperation[] all = new AtomicOperation[numThreads];
AtomicLong timeStamps = new AtomicLong(0);
AtomicInteger failures = new AtomicInteger(0);
// create all threads
for (int i = 0; i < numThreads; i++) {
all[i] = new AtomicOperation(region, opsPerThread, timeStamps, failures) {
@Override
public void run() {
boolean op = true;
for (int i = 0; i < numOps; i++) {
try {
// throw in some flushes
if (i % 10 == 0) {
synchronized (region) {
LOG.debug("flushing");
region.flush(true);
if (i % 100 == 0) {
region.compact(false);
}
}
}
long ts = timeStamps.incrementAndGet();
RowMutations rm = new RowMutations(row);
if (op) {
Put p = new Put(row, ts);
p.addColumn(fam1, qual1, value1);
p.setDurability(Durability.ASYNC_WAL);
rm.add(p);
Delete d = new Delete(row);
d.addColumns(fam1, qual2, ts);
d.setDurability(Durability.ASYNC_WAL);
rm.add(d);
} else {
Delete d = new Delete(row);
d.addColumns(fam1, qual1, ts);
d.setDurability(Durability.ASYNC_WAL);
rm.add(d);
Put p = new Put(row, ts);
p.addColumn(fam1, qual2, value2);
p.setDurability(Durability.ASYNC_WAL);
rm.add(p);
}
region.mutateRow(rm);
op ^= true;
// check: should always see exactly one column
Get g = new Get(row);
Result r = region.get(g);
if (r.size() != 1) {
LOG.debug(Objects.toString(r));
failures.incrementAndGet();
fail();
}
} catch (IOException e) {
e.printStackTrace();
failures.incrementAndGet();
fail();
}
}
}
};
}
// run all threads
for (int i = 0; i < numThreads; i++) {
all[i].start();
}
// wait for all threads to finish
for (int i = 0; i < numThreads; i++) {
try {
all[i].join();
} catch (InterruptedException e) {
}
}
assertEquals(0, failures.get());
}
use of org.apache.hadoop.hbase.client.RowMutations in project hbase by apache.
the class TestVisibilityLabels method testMutateRow.
@Test
public void testMutateRow() throws Exception {
final byte[] qual2 = Bytes.toBytes("qual2");
TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam)).build();
TEST_UTIL.getAdmin().createTable(tableDescriptor);
try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
Put p1 = new Put(row1);
p1.addColumn(fam, qual, value);
p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));
Put p2 = new Put(row1);
p2.addColumn(fam, qual2, value);
p2.setCellVisibility(new CellVisibility(SECRET));
RowMutations rm = new RowMutations(row1);
rm.add(p1);
rm.add(p2);
table.mutateRow(rm);
Get get = new Get(row1);
get.setAuthorizations(new Authorizations(CONFIDENTIAL));
Result result = table.get(get);
assertTrue(result.containsColumn(fam, qual));
assertFalse(result.containsColumn(fam, qual2));
get.setAuthorizations(new Authorizations(SECRET));
result = table.get(get);
assertFalse(result.containsColumn(fam, qual));
assertTrue(result.containsColumn(fam, qual2));
}
}
use of org.apache.hadoop.hbase.client.RowMutations in project hbase by apache.
the class ThriftUtilities method rowMutationsFromThrift.
/**
* Creates a {@link RowMutations} (HBase) from a {@link TRowMutations} (Thrift)
*
* @param in the <code>TRowMutations</code> to convert
*
* @return converted <code>RowMutations</code>
*/
public static RowMutations rowMutationsFromThrift(TRowMutations in) throws IOException {
List<TMutation> mutations = in.getMutations();
RowMutations out = new RowMutations(in.getRow(), mutations.size());
for (TMutation mutation : mutations) {
if (mutation.isSetPut()) {
out.add(putFromThrift(mutation.getPut()));
}
if (mutation.isSetDeleteSingle()) {
out.add(deleteFromThrift(mutation.getDeleteSingle()));
}
}
return out;
}
use of org.apache.hadoop.hbase.client.RowMutations in project hbase by apache.
the class TestHRegion method testCheckAndMutate_WithFilters.
@Test
@Deprecated
public void testCheckAndMutate_WithFilters() throws Throwable {
final byte[] FAMILY = Bytes.toBytes("fam");
// Setting up region
this.region = initHRegion(tableName, method, CONF, FAMILY);
// Put one row
Put put = new Put(row);
put.addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"));
put.addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"));
put.addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"));
region.put(put);
// Put with success
boolean ok = region.checkAndMutate(row, new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("b"))), new Put(row).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d")));
assertTrue(ok);
Result result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("D")));
assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
// Put with failure
ok = region.checkAndMutate(row, new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("c"))), new Put(row).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e")));
assertFalse(ok);
assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("E"))).isEmpty());
// Delete with success
ok = region.checkAndMutate(row, new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("b"))), new Delete(row).addColumns(FAMILY, Bytes.toBytes("D")));
assertTrue(ok);
assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("D"))).isEmpty());
// Mutate with success
ok = region.checkAndRowMutate(row, new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("b"))), new RowMutations(row).add((Mutation) new Put(row).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e"))).add((Mutation) new Delete(row).addColumns(FAMILY, Bytes.toBytes("A"))));
assertTrue(ok);
result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("E")));
assertEquals("e", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("E"))));
assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("A"))).isEmpty());
}
Aggregations