Search in sources :

Example 16 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey 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 = new MDSKey.Builder().add(applicationId.getNamespace()).add(applicationId.getApplication()).add(tracerName).build().getKey();
    byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
    Map<String, List<JsonElement>> result = new HashMap<>();
    try (Scanner scanner = table.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.get(propertyName);
            if (values == null) {
                values = new ArrayList<>();
                result.put(propertyName, values);
            }
            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(co.cask.cdap.api.dataset.table.Scanner) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Schema(co.cask.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) IOException(java.io.IOException) SchemaTypeAdapter(co.cask.cdap.internal.io.SchemaTypeAdapter) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) Row(co.cask.cdap.api.dataset.table.Row)

Example 17 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class ProfileStore method add.

/**
 * Add the profile to the profile store.
 *
 * @param profileId the id of the profile to add
 * @param profile the information of the profile
 * @throws IOException if there was an IO error adding the profile
 * @throws AlreadyExistsException if the profile already exists
 */
public void add(ProfileId profileId, Profile profile) throws IOException, AlreadyExistsException {
    Transactionals.execute(transactional, context -> {
        MetadataStoreDataset mds = getMDS(context);
        // make sure that a profile doesn't exist
        MDSKey rowKey = getRowKey(profileId);
        Profile value = mds.get(rowKey, Profile.class);
        if (value != null) {
            throw new AlreadyExistsException(profileId, String.format("Profile '%s' already exists.", profile.getName()));
        }
        mds.write(rowKey, profile);
    }, IOException.class, AlreadyExistsException.class);
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) MetadataStoreDataset(co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Profile(co.cask.cdap.proto.profile.Profile)

Example 18 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class ProfileStore method delete.

/**
 * Deletes the profile from the profile store
 *
 * @param profileId the id of the profile to delete
 * @throws IOException if there was an IO error deleting the profile
 * @throws NotFoundException if the profile is not found
 */
public void delete(ProfileId profileId) throws IOException, NotFoundException {
    Transactionals.execute(transactional, context -> {
        MetadataStoreDataset mds = getMDS(context);
        MDSKey rowKey = getRowKey(profileId);
        Profile value = getMDS(context).get(rowKey, Profile.class);
        if (value == null) {
            throw new NotFoundException(profileId);
        }
        mds.delete(getRowKey(profileId));
    }, IOException.class, NotFoundException.class);
}
Also used : MetadataStoreDataset(co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) NotFoundException(co.cask.cdap.common.NotFoundException) Profile(co.cask.cdap.proto.profile.Profile)

Example 19 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class MdsKey method getNamespacedIdFromKey.

static NamespacedEntityId getNamespacedIdFromKey(String type, byte[] rowKey) {
    MDSKey.Splitter keySplitter = new MDSKey(rowKey).split();
    // The rowkey is
    // [rowPrefix][targetType][targetId][key] for value rows and
    // [rowPrefix][targetType][targetId][key][index] for value index rows
    // so skip the first two.
    keySplitter.skipBytes();
    keySplitter.skipString();
    return EntityIdKeyHelper.getTargetIdIdFromKey(keySplitter, type);
}
Also used : MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey)

Example 20 with MDSKey

use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.

the class MetadataDataset method deleteIndexes.

/**
 * Deletes all indexes associated with a metadata key
 *
 * @param targetId the {@link NamespacedEntityId} for which keys are to be removed
 * @param metadataKey the key to remove from the metadata of the specified {@link NamespacedEntityId}
 */
private void deleteIndexes(NamespacedEntityId targetId, String metadataKey) {
    MDSKey mdsKey = MdsKey.getMDSIndexKey(targetId, metadataKey, null);
    byte[] startKey = mdsKey.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
    try (Scanner scan = indexedTable.scan(startKey, stopKey)) {
        Row next;
        while ((next = scan.next()) != null) {
            deleteIndexRow(next);
        }
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Row(co.cask.cdap.api.dataset.table.Row)

Aggregations

MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)66 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)31 GsonBuilder (com.google.gson.GsonBuilder)22 IOException (java.io.IOException)16 Gson (com.google.gson.Gson)14 HashMap (java.util.HashMap)12 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)11 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)9 Row (co.cask.cdap.api.dataset.table.Row)8 ProgramId (co.cask.cdap.proto.id.ProgramId)8 Nullable (javax.annotation.Nullable)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Test (org.junit.Test)7 Scanner (co.cask.cdap.api.dataset.table.Scanner)6 MetadataStoreDataset (co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset)6 ArrayList (java.util.ArrayList)6 WorkflowNodeStateDetail (co.cask.cdap.proto.WorkflowNodeStateDetail)5 ApplicationId (co.cask.cdap.proto.id.ApplicationId)5 Row (io.cdap.cdap.api.dataset.table.Row)5 Scanner (io.cdap.cdap.api.dataset.table.Scanner)5