use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class TableTest method testStringPutGet.
private void testStringPutGet(Table t, String key, String col, String val) throws Exception {
Transaction tx = txClient.startShort();
((TransactionAware) t).startTx(tx);
t.put(new Put(key, col, val));
Row row = t.get(new Get(key));
Assert.assertTrue(!row.isEmpty());
Assert.assertEquals(key, Bytes.toString(row.getRow()));
Assert.assertEquals(1, row.getColumns().size());
Assert.assertEquals(col, Bytes.toString(row.getColumns().entrySet().iterator().next().getKey()));
Assert.assertEquals(val, Bytes.toString(row.getColumns().entrySet().iterator().next().getValue()));
Assert.assertEquals(val, Bytes.toString(row.get(col)));
Assert.assertEquals(val, row.getString(col));
((TransactionAware) t).rollbackTx();
txClient.abort(tx);
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class TableTest method testScanWithFuzzyRowFilter.
@Test
public void testScanWithFuzzyRowFilter() throws Exception {
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try (Table table = getTable(CONTEXT1, MY_TABLE)) {
Transaction tx1 = txClient.startShort();
((TransactionAware) table).startTx(tx1);
// write data
byte[] abc = { 'a', 'b', 'c' };
for (byte b1 : abc) {
for (byte b2 : abc) {
for (byte b3 : abc) {
for (byte b4 : abc) {
table.put(new Put(new byte[] { b1, b2, b3, b4 }).add(C1, V1));
}
}
}
}
// we should have 81 (3^4) rows now
Assert.assertEquals(81, countRows(table));
// check that filter works against data written in same tx
verifyScanWithFuzzyRowFilter(table);
// commit tx, start new and TableAssert.verify scan again against "persisted" data
txClient.canCommitOrThrow(tx1, ((TransactionAware) table).getTxChanges());
Assert.assertTrue(((TransactionAware) table).commitTx());
txClient.commitOrThrow(tx1);
((TransactionAware) table).postTxCommit();
Transaction tx2 = txClient.startShort();
((TransactionAware) table).startTx(tx2);
verifyScanWithFuzzyRowFilter(table);
} finally {
admin.drop();
}
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class AppWithCustomTx method attemptNestedTransaction.
/**
* Attempt to nest transactions. we expect this to fail, and if it does, we write the value "failed"
* to the table, for the test case to validate.
*/
static void attemptNestedTransaction(Transactional txnl, final String row, final String key) {
try {
txnl.execute(new TxRunnable() {
@Override
public void run(DatasetContext ctext) throws Exception {
recordTransaction(ctext, row, key);
}
});
LOG.error("Nested transaction should not have succeeded for {}:{}", row, key);
} catch (TransactionFailureException e) {
// expected: starting nested transaction should fail
LOG.info("Nested transaction failed as expected for {}:{}", row, key);
} catch (RuntimeException e) {
// TODO (CDAP-6837): this is needed because worker's execute() propagates the tx failure as a runtime exception
if (e.getCause() instanceof TransactionFailureException) {
// expected: starting nested transaction should fail
LOG.info("Nested transaction failed as expected for {}:{}", row, key);
} else {
throw e;
}
}
// we know that the transactional is a program context and hence implement DatasetContext
TransactionCapturingTable capture = ((DatasetContext) txnl).getDataset(CAPTURE);
capture.getTable().put(new Put(row, key, FAILED));
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class AppWithCustomTx method recordTransaction.
/**
* If in a transaction, records the timeout that the current transaction was given, or "default" if no explicit
* timeout was given. Otherwise does nothing.
*
* Note: we know whether and what explicit timeout was given, because we inject a {@link RevealingTxSystemClient},
* which returns a {@link RevealingTransaction} for {@link TransactionSystemClient#startShort(int)} only.
*/
static void recordTransaction(DatasetContext context, String row, String column) {
TransactionCapturingTable capture = context.getDataset(CAPTURE);
Transaction tx = capture.getTx();
// we cannot cast because the RevealingTransaction is not visible in the program class loader
String value = DEFAULT;
if (tx == null) {
try {
capture.getTable().put(new Put(row, column, value));
throw new RuntimeException("put to table without transaction should have failed.");
} catch (DataSetException e) {
// expected
}
return;
}
if ("RevealingTransaction".equals(tx.getClass().getSimpleName())) {
int txTimeout;
try {
txTimeout = (int) tx.getClass().getField("timeout").get(tx);
} catch (Exception e) {
throw Throwables.propagate(e);
}
value = String.valueOf(txTimeout);
}
capture.getTable().put(new Put(row, column, value));
}
use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.
the class AdminAppTestRun method testAdminProgram.
private <T extends ProgramManager<T>> void testAdminProgram(ProgramManager<T> manager) throws Exception {
// create fileset b; it will be updated by the worker
addDatasetInstance(FileSet.class.getName(), "b", FileSetProperties.builder().setBasePath("some/path").setInputFormat(TextInputFormat.class).build());
DataSetManager<FileSet> bManager = getDataset("b");
String bFormat = bManager.get().getInputFormatClassName();
String bPath = bManager.get().getBaseLocation().toURI().getPath();
Assert.assertTrue(bPath.endsWith("some/path/"));
bManager.flush();
// create table c and write some data to it; it will be truncated by the worker
addDatasetInstance("table", "c");
DataSetManager<Table> cManager = getDataset("c");
cManager.get().put(new Put("x", "y", "z"));
cManager.flush();
// create table d; it will be dropped by the worker
addDatasetInstance("table", "d");
// start the worker and wait for it to finish
File newBasePath = new File(TMP_FOLDER.newFolder(), "extra");
Assert.assertFalse(newBasePath.exists());
manager.start(ImmutableMap.of("new.base.path", newBasePath.getPath()));
manager.waitForRun(ProgramRunStatus.COMPLETED, 30, TimeUnit.SECONDS);
// validate that worker created dataset a
DataSetManager<Table> aManager = getDataset("a");
Assert.assertNull(aManager.get().scan(null, null).next());
aManager.flush();
// validate that worker update fileset b, Get a new instance of b
bManager = getDataset("b");
Assert.assertEquals(bFormat, bManager.get().getInputFormatClassName());
String newBPath = bManager.get().getBaseLocation().toURI().getPath();
Assert.assertTrue(newBPath.endsWith("/extra/"));
// make sure the directory was created by fileset update (by moving the existing base path)
Assert.assertTrue(newBasePath.exists());
bManager.flush();
// validate that dataset c is empty
Assert.assertNull(cManager.get().scan(null, null).next());
cManager.flush();
// validate that dataset d is gone
Assert.assertNull(getDataset("d").get());
// run the worker again to drop all datasets
manager.start(ImmutableMap.of("dropAll", "true"));
manager.waitForRuns(ProgramRunStatus.COMPLETED, 2, 30, TimeUnit.SECONDS);
Assert.assertNull(getDataset("a").get());
Assert.assertNull(getDataset("b").get());
Assert.assertNull(getDataset("c").get());
Assert.assertNull(getDataset("d").get());
}
Aggregations