Search in sources :

Example 71 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class MetadataStoreDataset method scan.

/**
 * Run a scan on MDS for default COLUMN
 *
 * @param startId  scan start key
 * @param stopId   scan stop key
 * @param typeOfT  type of value
 * @param function function to process each element returned from scan.
 *                 If function.apply returns false then the scan is stopped.
 *                 Also, function.apply should not return null.
 * @param <T>      type of value
 */
public <T> void scan(MDSKey startId, @Nullable MDSKey stopId, Type typeOfT, Function<KeyValue<T>, Boolean> function) {
    byte[] startKey = startId.getKey();
    byte[] stopKey = stopId == null ? Bytes.stopKeyForPrefix(startKey) : stopId.getKey();
    try (Scanner scan = table.scan(startKey, stopKey)) {
        Row next;
        while ((next = scan.next()) != null) {
            byte[] columnValue = next.get(COLUMN);
            if (columnValue == null) {
                continue;
            }
            MDSKey key = new MDSKey(next.getRow());
            T value = deserialize(key, columnValue, typeOfT);
            // noinspection ConstantConditions
            if (!function.apply(new KeyValue<>(key, value))) {
                break;
            }
        }
    }
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) Row(io.cdap.cdap.api.dataset.table.Row)

Example 72 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class MetadataStoreDataset method exists.

/**
 * Check whether the value is there in the row and default COLUMN
 *
 * @param id the mds key for the row
 * @return a boolean which indicates the value exists or not
 */
public boolean exists(MDSKey id) {
    Row row = table.get(id.getKey());
    if (row.isEmpty()) {
        return false;
    }
    byte[] value = row.get(COLUMN);
    return value != null;
}
Also used : Row(io.cdap.cdap.api.dataset.table.Row)

Example 73 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class MetadataStoreDataset method getFirst.

/**
 * Get the first element in the row and default COLUMN, of type T
 *
 * @param id the mds key for the row
 * @param typeOfT the type of the result
 * @return the deserialized value of the result, null if not exist
 */
@Nullable
public <T> T getFirst(MDSKey id, Type typeOfT) {
    try {
        try (Scanner scan = table.scan(id.getKey(), Bytes.stopKeyForPrefix(id.getKey()))) {
            Row row = scan.next();
            if (row == null || row.isEmpty()) {
                return null;
            }
            byte[] value = row.get(COLUMN);
            if (value == null) {
                return null;
            }
            return deserialize(id, value, typeOfT);
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) Row(io.cdap.cdap.api.dataset.table.Row) Nullable(javax.annotation.Nullable)

Example 74 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class MetadataStoreDataset method getKV.

/**
 * Get all non-null values with the given ids for default COLUMN in a map
 *
 * @param ids set of the mds keys
 * @return a map of the deserialized value of the result
 */
protected Map<MDSKey, byte[]> getKV(Set<MDSKey> ids) {
    Map<MDSKey, byte[]> resultMap = new HashMap<>();
    List<Get> getList = new ArrayList<>();
    for (MDSKey id : ids) {
        getList.add(new Get(id.getKey()));
    }
    List<Row> rowList = table.get(getList);
    for (Row row : rowList) {
        if (row.isEmpty()) {
            continue;
        }
        byte[] value = row.get(COLUMN);
        if (value == null) {
            continue;
        }
        MDSKey key = new MDSKey(row.getRow());
        resultMap.put(key, value);
    }
    return resultMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Get(io.cdap.cdap.api.dataset.table.Get) ArrayList(java.util.ArrayList) Row(io.cdap.cdap.api.dataset.table.Row)

Example 75 with Row

use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.

the class LevelDBTableCore method deleteRange.

public void deleteRange(byte[] startRow, byte[] stopRow, @Nullable FuzzyRowFilter filter, @Nullable byte[][] columns) throws IOException {
    if (columns != null) {
        if (columns.length == 0) {
            return;
        }
        columns = Arrays.copyOf(columns, columns.length);
        Arrays.sort(columns, Bytes.BYTES_COMPARATOR);
    }
    DB db = getDB();
    DBIterator iterator = db.iterator();
    seekToStart(iterator, startRow);
    byte[] endKey = stopRow == null ? null : createStartKey(stopRow);
    DBIterator deleteIterator = db.iterator();
    seekToStart(deleteIterator, startRow);
    // todo make configurable
    final int deletesPerRound = 1024;
    try (Scanner scanner = new LevelDBScanner(iterator, endKey, filter, columns, null)) {
        Row rowValues;
        WriteBatch batch = db.createWriteBatch();
        int deletesInBatch = 0;
        // go through all matching cells and delete them in batches.
        while ((rowValues = scanner.next()) != null) {
            byte[] row = rowValues.getRow();
            for (byte[] column : rowValues.getColumns().keySet()) {
                addToDeleteBatch(batch, deleteIterator, row, column);
                deletesInBatch++;
                // perform the deletes when we have built up a batch.
                if (deletesInBatch >= deletesPerRound) {
                    // delete all the entries that were found
                    db.write(batch, getWriteOptions());
                    batch = db.createWriteBatch();
                    deletesInBatch = 0;
                }
            }
        }
        // perform any outstanding deletes
        if (deletesInBatch > 0) {
            db.write(batch, getWriteOptions());
        }
    } finally {
        deleteIterator.close();
    }
}
Also used : DBIterator(org.iq80.leveldb.DBIterator) Scanner(io.cdap.cdap.api.dataset.table.Scanner) Row(io.cdap.cdap.api.dataset.table.Row) WriteBatch(org.iq80.leveldb.WriteBatch) DB(org.iq80.leveldb.DB)

Aggregations

Row (io.cdap.cdap.api.dataset.table.Row)166 Scanner (io.cdap.cdap.api.dataset.table.Scanner)81 Test (org.junit.Test)50 Table (io.cdap.cdap.api.dataset.table.Table)34 Put (io.cdap.cdap.api.dataset.table.Put)29 ArrayList (java.util.ArrayList)26 TransactionExecutor (org.apache.tephra.TransactionExecutor)26 Get (io.cdap.cdap.api.dataset.table.Get)24 Schema (io.cdap.cdap.api.data.schema.Schema)21 HashMap (java.util.HashMap)19 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)16 Transaction (org.apache.tephra.Transaction)16 TransactionAware (org.apache.tephra.TransactionAware)16 IOException (java.io.IOException)14 Map (java.util.Map)14 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)13 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)12 WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)10 DimensionValue (io.cdap.cdap.api.dataset.lib.cube.DimensionValue)10 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)10