Search in sources :

Example 91 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class DraftStore method doSort.

/**
 * Helper method to apply the sorting onto a list of rows. Sorting needs to take place at the store-level so it can
 * leverage the StructuredRow to enable sorting on any field.
 *
 * @param rows list of {@link StructuredRow} to be sorted
 * @param sortRequest {@link SortRequest} describing the sort to be performed
 * @return a sorted list of {@link StructuredRow}
 */
private List<StructuredRow> doSort(List<StructuredRow> rows, @Nullable SortRequest sortRequest) {
    if (sortRequest == null) {
        return rows;
    }
    String sortField = sortRequest.getFieldName();
    FieldType field = TABLE_SPEC.getFieldTypes().stream().filter(f -> f.getName().equals(sortField)).findFirst().orElse(null);
    if (field == null) {
        throw new IllegalArgumentException(String.format("Invalid value '%s' for sortBy. This field does not exist in the Drafts table.", sortField));
    }
    FieldType.Type fieldType = field.getType();
    Comparator<StructuredRow> comparator;
    switch(fieldType) {
        case STRING:
            comparator = Comparator.<StructuredRow, String>comparing(o -> o.getString(sortField));
            break;
        case INTEGER:
            comparator = Comparator.<StructuredRow, Integer>comparing(o -> o.getInteger(sortField));
            break;
        case LONG:
            comparator = Comparator.<StructuredRow, Long>comparing(o -> o.getLong(sortField));
            break;
        case FLOAT:
            comparator = Comparator.<StructuredRow, Float>comparing(o -> o.getFloat(sortField));
            break;
        case DOUBLE:
            comparator = Comparator.<StructuredRow, Double>comparing(o -> o.getDouble(sortField));
            break;
        case BYTES:
            comparator = Comparator.comparing(o -> o.getBytes(sortField), Bytes.BYTES_COMPARATOR);
            break;
        default:
            throw new UnsupportedOperationException(String.format("Cannot sort field '%s' because type '%s' is not supported.", sortField, fieldType.toString()));
    }
    if (sortRequest.getOrder() != SortRequest.SortOrder.ASC) {
        comparator = comparator.reversed();
    }
    rows.sort(comparator);
    return rows;
}
Also used : TransactionRunners(io.cdap.cdap.spi.data.transaction.TransactionRunners) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) Bytes(io.cdap.cdap.api.common.Bytes) Fields(io.cdap.cdap.spi.data.table.field.Fields) ArrayList(java.util.ArrayList) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Gson(com.google.gson.Gson) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ETLConfig(io.cdap.cdap.etl.proto.v2.ETLConfig) Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTableSpecification(io.cdap.cdap.spi.data.table.StructuredTableSpecification) Nullable(javax.annotation.Nullable) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) StudioUtil(io.cdap.cdap.datapipeline.service.StudioUtil) ETLBatchConfig(io.cdap.cdap.etl.proto.v2.ETLBatchConfig) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) Collectors(java.util.stream.Collectors) DataStreamsConfig(io.cdap.cdap.etl.proto.v2.DataStreamsConfig) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) List(java.util.List) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Range(io.cdap.cdap.spi.data.table.field.Range) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) FieldType(io.cdap.cdap.spi.data.table.field.FieldType)

Example 92 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class DraftStore method getDraft.

/**
 * Fetch a given draft if it exists
 *
 * @param id {@link DraftId} that is used to uniquely identify a draft
 * @return an {@link Optional<Draft>} representing the requested draft
 * @throws TableNotFoundException if the draft store table is not found
 * @throws InvalidFieldException if the fields in the {@link DraftId} object do not match the fields in the
 *   StructuredTable
 */
public Optional<Draft> getDraft(DraftId id) throws TableNotFoundException, InvalidFieldException {
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        Optional<StructuredRow> row = table.read(getKey(id));
        return row.map(this::fromRow);
    }, TableNotFoundException.class, InvalidFieldException.class);
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 93 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class OAuthStore method getProvider.

/**
 * Get an OAuth provider.
 *
 * @param name name of {@link OAuthProvider} to read
 * @throws OAuthStoreException if the read fails
 */
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
    OAuthClientCredentials clientCreds;
    try {
        String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
        clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
    } catch (Exception e) {
        return Optional.empty();
    }
    try {
        return TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            Optional<StructuredRow> row = table.read(getKey(name));
            return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Example 94 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class UsageTable method getPrograms.

/**
 * Returns programs using dataset.
 * @param datasetInstanceId dataset
 * @return programs using datasetInstanceId
 */
public Set<ProgramId> getPrograms(DatasetId datasetInstanceId) throws IOException {
    Set<ProgramId> programs = new HashSet<>();
    Field<?> index = Fields.stringField(StoreDefinition.UsageStore.INDEX_FIELD, GSON.toJson(new DatasetKey(datasetInstanceId)));
    try (CloseableIterator<StructuredRow> iterator = table.scan(index)) {
        while (iterator.hasNext()) {
            StructuredRow row = iterator.next();
            programs.add(new ProgramId(row.getString(StoreDefinition.UsageStore.NAMESPACE_FIELD), row.getString(StoreDefinition.UsageStore.APPLICATION_FIELD), row.getString(StoreDefinition.UsageStore.PROGRAM_TYPE_FIELD), row.getString(StoreDefinition.UsageStore.PROGRAM_FIELD)));
        }
    }
    return programs;
}
Also used : StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ProgramId(io.cdap.cdap.proto.id.ProgramId) HashSet(java.util.HashSet)

Example 95 with StructuredRow

use of io.cdap.cdap.spi.data.StructuredRow in project cdap by caskdata.

the class FieldLineageTable method getSourceFields.

private Set<String> getSourceFields(EndPoint endPoint, long start, long end) throws IOException {
    Set<Long> checksums = getChecksumsWithProgramRunsInRange(OUTGOING_DIRECTION_MARKER, endPoint, start, end).keySet();
    Set<String> fields = new HashSet<>();
    for (long checksum : checksums) {
        List<Field<?>> prefix = getSummaryPrefix(checksum, OUTGOING_DIRECTION_MARKER, endPoint);
        try (CloseableIterator<StructuredRow> iterator = getSummaryFieldsTable().scan(Range.singleton(prefix), Integer.MAX_VALUE)) {
            while (iterator.hasNext()) {
                StructuredRow row = iterator.next();
                fields.add(row.getString(StoreDefinition.FieldLineageStore.ENDPOINT_FIELD));
            }
        }
    }
    return fields;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

StructuredRow (io.cdap.cdap.spi.data.StructuredRow)142 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)68 Field (io.cdap.cdap.spi.data.table.field.Field)66 ArrayList (java.util.ArrayList)54 Range (io.cdap.cdap.spi.data.table.field.Range)36 HashSet (java.util.HashSet)28 IOException (java.io.IOException)22 HashMap (java.util.HashMap)22 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)20 LinkedHashMap (java.util.LinkedHashMap)18 Map (java.util.Map)18 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)16 Collection (java.util.Collection)16 Set (java.util.Set)16 Nullable (javax.annotation.Nullable)16 ImmutableList (com.google.common.collect.ImmutableList)14 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)14 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)14 Fields (io.cdap.cdap.spi.data.table.field.Fields)14