Search in sources :

Example 11 with Scan

use of io.cdap.cdap.api.dataset.table.Scan in project cdap by caskdata.

the class MetadataStoreDataset method listKV.

/**
 * Returns mapping of all that match the given keySet provided they pass the combinedFilter predicate
 * for default COLUMN
 *
 * @param keySet row key set
 * @param typeOfT the type of the result
 * @param limit limit number of result
 * @param combinedFilter filter for key
 * @return map of row key to result
 */
public <T> Map<MDSKey, T> listKV(Set<MDSKey> keySet, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) {
    // Sort fuzzy keys
    List<MDSKey> sortedKeys = Lists.newArrayList(keySet);
    Collections.sort(sortedKeys);
    // Scan using fuzzy filter
    byte[] startKey = sortedKeys.get(0).getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(sortedKeys.get(sortedKeys.size() - 1).getKey());
    List<ImmutablePair<byte[], byte[]>> fuzzyKeys = new ArrayList<>();
    for (MDSKey key : sortedKeys) {
        fuzzyKeys.add(getFuzzyKeyFor(key));
    }
    Scan scan = new Scan(startKey, stopKey, new FuzzyRowFilter(fuzzyKeys));
    return listCombinedFilterKV(scan, typeOfT, limit, combinedFilter);
}
Also used : ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) ArrayList(java.util.ArrayList) Scan(io.cdap.cdap.api.dataset.table.Scan)

Example 12 with Scan

use of io.cdap.cdap.api.dataset.table.Scan in project cdap by caskdata.

the class MetadataStoreDataset method listKV.

/**
 * Return mapping of all that has the prefix of the given mds key and the value with it.
 *
 * @param startId prefix row key.
 * @param limit limit number of result.
 * @return map of row key to result.
 */
public Map<MDSKey, byte[]> listKV(MDSKey startId, int limit) {
    byte[] startKey = startId.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
    Scan scan = new Scan(startKey, stopKey);
    Map<MDSKey, byte[]> map = new LinkedHashMap<>();
    try (Scanner scanner = table.scan(scan)) {
        Row next;
        while ((limit > 0) && (next = scanner.next()) != null) {
            MDSKey key = new MDSKey(next.getRow());
            byte[] value = next.get(COLUMN);
            if (value == null) {
                continue;
            }
            map.put(key, value);
            limit--;
        }
    }
    return map;
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) Scan(io.cdap.cdap.api.dataset.table.Scan) Row(io.cdap.cdap.api.dataset.table.Row) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with Scan

use of io.cdap.cdap.api.dataset.table.Scan in project cdap by caskdata.

the class MetadataDataset method getMetadata.

/**
 * Returns metadata for a given set of entities
 *
 * @param metadataEntitys entities for which metadata is required
 * @return map of entitiyId to set of metadata for that entity
 */
public Set<Record> getMetadata(Set<? extends MetadataEntity> metadataEntitys) {
    if (metadataEntitys.isEmpty()) {
        return Collections.emptySet();
    }
    List<ImmutablePair<byte[], byte[]>> fuzzyKeys = new ArrayList<>(metadataEntitys.size());
    for (MetadataEntity metadataEntity : metadataEntitys) {
        fuzzyKeys.add(getFuzzyKeyFor(metadataEntity));
    }
    // Sort fuzzy keys
    fuzzyKeys.sort(FUZZY_KEY_COMPARATOR);
    // Scan using fuzzy filter. Scan returns one row per property.
    // Group the rows on namespacedId
    Multimap<MetadataEntity, MetadataEntry> metadataMap = HashMultimap.create();
    byte[] start = fuzzyKeys.get(0).getFirst();
    byte[] end = Bytes.stopKeyForPrefix(fuzzyKeys.get(fuzzyKeys.size() - 1).getFirst());
    try (Scanner scan = indexedTable.scan(new Scan(start, end, new FuzzyRowFilter(fuzzyKeys)))) {
        Row next;
        while ((next = scan.next()) != null) {
            MetadataEntry metadataEntry = convertRow(next);
            if (metadataEntry != null) {
                metadataMap.put(metadataEntry.getMetadataEntity(), metadataEntry);
            }
        }
    }
    // Create metadata objects for each entity from grouped rows
    Set<Record> metadataSet = new HashSet<>();
    for (Map.Entry<MetadataEntity, Collection<MetadataEntry>> entry : metadataMap.asMap().entrySet()) {
        Map<String, String> properties = new HashMap<>();
        Set<String> tags = Collections.emptySet();
        for (MetadataEntry metadataEntry : entry.getValue()) {
            if (MetadataConstants.TAGS_KEY.equals(metadataEntry.getKey())) {
                tags = splitTags(metadataEntry.getValue());
            } else {
                properties.put(metadataEntry.getKey(), metadataEntry.getValue());
            }
        }
        metadataSet.add(new Record(entry.getKey(), properties, tags));
    }
    return metadataSet;
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Scanner(io.cdap.cdap.api.dataset.table.Scanner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FuzzyRowFilter(io.cdap.cdap.data2.dataset2.lib.table.FuzzyRowFilter) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) Collection(java.util.Collection) Scan(io.cdap.cdap.api.dataset.table.Scan) Row(io.cdap.cdap.api.dataset.table.Row) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

Scan (io.cdap.cdap.api.dataset.table.Scan)13 Transaction (org.apache.tephra.Transaction)7 Scanner (io.cdap.cdap.api.dataset.table.Scanner)6 Test (org.junit.Test)6 Table (io.cdap.cdap.api.dataset.table.Table)5 TransactionAware (org.apache.tephra.TransactionAware)5 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)4 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)4 Row (io.cdap.cdap.api.dataset.table.Row)3 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)3 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)3 ImmutableList (com.google.common.collect.ImmutableList)2 Get (io.cdap.cdap.api.dataset.table.Get)2 ImmutablePair (io.cdap.cdap.common.utils.ImmutablePair)2 BufferingTable (io.cdap.cdap.data2.dataset2.lib.table.BufferingTable)2 BufferingTableTest (io.cdap.cdap.data2.dataset2.lib.table.BufferingTableTest)2 TableId (io.cdap.cdap.data2.util.TableId)2 DelegatingTable (io.cdap.cdap.data2.util.hbase.DelegatingTable)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2