Search in sources :

Example 66 with Row

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

the class MetadataDataset method removeMetadata.

/**
 * Removes all keys that satisfy a given predicate from the metadata of the specified {@link MetadataEntity}.
 * @param metadataEntity the {@link MetadataEntity} for which keys are to be removed
 * @param filter the {@link Predicate} that should be satisfied to remove a key
 */
private Change removeMetadata(MetadataEntity metadataEntity, Predicate<String> filter) {
    MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, null);
    byte[] prefix = mdsKey.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
    Map<String, String> existingMetadata = new HashMap<>();
    Map<String, String> deletedMetadata = new HashMap<>();
    try (Scanner scan = indexedTable.scan(prefix, stopKey)) {
        Row next;
        while ((next = scan.next()) != null) {
            String value = next.getString(VALUE_COLUMN);
            if (value == null) {
                continue;
            }
            String metadataKey = MetadataKey.extractMetadataKey(next.getRow());
            // put all the metadata for this entity as existing
            existingMetadata.put(metadataKey, value);
            if (filter.test(metadataKey)) {
                // if the key matches the key to be deleted delete it and put it in deleted
                indexedTable.delete(new Delete(next.getRow()));
                // store the key to delete its indexes later
                deletedMetadata.put(metadataKey, value);
            }
        }
    }
    // current metadata is existing - deleted
    Map<String, String> currentMetadata = new HashMap<>(existingMetadata);
    // delete all the indexes for all deleted metadata key
    for (String deletedMetadataKey : deletedMetadata.keySet()) {
        deleteIndexes(metadataEntity, deletedMetadataKey);
        currentMetadata.remove(deletedMetadataKey);
    }
    Record changedMetadata = getMetadata(metadataEntity, currentMetadata);
    writeHistory(changedMetadata);
    return new Change(getMetadata(metadataEntity, existingMetadata), changedMetadata);
}
Also used : Delete(io.cdap.cdap.api.dataset.table.Delete) Scanner(io.cdap.cdap.api.dataset.table.Scanner) HashMap(java.util.HashMap) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Row(io.cdap.cdap.api.dataset.table.Row)

Example 67 with Row

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

the class MetadataDataset method getMetadata.

/**
 * Return metadata based on target id, and key.
 *
 * @param metadataEntity The id of the target
 * @param key The metadata key to get
 * @return instance of {@link MetadataEntry} for the target type, id, and key
 */
@Nullable
private MetadataEntry getMetadata(MetadataEntity metadataEntity, String key) {
    MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, key);
    Row row = indexedTable.get(mdsKey.getKey());
    if (row.isEmpty()) {
        return null;
    }
    byte[] value = row.get(VALUE_COLUMN);
    if (value == null) {
        // This can happen when all tags are removed one by one. The row still exists, but the value is null.
        return null;
    }
    return new MetadataEntry(metadataEntity, key, Bytes.toString(value));
}
Also used : MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Row(io.cdap.cdap.api.dataset.table.Row) Nullable(javax.annotation.Nullable)

Example 68 with Row

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

the class MetadataStoreDataset method listCombinedFilterKV.

private <T> Map<MDSKey, T> listCombinedFilterKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) {
    try {
        Map<MDSKey, T> map = Maps.newLinkedHashMap();
        try (Scanner scan = table.scan(runScan)) {
            Row next;
            while ((limit > 0) && (next = scan.next()) != null) {
                MDSKey key = new MDSKey(next.getRow());
                byte[] columnValue = next.get(COLUMN);
                if (columnValue == null) {
                    continue;
                }
                T value = deserialize(key, columnValue, typeOfT);
                KeyValue<T> kv = new KeyValue<>(key, value);
                // Combined Filter doesn't pass
                if (combinedFilter != null && !combinedFilter.test(kv)) {
                    continue;
                }
                map.put(kv.getKey(), kv.getValue());
                limit--;
            }
            return map;
        }
    } 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)

Example 69 with Row

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

the class MetadataStoreDataset method listKV.

private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) {
    try {
        Map<MDSKey, T> map = Maps.newLinkedHashMap();
        try (Scanner scan = table.scan(runScan)) {
            Row next;
            while ((limit > 0) && (next = scan.next()) != null) {
                MDSKey key = new MDSKey(next.getRow());
                byte[] columnValue = next.get(COLUMN);
                if (columnValue == null) {
                    continue;
                }
                T value = deserialize(key, columnValue, typeOfT);
                // Key Filter doesn't pass
                if (keyFilter != null && !keyFilter.test(key)) {
                    continue;
                }
                // If Value Filter doesn't pass
                if (valueFilter != null && !valueFilter.test(value)) {
                    continue;
                }
                map.put(key, value);
                limit--;
            }
            return map;
        }
    } 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)

Example 70 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 of type T with the given ids for default COLUMN in a map
 *
 * @param ids set of the mds keys
 * @param typeOfT the type of the result
 * @return a map of the deserialized value of the result
 */
protected <T> Map<MDSKey, T> getKV(Set<MDSKey> ids, Type typeOfT) {
    Map<MDSKey, T> 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());
        T result = deserialize(key, value, typeOfT);
        resultMap.put(key, result);
    }
    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)

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