Search in sources :

Example 26 with Put

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);
}
Also used : Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Get(io.cdap.cdap.api.dataset.table.Get) Row(io.cdap.cdap.api.dataset.table.Row) Put(io.cdap.cdap.api.dataset.table.Put)

Example 27 with Put

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();
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) HBaseTable(io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 28 with Put

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));
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TxRunnable(io.cdap.cdap.api.TxRunnable) DatasetContext(io.cdap.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) Put(io.cdap.cdap.api.dataset.table.Put)

Example 29 with Put

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));
}
Also used : Transaction(org.apache.tephra.Transaction) RevealingTransaction(io.cdap.cdap.test.RevealingTxSystemClient.RevealingTransaction) DataSetException(io.cdap.cdap.api.dataset.DataSetException) Put(io.cdap.cdap.api.dataset.table.Put) TransactionFailureException(org.apache.tephra.TransactionFailureException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException)

Example 30 with Put

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());
}
Also used : KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Table(io.cdap.cdap.api.dataset.table.Table) FileSet(io.cdap.cdap.api.dataset.lib.FileSet) File(java.io.File) Put(io.cdap.cdap.api.dataset.table.Put)

Aggregations

Put (io.cdap.cdap.api.dataset.table.Put)53 Test (org.junit.Test)24 Table (io.cdap.cdap.api.dataset.table.Table)23 Get (io.cdap.cdap.api.dataset.table.Get)13 Row (io.cdap.cdap.api.dataset.table.Row)13 Transaction (org.apache.tephra.Transaction)13 TransactionAware (org.apache.tephra.TransactionAware)13 Schema (io.cdap.cdap.api.data.schema.Schema)10 TransactionExecutor (org.apache.tephra.TransactionExecutor)10 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)7 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)6 IOException (java.io.IOException)6 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)5 DatasetId (io.cdap.cdap.proto.id.DatasetId)5 WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)4 DataSetException (io.cdap.cdap.api.dataset.DataSetException)4 Scanner (io.cdap.cdap.api.dataset.table.Scanner)4 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)4 ReflectionPutWriter (io.cdap.cdap.internal.io.ReflectionPutWriter)4 Map (java.util.Map)4