Search in sources :

Example 11 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class BufferingTable method get.

/**
 * NOTE: Depending on the use-case, calling this method may be much less
 *       efficient than calling same method with columns as parameters because it may always require round trip to
 *       persistent store
 */
@ReadOnly
@Override
public Row get(byte[] row) {
    ensureTransactionIsStarted();
    reportRead(1);
    try {
        return new Result(row, getRowMap(row));
    } catch (Exception e) {
        LOG.debug("get failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("get failed", e);
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) Result(io.cdap.cdap.api.dataset.table.Result) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 12 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class IndexedObjectStore method readAllByIndex.

/**
 * Read all the objects from the objectStore for a given index. Returns all the objects that match the secondaryKey.
 * Returns an empty list if no values are found. Never returns null.
 *
 * @param secondaryKey for the lookup.
 * @return List of Objects matching the secondaryKey.
 */
@ReadOnly
public List<T> readAllByIndex(byte[] secondaryKey) {
    List<T> resultList = new ArrayList<>();
    // Lookup the secondaryKey and get all the keys in primary
    // Each row with secondaryKey as rowKey contains column named as the primary key
    // of every object that can be looked up using the secondaryKey
    Row row = index.get(secondaryKey);
    // if the index has no match, return nothing
    if (!row.isEmpty()) {
        resultList = row.getColumns().keySet().stream().map(objectStore::read).collect(Collectors.toList());
    }
    return Collections.unmodifiableList(resultList);
}
Also used : ArrayList(java.util.ArrayList) Row(io.cdap.cdap.api.dataset.table.Row) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 13 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class IndexedTable method readByIndex.

/**
 * Reads table rows by the given secondary index key.  If no rows are indexed by the given key, then a
 * {@link io.cdap.cdap.api.dataset.table.Scanner} with no results will be returned.
 *
 * @return a Scanner returning rows from the data table, whose stored value for the given column matches the
 * given value.
 * @throws java.lang.IllegalArgumentException if the given column is not configured for indexing.
 */
@ReadOnly
public Scanner readByIndex(byte[] column, byte[] value) {
    assertIndexedColumn(column);
    byte[] rowKeyPrefix = Bytes.concat(keyPrefix, column, KEY_DELIMITER, value, KEY_DELIMITER);
    byte[] stopRow = Bytes.stopKeyForPrefix(rowKeyPrefix);
    Scanner indexScan = index.scan(rowKeyPrefix, stopRow);
    return new IndexScanner(indexScan, column, value);
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) RecordScanner(io.cdap.cdap.api.data.batch.RecordScanner) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 14 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class HBaseTable method scanPersisted.

@ReadOnly
@Override
protected Scanner scanPersisted(io.cdap.cdap.api.dataset.table.Scan scan) throws Exception {
    ScanBuilder hScan = tableUtil.buildScan();
    hScan.addFamily(columnFamily);
    // TODO (CDAP-11954): use common utility method to extract these configs
    if (scan.getProperties().containsKey(CONFIG_HBASE_CLIENT_CACHE_BLOCKS)) {
        hScan.setCacheBlocks(Boolean.valueOf(scan.getProperties().get(CONFIG_HBASE_CLIENT_CACHE_BLOCKS)));
    } else if (arguments.containsKey(CONFIG_HBASE_CLIENT_CACHE_BLOCKS)) {
        hScan.setCacheBlocks(Boolean.valueOf(arguments.get(CONFIG_HBASE_CLIENT_CACHE_BLOCKS)));
    } else if (properties.containsKey(CONFIG_HBASE_CLIENT_CACHE_BLOCKS)) {
        hScan.setCacheBlocks(Boolean.valueOf(properties.get(CONFIG_HBASE_CLIENT_CACHE_BLOCKS)));
    } else {
        // NOTE: by default we assume scanner is used in mapreduce job, hence no cache blocks
        hScan.setCacheBlocks(false);
    }
    if (scan.getProperties().containsKey(CONFIG_HBASE_CLIENT_SCANNER_CACHING)) {
        hScan.setCaching(Integer.valueOf(scan.getProperties().get(CONFIG_HBASE_CLIENT_SCANNER_CACHING)));
    } else if (arguments.containsKey(CONFIG_HBASE_CLIENT_SCANNER_CACHING)) {
        hScan.setCaching(Integer.valueOf(arguments.get(CONFIG_HBASE_CLIENT_SCANNER_CACHING)));
    } else if (properties.containsKey(CONFIG_HBASE_CLIENT_SCANNER_CACHING)) {
        hScan.setCaching(Integer.valueOf(properties.get(CONFIG_HBASE_CLIENT_SCANNER_CACHING)));
    } else {
        // NOTE: by default we use this hard-coded value, for backwards-compatibility with CDAP<4.1.2|4.2.1|4.3
        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, getEncodedTx());
    ResultScanner resultScanner = wrapResultScanner(table.getScanner(hScan.build()));
    return new HBaseScanner(resultScanner, columnFamily);
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ScanBuilder(io.cdap.cdap.data2.util.hbase.ScanBuilder) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 15 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class HBaseTable method getInternal.

// columns being null means to get all rows; empty columns means get no rows.
@ReadOnly
private NavigableMap<byte[], byte[]> getInternal(byte[] row, @Nullable byte[][] columns) throws IOException {
    if (columns != null && columns.length == 0) {
        return EMPTY_ROW_MAP;
    }
    Get get = createGet(row, columns);
    Result result = table.get(get);
    // no tx logic needed
    if (tx == null) {
        return result.isEmpty() ? EMPTY_ROW_MAP : result.getFamilyMap(columnFamily);
    }
    return getRowMap(result, columnFamily);
}
Also used : Get(org.apache.hadoop.hbase.client.Get) Result(org.apache.hadoop.hbase.client.Result) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Aggregations

ReadOnly (io.cdap.cdap.api.annotation.ReadOnly)26 DataSetException (io.cdap.cdap.api.dataset.DataSetException)14 IOException (java.io.IOException)10 Result (io.cdap.cdap.api.dataset.table.Result)8 Row (io.cdap.cdap.api.dataset.table.Row)8 Scanner (io.cdap.cdap.api.dataset.table.Scanner)6 RecordScanner (io.cdap.cdap.api.data.batch.RecordScanner)4 NavigableMap (java.util.NavigableMap)4 PartitionAlreadyExistsException (io.cdap.cdap.api.dataset.lib.PartitionAlreadyExistsException)2 Get (io.cdap.cdap.api.dataset.table.Get)2 FuzzyRowFilter (io.cdap.cdap.data2.dataset2.lib.table.FuzzyRowFilter)2 ScanBuilder (io.cdap.cdap.data2.util.hbase.ScanBuilder)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 Get (org.apache.hadoop.hbase.client.Get)2 Result (org.apache.hadoop.hbase.client.Result)2 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)2 Location (org.apache.twill.filesystem.Location)2