Search in sources :

Example 21 with Get

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

the class IndexedTableTest method testIndexedOperations.

@Test
public void testIndexedOperations() throws Exception {
    TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(table);
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // add a value c with idx = 1, and b with idx = 2
            table.put(new Put(keyC).add(idxCol, idx1).add(valCol, valC));
            table.put(new Put(keyB).add(idxCol, idx2).add(valCol, valB));
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // read by key c
            Row row = table.get(new Get(keyC, colIdxVal));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valC });
            // read by key b
            row = table.get(new Get(keyB, colIdxVal));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valB });
            // read by idx 1 -> c
            row = readFirst(table.readByIndex(idxCol, idx1));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valC });
            // read by idx 2 -> b
            row = readFirst(table.readByIndex(idxCol, idx2));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valB });
            // test read over empty index (idx 3)
            assertEmpty(table.readByIndex(idxCol, idx3));
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // add a value a with idx = 1
            table.put(new Put(keyA).add(idxCol, idx1).add(valCol, valA));
        }
    });
    // read by idx 1 -> a
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Row row = readFirst(table.readByIndex(idxCol, idx1));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valA });
        }
    });
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // delete value a
            table.delete(new Delete(keyA, colIdxVal));
        }
    });
    // read by idx 1 -> c
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Row row = readFirst(table.readByIndex(idxCol, idx1));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx1, valC });
        }
    });
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // add a value aa with idx 2
            table.put(new Put(keyAA).add(idxCol, idx2).add(valCol, valAA));
        }
    });
    // read by idx 2 -> aa
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Row row = readFirst(table.readByIndex(idxCol, idx2));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valAA });
        }
    });
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // swap value for aa to ab
            Assert.assertTrue(table.compareAndSwap(keyAA, valCol, valAA, valAB));
        }
    });
    // read by idx 2 -> ab
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Row row = readFirst(table.readByIndex(idxCol, idx2));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valAB });
        }
    });
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // swap value for aa to bb
            Assert.assertTrue(table.compareAndSwap(keyAA, valCol, valAB, valBB));
        }
    });
    // read by idx 2 -> bb (value of key aa)
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Row row = readFirst(table.readByIndex(idxCol, idx2));
            TableAssert.assertColumns(row, colIdxVal, new byte[][] { idx2, valBB });
        }
    });
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // swap value for aa to null
            Assert.assertTrue(table.compareAndSwap(keyAA, valCol, valBB, null));
        }
    });
    // read by idx 2 -> null (value of b)
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            Row row = readFirst(table.readByIndex(idxCol, idx2));
            TableAssert.assertColumn(row, idxCol, idx2);
        }
    });
    // start a new transaction
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // swap idx for c to 3
            Assert.assertTrue(table.compareAndSwap(keyC, idxCol, idx1, idx3));
        }
    });
    // read by idx 1 -> null (no row has that any more)
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            assertEmpty(table.readByIndex(idxCol, idx1));
            // read by idx 3 > c
            Row row = readFirst(table.readByIndex(idxCol, idx3));
            TableAssert.assertColumns(row, new byte[][] { idxCol, valCol }, new byte[][] { idx3, valC });
        }
    });
}
Also used : Delete(co.cask.cdap.api.dataset.table.Delete) Get(co.cask.cdap.api.dataset.table.Get) TransactionExecutor(org.apache.tephra.TransactionExecutor) Row(co.cask.cdap.api.dataset.table.Row) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 22 with Get

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

the class ProgramScheduleStoreDataset method findSchedules.

/**
   * Find all schedules that have a trigger with a given trigger key.
   *
   * @param triggerKey the trigger key to look up
   * @return a list of all schedules that are triggered by this key; never null
   */
public Collection<ProgramScheduleRecord> findSchedules(String triggerKey) {
    Map<ScheduleId, ProgramScheduleRecord> schedulesFound = new HashMap<>();
    try (Scanner scanner = store.readByIndex(TRIGGER_KEY_COLUMN_BYTES, Bytes.toBytes(triggerKey))) {
        Row triggerRow;
        while ((triggerRow = scanner.next()) != null) {
            String triggerRowKey = Bytes.toString(triggerRow.getRow());
            try {
                ScheduleId scheduleId = extractScheduleIdFromTriggerKey(triggerRowKey);
                if (schedulesFound.containsKey(scheduleId)) {
                    continue;
                }
                Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
                byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
                if (serialized == null) {
                    throw new NotFoundException(scheduleId);
                }
                ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
                ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row);
                ProgramScheduleRecord record = new ProgramScheduleRecord(schedule, meta);
                schedulesFound.put(scheduleId, record);
            } catch (IllegalArgumentException | NotFoundException e) {
                // the only exceptions we know to be thrown here are IllegalArgumentException (ill-formed key) or
                // NotFoundException (if the schedule does not exist). Both should never happen, so we warn and ignore.
                // we will let any other exception propagate up, because it would be a DataSetException or similarly serious.
                LOG.warn("Problem with trigger id '{}' found for trigger key '{}': {}. Skipping entry.", triggerRowKey, triggerKey, e.getMessage());
            }
        }
    }
    return schedulesFound.values();
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) ProgramScheduleMeta(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleMeta) HashMap(java.util.HashMap) NotFoundException(co.cask.cdap.common.NotFoundException) ScheduleId(co.cask.cdap.proto.id.ScheduleId) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Get(co.cask.cdap.api.dataset.table.Get) ProgramScheduleRecord(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) Row(co.cask.cdap.api.dataset.table.Row)

Example 23 with Get

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

the class ProgramScheduleStoreDataset method getSchedule.

/**
   * Read a schedule from the store.
   *
   * @param scheduleId the id of the schedule to read
   * @return the schedule from the store
   * @throws NotFoundException if the schedule does not exist in the store
   */
public ProgramSchedule getSchedule(ScheduleId scheduleId) throws NotFoundException {
    Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
    byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
    if (serialized == null) {
        throw new NotFoundException(scheduleId);
    }
    return GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
}
Also used : Get(co.cask.cdap.api.dataset.table.Get) NotFoundException(co.cask.cdap.common.NotFoundException) Row(co.cask.cdap.api.dataset.table.Row)

Example 24 with Get

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

the class BufferingTableTest method testMultiGetIncludesBuffer.

@Test
public void testMultiGetIncludesBuffer() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try {
        // persist some data
        BufferingTable table = getTable(CONTEXT1, MY_TABLE);
        Transaction tx1 = txClient.startShort();
        table.startTx(tx1);
        // writing a couple rows
        // table should look like the following, with everything in the buffer
        //          c1    c2    c3    c4
        // r1       1     2     3     -
        // r2       -     3     2     1
        table.put(R1, a(C1, C2, C3), lb(1, 2, 3));
        table.put(R2, a(C2, C3, C4), lb(3, 2, 1));
        // check that multi-get can see buffered writes
        List<Row> rows = table.get(Lists.newArrayList(new Get(R1), new Get(R2)));
        Assert.assertEquals(2, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C1, C2, C3), lb(1, 2, 3));
        TableAssert.assertRow(rows.get(1), R2, a(C2, C3, C4), lb(3, 2, 1));
        // check multi-get with gets that specify columns, and one get that should return an empty row
        rows = table.get(Lists.newArrayList(new Get(R1, C2, C3), new Get(R2, C2, C3), new Get(R3)));
        Assert.assertEquals(3, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C2, C3), lb(2, 3));
        TableAssert.assertRow(rows.get(1), R2, a(C2, C3), lb(3, 2));
        Assert.assertTrue(rows.get(2).isEmpty());
        // persist changes
        Collection<byte[]> txChanges = table.getTxChanges();
        Assert.assertTrue(txClient.canCommit(tx1, txChanges));
        Assert.assertTrue(table.commitTx());
        Assert.assertTrue(txClient.commit(tx1));
        table.postTxCommit();
        // start another transaction
        Transaction tx2 = txClient.startShort();
        table.startTx(tx2);
        // now add another row, delete a row, and change some column values
        // table should look like the following
        //         c1    c2    c3    c4    c5
        // r1      -     -     3     2     -
        // r3      -     -     -     -     1
        table.put(R1, a(C2, C3, C4), lb(4, 3, 2));
        table.delete(R1, a(C1, C2));
        table.delete(R2);
        table.put(R3, C5, L1);
        // verify multi-get sees persisted data with buffer applied on top
        rows = table.get(Lists.newArrayList(new Get(R1), new Get(R2), new Get(R3)));
        Assert.assertEquals(3, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C3, C4), lb(3, 2));
        Assert.assertTrue(rows.get(1).isEmpty());
        TableAssert.assertRow(rows.get(2), R3, a(C5), lb(1));
        // pretend there was a write conflict and rollback changes
        Assert.assertTrue(table.rollbackTx());
        txClient.abort(tx2);
        // start another transaction and make sure it can't see what was done before
        Transaction tx3 = txClient.startShort();
        table.startTx(tx3);
        rows = table.get(Lists.newArrayList(new Get(R1), new Get(R2)));
        Assert.assertEquals(2, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C1, C2, C3), lb(1, 2, 3));
        TableAssert.assertRow(rows.get(1), R2, a(C2, C3, C4), lb(3, 2, 1));
    } finally {
        admin.drop();
    }
}
Also used : Transaction(org.apache.tephra.Transaction) Get(co.cask.cdap.api.dataset.table.Get) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Row(co.cask.cdap.api.dataset.table.Row) Test(org.junit.Test)

Example 25 with Get

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

the class TopKCollector method readWordAssocs.

/**
   * Returns the top words associated with the specified word and the number
   * of times the words have appeared together.
   * @param word the word of interest
   * @param limit the number of associations to return, at most
   * @return a map of the top associated words to their co-occurrence count
   */
@ReadOnly
public Map<String, Long> readWordAssocs(String word, int limit) {
    // Retrieve all columns of the word’s row
    Row result = this.table.get(new Get(word));
    TopKCollector collector = new TopKCollector(limit);
    if (!result.isEmpty()) {
        // Iterate over all columns
        for (Map.Entry<byte[], byte[]> entry : result.getColumns().entrySet()) {
            collector.add(Bytes.toLong(entry.getValue()), Bytes.toString(entry.getKey()));
        }
    }
    return collector.getTopK();
}
Also used : Get(co.cask.cdap.api.dataset.table.Get) Row(co.cask.cdap.api.dataset.table.Row) TreeMap(java.util.TreeMap) Map(java.util.Map) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Aggregations

Get (co.cask.cdap.api.dataset.table.Get)31 Table (co.cask.cdap.api.dataset.table.Table)17 Row (co.cask.cdap.api.dataset.table.Row)16 Test (org.junit.Test)15 Put (co.cask.cdap.api.dataset.table.Put)13 TransactionAware (org.apache.tephra.TransactionAware)9 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)8 Transaction (org.apache.tephra.Transaction)8 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)7 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)6 TransactionExecutor (org.apache.tephra.TransactionExecutor)6 NotFoundException (co.cask.cdap.common.NotFoundException)5 IOException (java.io.IOException)4 ReadOnly (co.cask.cdap.api.annotation.ReadOnly)3 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)3 Delete (co.cask.cdap.api.dataset.table.Delete)3 Scanner (co.cask.cdap.api.dataset.table.Scanner)3 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 ApplicationManager (co.cask.cdap.test.ApplicationManager)3 FlowManager (co.cask.cdap.test.FlowManager)3