Search in sources :

Example 11 with ReadOnly

use of co.cask.cdap.api.annotation.ReadOnly in project cdap by caskdata.

the class HBaseTable method scanPersisted.

@ReadOnly
@Override
protected Scanner scanPersisted(co.cask.cdap.api.dataset.table.Scan scan) throws Exception {
    ScanBuilder hScan = tableUtil.buildScan();
    hScan.addFamily(columnFamily);
    // todo: should be configurable
    // NOTE: by default we assume scanner is used in mapreduce job, hence no cache blocks
    hScan.setCacheBlocks(false);
    hScan.setCaching(1000);
    byte[] startRow = scan.getStartRow();
    byte[] stopRow = scan.getStopRow();
    if (startRow != null) {
        hScan.setStartRow(startRow);
    }
    if (stopRow != null) {
        hScan.setStopRow(stopRow);
    }
    setFilterIfNeeded(hScan, scan.getFilter());
    hScan.setAttribute(TxConstants.TX_OPERATION_ATTRIBUTE_KEY, txCodec.encode(tx));
    ResultScanner resultScanner = wrapResultScanner(hTable.getScanner(hScan.build()));
    return new HBaseScanner(resultScanner, columnFamily);
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ScanBuilder(co.cask.cdap.data2.util.hbase.ScanBuilder) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Example 12 with ReadOnly

use of co.cask.cdap.api.annotation.ReadOnly in project cdap by caskdata.

the class LevelDBTable method scanPersisted.

@ReadOnly
@Override
protected Scanner scanPersisted(Scan scan) throws Exception {
    FuzzyRowFilter filter = null;
    if (scan.getFilter() != null) {
        // todo: currently we support only FuzzyRowFilter as an experimental feature
        if (scan.getFilter() instanceof FuzzyRowFilter) {
            filter = (FuzzyRowFilter) scan.getFilter();
        } else {
            throw new DataSetException("Unknown filter type: " + scan.getFilter());
        }
    }
    final Scanner scanner = core.scan(scan.getStartRow(), scan.getStopRow(), filter, null, tx);
    return new Scanner() {

        @Nullable
        @Override
        public Row next() {
            return LevelDBTable.this.next(scanner);
        }

        @Override
        public void close() {
            scanner.close();
        }
    };
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) DataSetException(co.cask.cdap.api.dataset.DataSetException) FuzzyRowFilter(co.cask.cdap.data2.dataset2.lib.table.FuzzyRowFilter) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Example 13 with ReadOnly

use of co.cask.cdap.api.annotation.ReadOnly 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)

Example 14 with ReadOnly

use of co.cask.cdap.api.annotation.ReadOnly in project cdap by caskdata.

the class KeyValueTable method readAll.

/**
   * Reads the values for an array of given keys.
   *
   * @param keys the keys to be read
   * @return a map of the stored values, keyed by key
   */
@ReadOnly
public Map<byte[], byte[]> readAll(byte[][] keys) {
    List<Get> gets = new ArrayList<>(keys.length);
    for (byte[] key : keys) {
        gets.add(new Get(key).add(KEY_COLUMN));
    }
    List<Row> results = table.get(gets);
    Map<byte[], byte[]> values = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    for (Row row : results) {
        if (row.get(KEY_COLUMN) != null) {
            values.put(row.getRow(), row.get(KEY_COLUMN));
        }
    }
    return values;
}
Also used : Get(co.cask.cdap.api.dataset.table.Get) ArrayList(java.util.ArrayList) Row(co.cask.cdap.api.dataset.table.Row) TreeMap(java.util.TreeMap) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Aggregations

ReadOnly (co.cask.cdap.api.annotation.ReadOnly)14 DataSetException (co.cask.cdap.api.dataset.DataSetException)6 Row (co.cask.cdap.api.dataset.table.Row)5 IOException (java.io.IOException)5 Result (co.cask.cdap.api.dataset.table.Result)4 Get (co.cask.cdap.api.dataset.table.Get)3 Scanner (co.cask.cdap.api.dataset.table.Scanner)3 RecordScanner (co.cask.cdap.api.data.batch.RecordScanner)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 NavigableMap (java.util.NavigableMap)2 TreeMap (java.util.TreeMap)2 FuzzyRowFilter (co.cask.cdap.data2.dataset2.lib.table.FuzzyRowFilter)1 ScanBuilder (co.cask.cdap.data2.util.hbase.ScanBuilder)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 Get (org.apache.hadoop.hbase.client.Get)1 Result (org.apache.hadoop.hbase.client.Result)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1