Search in sources :

Example 16 with Row

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

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 17 with Row

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

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 18 with Row

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

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 19 with Row

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

the class DefaultPreviewStore method deleteExpiredData.

@Override
public void deleteExpiredData(long ttlInSeconds) {
    Gson gson = new GsonBuilder().registerTypeAdapter(EntityId.class, new EntityIdTypeAdapter()).create();
    byte[] startRowKey = new MDSKey.Builder().add(META_ROW_KEY_PREFIX).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    long currentTimeInSeconds = System.currentTimeMillis() / 1000;
    try (Scanner scanner = previewTable.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            String applicationIdGson = Bytes.toString(columns.get(APPID));
            if (applicationIdGson == null) {
                continue;
            }
            ApplicationId applicationId = gson.fromJson(applicationIdGson, ApplicationId.class);
            long applicationSubmitTime = RunIds.getTime(applicationId.getApplication(), TimeUnit.SECONDS);
            if ((currentTimeInSeconds - applicationSubmitTime) > ttlInSeconds) {
                remove(applicationId);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Error while scanning the preview requests for deletion.", e);
    }
}
Also used : EntityIdTypeAdapter(io.cdap.cdap.proto.codec.EntityIdTypeAdapter) Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) EntityId(io.cdap.cdap.proto.id.EntityId) Row(io.cdap.cdap.api.dataset.table.Row) ApplicationId(io.cdap.cdap.proto.id.ApplicationId)

Example 20 with Row

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

the class DefaultPreviewStore method get.

@Override
public Map<String, List<JsonElement>> get(ApplicationId applicationId, String tracerName) {
    // PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    byte[] startRowKey = getPreviewRowKeyBuilder(DATA_ROW_KEY_PREFIX, applicationId).add(tracerName).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    Map<String, List<JsonElement>> result = new HashMap<>();
    try (Scanner scanner = previewTable.scan(startRowKey, stopRowKey, null, null, null)) {
        Row indexRow;
        while ((indexRow = scanner.next()) != null) {
            Map<byte[], byte[]> columns = indexRow.getColumns();
            String propertyName = Bytes.toString(columns.get(PROPERTY));
            JsonElement value = gson.fromJson(Bytes.toString(columns.get(VALUE)), JsonElement.class);
            List<JsonElement> values = result.computeIfAbsent(propertyName, k -> new ArrayList<>());
            values.add(value);
        }
    } catch (IOException e) {
        String message = String.format("Error while reading preview data for application '%s' and tracer '%s'.", applicationId, tracerName);
        throw new RuntimeException(message, e);
    }
    return result;
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Schema(io.cdap.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) 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