Search in sources :

Example 81 with Row

use of co.cask.cdap.api.dataset.table.Row 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 82 with Row

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

the class DatasetBasedTimeScheduleStore method upgradeJobs.

private void upgradeJobs(Table table) {
    Row result = table.get(JOB_KEY);
    if (result.isEmpty()) {
        return;
    }
    for (byte[] column : result.getColumns().values()) {
        JobDetail oldJobDetail = (JobDetail) SerializationUtils.deserialize(column);
        JobDetail jobDetail = addDefaultAppVersionIfNeeded(oldJobDetail);
        if (!jobDetail.equals(oldJobDetail)) {
            // Write the new jobDetail only if the upgraded key doesn't exist already
            if (readJob(table, jobDetail.getKey()) == null) {
                persistJob(table, jobDetail);
            }
            removeJob(table, oldJobDetail.getKey());
        }
    }
}
Also used : JobDetail(org.quartz.JobDetail) Row(co.cask.cdap.api.dataset.table.Row)

Example 83 with Row

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

the class DatasetBasedTimeScheduleStore method readTrigger.

private TriggerStatusV2 readTrigger(TriggerKey key) {
    byte[][] col = new byte[1][];
    col[0] = Bytes.toBytes(key.getName());
    Row result = table.get(TRIGGER_KEY, col);
    byte[] bytes = null;
    if (!result.isEmpty()) {
        bytes = result.get(col[0]);
    }
    if (bytes != null) {
        return (TriggerStatusV2) SerializationUtils.deserialize(bytes);
    } else {
        return null;
    }
}
Also used : Row(co.cask.cdap.api.dataset.table.Row)

Example 84 with Row

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

the class FactScanner method createIterator.

private Iterator<FactScanResult> createIterator() {
    return new AbstractIterator<FactScanResult>() {

        @Override
        protected FactScanResult computeNext() {
            Row rowResult;
            while ((rowResult = scanner.next()) != null) {
                rowScanned++;
                byte[] rowKey = rowResult.getRow();
                // Decode context and metric from key
                String measureName = codec.getMeasureName(rowKey);
                // if measureNames is empty we include all metrics
                if (!measureNames.isEmpty() && !measureNames.contains(measureName)) {
                    continue;
                }
                // todo: codec.getDimensionValues(rowKey) needs to un-encode dimension names which may result in read in
                //       entity table (depending on the cache and its state). To avoid that, we can pass to scanner the
                //       list of dimension names as we *always* know it (it is given) at the time of scanning
                List<DimensionValue> dimensionValues = codec.getDimensionValues(rowKey);
                boolean exhausted = false;
                List<TimeValue> timeValues = Lists.newLinkedList();
                // todo: entry set is ordered by ts?
                for (Map.Entry<byte[], byte[]> columnValue : rowResult.getColumns().entrySet()) {
                    long ts = codec.getTimestamp(rowKey, columnValue.getKey());
                    if (ts < startTs) {
                        continue;
                    }
                    if (ts > endTs) {
                        exhausted = true;
                        break;
                    }
                    // todo: move Bytes.toLong into codec?
                    TimeValue timeValue = new TimeValue(ts, Bytes.toLong(columnValue.getValue()));
                    timeValues.add(timeValue);
                }
                if (timeValues.isEmpty() && exhausted) {
                    break;
                }
                // todo: can return empty list, if all data is < startTs or > endTs
                return new FactScanResult(measureName, dimensionValues, timeValues);
            }
            scanner.close();
            return endOfData();
        }
    };
}
Also used : DimensionValue(co.cask.cdap.api.dataset.lib.cube.DimensionValue) AbstractIterator(com.google.common.collect.AbstractIterator) Row(co.cask.cdap.api.dataset.table.Row) Map(java.util.Map) TimeValue(co.cask.cdap.api.dataset.lib.cube.TimeValue)

Example 85 with Row

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

the class MetricsTableTest method testFuzzyScan.

@Test
public void testFuzzyScan() throws Exception {
    MetricsTable table = getTable("testFuzzyScan");
    NavigableMap<byte[], SortedMap<byte[], Long>> writes = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    byte[] abc = { 'a', 'b', 'c' };
    for (byte b1 : abc) {
        for (byte b2 : abc) {
            for (byte b3 : abc) {
                for (byte b4 : abc) {
                    // we put two columns, but will scan only one column
                    writes.put(new byte[] { b1, b2, b3, b4 }, mapOf(A, Bytes.toLong(X)));
                }
            }
        }
    }
    table.put(writes);
    // we should have 81 (3^4) rows now
    Assert.assertEquals(81, countRange(table, null, null));
    // now do a fuzzy scan of the table
    FuzzyRowFilter filter = new FuzzyRowFilter(ImmutableList.of(ImmutablePair.of(new byte[] { '*', 'b', '*', 'b' }, new byte[] { 0x01, 0x00, 0x01, 0x00 })));
    Scanner scanner = table.scan(null, null, filter);
    int count = 0;
    while (true) {
        Row entry = scanner.next();
        if (entry == null) {
            break;
        }
        Assert.assertTrue(entry.getRow()[1] == 'b' && entry.getRow()[3] == 'b');
        Assert.assertEquals(1, entry.getColumns().size());
        Assert.assertTrue(entry.getColumns().containsKey(A));
        count++;
    }
    Assert.assertEquals(9, count);
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) Row(co.cask.cdap.api.dataset.table.Row) Test(org.junit.Test)

Aggregations

Row (co.cask.cdap.api.dataset.table.Row)111 Scanner (co.cask.cdap.api.dataset.table.Scanner)60 Test (org.junit.Test)23 Table (co.cask.cdap.api.dataset.table.Table)20 Get (co.cask.cdap.api.dataset.table.Get)16 ArrayList (java.util.ArrayList)16 TransactionExecutor (org.apache.tephra.TransactionExecutor)16 Map (java.util.Map)15 Put (co.cask.cdap.api.dataset.table.Put)14 HashMap (java.util.HashMap)10 Scan (co.cask.cdap.api.dataset.table.Scan)9 TransactionAware (org.apache.tephra.TransactionAware)9 MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)8 QueueEntryRow (co.cask.cdap.data2.transaction.queue.QueueEntryRow)8 DatasetId (co.cask.cdap.proto.id.DatasetId)8 IOException (java.io.IOException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Transaction (org.apache.tephra.Transaction)7 WriteOnly (co.cask.cdap.api.annotation.WriteOnly)6 Schema (co.cask.cdap.api.data.schema.Schema)6