Search in sources :

Example 61 with Row

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

the class WorkflowDataset method delete.

public void delete(ApplicationId id) {
    MDSKey mdsKey = new MDSKey.Builder().add(id.getNamespace()).add(id.getApplication()).build();
    Scanner scanner = table.scan(mdsKey.getKey(), Bytes.stopKeyForPrefix(mdsKey.getKey()));
    Row row;
    try {
        while ((row = scanner.next()) != null) {
            table.delete(row.getRow());
        }
    } finally {
        scanner.close();
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Row(co.cask.cdap.api.dataset.table.Row)

Example 62 with Row

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

the class WorkflowDataset method scan.

/**
   * This function scans the workflow.stats dataset for a list of workflow runs in a time range.
   *
   * @param id The workflow id
   * @param timeRangeStart Start of the time range that the scan should begin from
   * @param timeRangeEnd End of the time range that the scan should end at
   * @return List of WorkflowRunRecords
   */
private List<WorkflowRunRecord> scan(WorkflowId id, long timeRangeStart, long timeRangeEnd) {
    byte[] startRowKey = getRowKeyBuilder(id, timeRangeStart).build().getKey();
    byte[] endRowKey = getRowKeyBuilder(id, timeRangeEnd).build().getKey();
    Scan scan = new Scan(startRowKey, endRowKey);
    Scanner scanner = table.scan(scan);
    Row indexRow;
    List<WorkflowRunRecord> workflowRunRecordList = new ArrayList<>();
    while ((indexRow = scanner.next()) != null) {
        Map<byte[], byte[]> columns = indexRow.getColumns();
        String workflowRunId = Bytes.toString(columns.get(RUNID));
        long timeTaken = Bytes.toLong(columns.get(TIME_TAKEN));
        List<ProgramRun> programRunList = GSON.fromJson(Bytes.toString(columns.get(NODES)), PROGRAM_RUNS_TYPE);
        WorkflowRunRecord workflowRunRecord = new WorkflowRunRecord(workflowRunId, timeTaken, programRunList);
        workflowRunRecordList.add(workflowRunRecord);
    }
    return workflowRunRecordList;
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) ArrayList(java.util.ArrayList) Scan(co.cask.cdap.api.dataset.table.Scan) Row(co.cask.cdap.api.dataset.table.Row)

Example 63 with Row

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

the class MockSink method clear.

/**
   * Clear any records written to this sink.
   *
   * @param tableManager dataset manager used to get the sink dataset
   */
public static void clear(DataSetManager<Table> tableManager) {
    tableManager.flush();
    Table table = tableManager.get();
    try (Scanner scanner = table.scan(null, null)) {
        Row row;
        while ((row = scanner.next()) != null) {
            table.delete(row.getRow());
        }
    }
    tableManager.flush();
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) Table(co.cask.cdap.api.dataset.table.Table) Row(co.cask.cdap.api.dataset.table.Row)

Example 64 with Row

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

the class MetricsDataMigrator method migrateMetricsData.

private void migrateMetricsData(EntityTable entityTable, MetricsTable metricsTable, String scope, Version version) {
    MetricsEntityCodec codec = getEntityCodec(entityTable);
    int idSize = getIdSize(version);
    Row row;
    long rowCount = 0;
    try {
        Scanner scanner = metricsTable.scan(null, null, null);
        while ((row = scanner.next()) != null) {
            byte[] rowKey = row.getRow();
            int offset = 0;
            String context = codec.decode(MetricsEntityType.CONTEXT, rowKey, offset, idSize);
            context = getContextBasedOnVersion(context, version);
            offset += codec.getEncodedSize(MetricsEntityType.CONTEXT, idSize);
            String metricName = codec.decode(MetricsEntityType.METRIC, rowKey, offset, idSize);
            offset += codec.getEncodedSize(MetricsEntityType.METRIC, idSize);
            scope = getScopeBasedOnVersion(scope, metricName, version);
            metricName = getMetricNameBasedOnVersion(metricName, version);
            String runId = codec.decode(MetricsEntityType.RUN, rowKey, offset, idSize);
            parseAndAddNewMetricValue(scope, context, metricName, runId, row.getColumns());
            rowCount++;
            printStatus(rowCount);
        }
        System.out.println("Migrated " + rowCount + " records");
    } catch (Exception e) {
        LOG.warn("Exception during data-transfer in aggregates table", e);
    //no-op
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) Row(co.cask.cdap.api.dataset.table.Row) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 65 with Row

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

the class PartitionedFileSetDataset method addMetadata.

@WriteOnly
@Override
public void addMetadata(PartitionKey key, Map<String, String> metadata) {
    final byte[] rowKey = generateRowKey(key, partitioning);
    Row row = partitionsTable.get(rowKey);
    if (row.isEmpty()) {
        throw new PartitionNotFoundException(key, getName());
    }
    // ensure that none of the entries already exist in the metadata
    for (Map.Entry<String, String> metadataEntry : metadata.entrySet()) {
        String metadataKey = metadataEntry.getKey();
        byte[] columnKey = columnKeyFromMetadataKey(metadataKey);
        if (row.get(columnKey) != null) {
            throw new DataSetException(String.format("Entry already exists for metadata key: %s", metadataKey));
        }
    }
    Put put = new Put(rowKey);
    addMetadataToPut(metadata, put);
    partitionsTable.put(put);
}
Also used : PartitionNotFoundException(co.cask.cdap.api.dataset.PartitionNotFoundException) DataSetException(co.cask.cdap.api.dataset.DataSetException) Row(co.cask.cdap.api.dataset.table.Row) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Put(co.cask.cdap.api.dataset.table.Put) WriteOnly(co.cask.cdap.api.annotation.WriteOnly)

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