Search in sources :

Example 26 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class DefaultSecretStore method store.

@Override
public <T> void store(String namespace, String name, Encoder<T> encoder, T data) throws IOException {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(StoreDefinition.SecretStore.SECRET_STORE_TABLE);
        List<Field<?>> fields = ImmutableList.<Field<?>>builder().addAll(getKeyFields(namespace, name)).add(Fields.bytesField(StoreDefinition.SecretStore.SECRET_DATA_FIELD, encoder.encode(data))).build();
        table.upsert(fields);
    }, IOException.class);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable)

Example 27 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class ConnectionStore method saveConnection.

/**
 * Save the connection in the store.
 *
 * @param connectionId the connection id
 * @param connection the connection information
 * @param overWrite flag indicating whether the store should overwrite an existing connection with same connection id
 *                  but different connection name, i.e, a b and a.b both convert to id a_b
 */
public void saveConnection(ConnectionId connectionId, Connection connection, boolean overWrite) {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = context.getTable(TABLE_ID);
        Connection oldConnection = getConnectionInternal(table, connectionId, false);
        Connection newConnection = connection;
        if (oldConnection != null) {
            if (oldConnection.isPreConfigured()) {
                throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s and is pre-configured. " + "Preconfigured connections cannot be updated or overwritten.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
            }
            if (!oldConnection.getName().equals(newConnection.getName()) && !overWrite) {
                throw new ConnectionConflictException(String.format("Connection %s in namespace %s has same id %s. Please choose a different connection name.", oldConnection.getName(), connectionId.getNamespace(), connectionId.getConnectionId()));
            }
            newConnection = new Connection(connection.getName(), oldConnection.getConnectionId(), connection.getConnectionType(), connection.getDescription(), connection.isPreConfigured(), connection.isDefault(), oldConnection.getCreatedTimeMillis(), connection.getUpdatedTimeMillis(), connection.getPlugin());
        }
        Collection<Field<?>> fields = getConnectionKeys(connectionId);
        fields.add(Fields.longField(CREATED_COL, newConnection.getCreatedTimeMillis()));
        fields.add(Fields.longField(UPDATED_COL, newConnection.getUpdatedTimeMillis()));
        fields.add(Fields.stringField(CONNECTION_DATA_FIELD, GSON.toJson(newConnection)));
        table.upsert(fields);
    });
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) Connection(io.cdap.cdap.etl.proto.connection.Connection)

Example 28 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class DatasetTypeTable method writeModule.

public void writeModule(NamespaceId namespaceId, DatasetModuleMeta moduleMeta) throws IOException {
    DatasetModuleId datasetModuleId = namespaceId.datasetModule(moduleMeta.getName());
    DatasetModuleMeta existing = getModule(datasetModuleId);
    List<Field<?>> fields = getModuleKey(namespaceId.getEntityName(), moduleMeta.getName());
    fields.add(Fields.stringField(StoreDefinition.DatasetTypeStore.DATASET_METADATA_FIELD, GSON.toJson(moduleMeta)));
    getModuleTable().upsert(fields);
    for (String type : moduleMeta.getTypes()) {
        writeTypeToModuleMapping(namespaceId.datasetType(type), datasetModuleId);
    }
    if (existing != null) {
        Set<String> removed = new HashSet<>(existing.getTypes());
        removed.removeAll(moduleMeta.getTypes());
        for (String type : removed) {
            getTypeTable().deleteAll(Range.singleton(getTypeKey(datasetModuleId.getNamespace(), type)));
        }
    }
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) Field(io.cdap.cdap.spi.data.table.field.Field) DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta) HashSet(java.util.HashSet)

Example 29 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class FieldLineageTable method getChecksumsWithProgramRunsInRange.

private Map<Long, Set<ProgramRunId>> getChecksumsWithProgramRunsInRange(String direction, EndPoint endPoint, long start, long end) throws IOException {
    // time is inverted, hence we need to pass end-time for getting start key
    List<Field<?>> scanStartKey = getScanKey(direction, endPoint, end);
    // time is inverted, hence we need to pass start-time for getting end key
    List<Field<?>> scanEndKey = getScanKey(direction, endPoint, start);
    Map<Long, Set<ProgramRunId>> result = new LinkedHashMap<>();
    try (CloseableIterator<StructuredRow> iterator = getEndpointChecksumTable().scan(Range.create(scanStartKey, Range.Bound.INCLUSIVE, scanEndKey, Range.Bound.INCLUSIVE), Integer.MAX_VALUE)) {
        while (iterator.hasNext()) {
            StructuredRow row = iterator.next();
            long checksum = row.getLong(StoreDefinition.FieldLineageStore.CHECKSUM_FIELD);
            ProgramRunId programRunId = GSON.fromJson(row.getString(StoreDefinition.FieldLineageStore.PROGRAM_RUN_FIELD), ProgramRunId.class);
            Set<ProgramRunId> programRuns = result.computeIfAbsent(checksum, k -> new HashSet<>());
            programRuns.add(programRunId);
        }
    }
    return result;
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with Field

use of io.cdap.cdap.spi.data.table.field.Field in project cdap by caskdata.

the class FieldLineageTable method getOperations.

private Set<ProgramRunOperations> getOperations(String direction, EndPoint endPoint, long start, long end) throws IOException {
    Map<Long, Set<ProgramRunId>> checksumsWithProgramRunsInRange = getChecksumsWithProgramRunsInRange(direction, endPoint, start, end);
    Set<ProgramRunOperations> result = new LinkedHashSet<>();
    for (Map.Entry<Long, Set<ProgramRunId>> entry : checksumsWithProgramRunsInRange.entrySet()) {
        long checksum = entry.getKey();
        List<Field<?>> keys = getOperationsKey(checksum);
        Optional<StructuredRow> row = getOperationsTable().read(keys);
        if (!row.isPresent()) {
            continue;
        }
        String value = row.get().getString(StoreDefinition.FieldLineageStore.OPERATIONS_FIELD);
        Set<Operation> operations;
        try {
            operations = GSON.fromJson(value, SET_OPERATION_TYPE);
        } catch (JsonSyntaxException e) {
            LOG.warn(String.format("Failed to parse json from checksum %d'. Ignoring operations.", checksum));
            continue;
        }
        if (operations != null) {
            result.add(new ProgramRunOperations(entry.getValue(), operations));
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgramRunOperations(io.cdap.cdap.proto.metadata.lineage.ProgramRunOperations) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ReadOperation(io.cdap.cdap.api.lineage.field.ReadOperation) Operation(io.cdap.cdap.api.lineage.field.Operation) WriteOperation(io.cdap.cdap.api.lineage.field.WriteOperation) Field(io.cdap.cdap.spi.data.table.field.Field) JsonSyntaxException(com.google.gson.JsonSyntaxException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Field (io.cdap.cdap.spi.data.table.field.Field)72 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)40 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)33 ArrayList (java.util.ArrayList)33 Range (io.cdap.cdap.spi.data.table.field.Range)24 HashSet (java.util.HashSet)18 Map (java.util.Map)17 List (java.util.List)16 Nullable (javax.annotation.Nullable)16 CloseableIterator (io.cdap.cdap.api.dataset.lib.CloseableIterator)15 Collections (java.util.Collections)15 HashMap (java.util.HashMap)15 LinkedHashSet (java.util.LinkedHashSet)15 Set (java.util.Set)15 Fields (io.cdap.cdap.spi.data.table.field.Fields)14 Collection (java.util.Collection)14 LinkedHashMap (java.util.LinkedHashMap)14 Collectors (java.util.stream.Collectors)14 Optional (java.util.Optional)13 IOException (java.io.IOException)12