Search in sources :

Example 56 with Put

use of co.cask.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(co.cask.cdap.test.RevealingTxSystemClient.RevealingTransaction) DataSetException(co.cask.cdap.api.dataset.DataSetException) Put(co.cask.cdap.api.dataset.table.Put) TransactionFailureException(org.apache.tephra.TransactionFailureException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException)

Example 57 with Put

use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.

the class MockAction method run.

@Override
public void run(ActionContext context) throws Exception {
    context.execute(new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            Table table = context.getDataset(config.tableName);
            Put put = new Put(config.rowKey);
            put.add(config.columnKey, config.value);
            table.put(put);
        }
    });
    // Set the same value in the arguments as well.
    context.getArguments().set(config.rowKey + config.columnKey, config.value);
    if (config.argumentKey != null && config.argumentValue != null) {
        if (!context.getArguments().get(config.argumentKey).equals(config.argumentValue)) {
            throw new IllegalStateException(String.format("Expected %s to be present in the argument map with value %s.", config.argumentKey, config.argumentValue));
        }
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) TxRunnable(co.cask.cdap.api.TxRunnable) DatasetContext(co.cask.cdap.api.data.DatasetContext) Put(co.cask.cdap.api.dataset.table.Put)

Example 58 with Put

use of co.cask.cdap.api.dataset.table.Put in project cdap by caskdata.

the class TimeseriesDataset method write.

/**
 * Writes constructed value. This implementation overrides the existing value.
 * This method can be overridden to apply update logic relevant to the subclass (e.g. increment counter).
 *
 * @param row row key to write to
 * @param columnName column name to write to
 * @param value value passed with {@link Entry} into
 */
void write(byte[] row, byte[] columnName, byte[] value) {
    Put put = new Put(row, columnName, value);
    table.put(put);
}
Also used : Put(co.cask.cdap.api.dataset.table.Put)

Aggregations

Put (co.cask.cdap.api.dataset.table.Put)58 Table (co.cask.cdap.api.dataset.table.Table)23 Test (org.junit.Test)23 Row (co.cask.cdap.api.dataset.table.Row)16 Get (co.cask.cdap.api.dataset.table.Get)15 Transaction (org.apache.tephra.Transaction)12 TransactionAware (org.apache.tephra.TransactionAware)12 TransactionExecutor (org.apache.tephra.TransactionExecutor)10 Schema (co.cask.cdap.api.data.schema.Schema)9 IOException (java.io.IOException)8 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)7 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)7 Map (java.util.Map)7 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)6 WriteOnly (co.cask.cdap.api.annotation.WriteOnly)5 DataSetException (co.cask.cdap.api.dataset.DataSetException)5 Scanner (co.cask.cdap.api.dataset.table.Scanner)5 TxRunnable (co.cask.cdap.api.TxRunnable)4 DatasetContext (co.cask.cdap.api.data.DatasetContext)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)4