Search in sources :

Example 51 with Put

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

the class MetricsTableOnTable method put.

@Override
public void put(SortedMap<byte[], ? extends SortedMap<byte[], Long>> updates) {
    for (Map.Entry<byte[], ? extends SortedMap<byte[], Long>> rowUpdate : updates.entrySet()) {
        Put put = new Put(rowUpdate.getKey());
        for (Map.Entry<byte[], Long> columnUpdate : rowUpdate.getValue().entrySet()) {
            put.add(columnUpdate.getKey(), columnUpdate.getValue());
        }
        table.put(put);
    }
}
Also used : Map(java.util.Map) NavigableMap(java.util.NavigableMap) SortedMap(java.util.SortedMap) Put(co.cask.cdap.api.dataset.table.Put)

Example 52 with Put

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

the class ObjectMappedTableDataset method write.

@WriteOnly
@Override
public void write(byte[] key, T object) {
    Put put = new Put(key);
    try {
        putWriter.write(object, put);
        table.put(put);
    } catch (IOException e) {
        // should never happen
        throw new DataSetException("Failed to encode object to be written: " + e.getMessage(), e);
    }
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) IOException(java.io.IOException) Put(co.cask.cdap.api.dataset.table.Put) WriteOnly(co.cask.cdap.api.annotation.WriteOnly)

Example 53 with Put

use of co.cask.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 : Table(co.cask.cdap.api.dataset.table.Table) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) FileSet(co.cask.cdap.api.dataset.lib.FileSet) File(java.io.File) Put(co.cask.cdap.api.dataset.table.Put)

Example 54 with Put

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

the class AdminAppTestRun method testAdminService.

@Test
public void testAdminService() throws Exception {
    // Start the service
    ServiceManager serviceManager = appManager.getServiceManager(AdminApp.SERVICE_NAME).start();
    try {
        URI serviceURI = serviceManager.getServiceURL(10, TimeUnit.SECONDS).toURI();
        // dataset nn should not exist
        HttpResponse response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("false", response.getResponseBodyAsString());
        // create nn as a table
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("create/nn/table").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // now nn should exist
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("true", response.getResponseBodyAsString());
        // create it again as a fileset -> should fail with conflict
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("create/nn/fileSet").toURL()).build());
        Assert.assertEquals(409, response.getResponseCode());
        // get the type for xx -> not found
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("type/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // get the type for nn -> table
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("type/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("table", response.getResponseBodyAsString());
        // update xx's properties -> should get not-found
        Map<String, String> nnProps = TableProperties.builder().setTTL(1000L).build().getProperties();
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("update/xx").toURL()).withBody(GSON.toJson(nnProps)).build());
        Assert.assertEquals(404, response.getResponseCode());
        // update nn's properties
        response = HttpRequests.execute(HttpRequest.put(serviceURI.resolve("update/nn").toURL()).withBody(GSON.toJson(nnProps)).build());
        Assert.assertEquals(200, response.getResponseCode());
        // get properties for xx -> not found
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("props/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // get properties for nn and validate
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("props/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Map<String, String> returnedProps = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Map<String, String>>() {
        }.getType());
        Assert.assertEquals(nnProps, returnedProps);
        // write some data to the table
        DataSetManager<Table> nnManager = getDataset("nn");
        nnManager.get().put(new Put("x", "y", "z"));
        nnManager.flush();
        // in a new tx, validate that data is in table
        Assert.assertFalse(nnManager.get().get(new Get("x")).isEmpty());
        Assert.assertEquals("z", nnManager.get().get(new Get("x", "y")).getString("y"));
        nnManager.flush();
        // truncate xx -> not found
        response = HttpRequests.execute(HttpRequest.post(serviceURI.resolve("truncate/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // truncate nn
        response = HttpRequests.execute(HttpRequest.post(serviceURI.resolve("truncate/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // validate table is empty
        Assert.assertTrue(nnManager.get().get(new Get("x")).isEmpty());
        nnManager.flush();
        // delete nn
        response = HttpRequests.execute(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        // delete again -> not found
        response = HttpRequests.execute(HttpRequest.delete(serviceURI.resolve("delete/nn").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // delete xx which never existed -> not found
        response = HttpRequests.execute(HttpRequest.delete(serviceURI.resolve("delete/xx").toURL()).build());
        Assert.assertEquals(404, response.getResponseCode());
        // exists should now return false for nn
        response = HttpRequests.execute(HttpRequest.get(serviceURI.resolve("exists/nn").toURL()).build());
        Assert.assertEquals(200, response.getResponseCode());
        Assert.assertEquals("false", response.getResponseBodyAsString());
        Assert.assertNull(getDataset("nn").get());
    } finally {
        serviceManager.stop();
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) ServiceManager(co.cask.cdap.test.ServiceManager) TypeToken(co.cask.cdap.internal.guava.reflect.TypeToken) Get(co.cask.cdap.api.dataset.table.Get) HttpResponse(co.cask.common.http.HttpResponse) URI(java.net.URI) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 55 with Put

use of co.cask.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(co.cask.cdap.api.TxRunnable) DatasetContext(co.cask.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException) 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