Search in sources :

Example 96 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class MockRuntimeDatasetSource method writeInput.

/**
 * Used to write the input records for the pipeline run. Should be called after the pipeline has been created.
 *
 * @param tableManager dataset manager used to write to the source dataset
 * @param records records that should be the input for the pipeline
 */
public static void writeInput(DataSetManager<Table> tableManager, Iterable<StructuredRecord> records) throws Exception {
    tableManager.flush();
    Table table = tableManager.get();
    // each rowkey will be a UUID.
    for (StructuredRecord record : records) {
        byte[] row = Bytes.toBytes(UUID.randomUUID());
        table.put(row, SCHEMA_COL, Bytes.toBytes(record.getSchema().toString()));
        table.put(row, RECORD_COL, Bytes.toBytes(StructuredRecordStringConverter.toJsonString(record)));
    }
    tableManager.flush();
}
Also used : KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Table(io.cdap.cdap.api.dataset.table.Table) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 97 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class MockSQLEngineWithCapabilities method writeInput.

/**
 * Used to write the input records for the pipeline run. Should be called after the pipeline has been created.
 *
 * @param fileName file to write the records into
 * @param records records that should be the input for the pipeline
 */
public static void writeInput(String fileName, Iterable<StructuredRecord> records) throws Exception {
    Function<StructuredRecord, String> mapper = input -> {
        try {
            return StructuredRecordStringConverter.toJsonString(input);
        } catch (IOException e) {
            throw new RuntimeException("Unable to set up file for test.", e);
        }
    };
    String output = Joiner.on("\n").join(Iterables.transform(records, mapper));
    Files.write(output, new File(fileName), Charsets.UTF_8);
}
Also used : Iterables(com.google.common.collect.Iterables) BatchSQLEngine(io.cdap.cdap.etl.api.engine.sql.BatchSQLEngine) SQLPullRequest(io.cdap.cdap.etl.api.engine.sql.request.SQLPullRequest) SQLPushRequest(io.cdap.cdap.etl.api.engine.sql.request.SQLPushRequest) SQLDatasetConsumer(io.cdap.cdap.etl.api.engine.sql.dataset.SQLDatasetConsumer) HashMap(java.util.HashMap) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) PluginConfig(io.cdap.cdap.api.plugin.PluginConfig) HashSet(java.util.HashSet) PullCapability(io.cdap.cdap.etl.api.engine.sql.capability.PullCapability) SQLDatasetProducer(io.cdap.cdap.etl.api.engine.sql.dataset.SQLDatasetProducer) Files(com.google.common.io.Files) Name(io.cdap.cdap.api.annotation.Name) Gson(com.google.gson.Gson) Map(java.util.Map) StructuredRecordStringConverter(io.cdap.cdap.format.StructuredRecordStringConverter) ETLPlugin(io.cdap.cdap.etl.proto.v2.ETLPlugin) Nullable(javax.annotation.Nullable) SQLWriteResult(io.cdap.cdap.etl.api.engine.sql.request.SQLWriteResult) SQLPullDataset(io.cdap.cdap.etl.api.engine.sql.dataset.SQLPullDataset) Charsets(com.google.common.base.Charsets) SQLJoinDefinition(io.cdap.cdap.etl.api.engine.sql.request.SQLJoinDefinition) Function(com.google.common.base.Function) SQLEngineException(io.cdap.cdap.etl.api.engine.sql.SQLEngineException) SQLJoinRequest(io.cdap.cdap.etl.api.engine.sql.request.SQLJoinRequest) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Plugin(io.cdap.cdap.api.annotation.Plugin) Schema(io.cdap.cdap.api.data.schema.Schema) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Serializable(java.io.Serializable) SQLPushDataset(io.cdap.cdap.etl.api.engine.sql.dataset.SQLPushDataset) RuntimeContext(io.cdap.cdap.api.RuntimeContext) SQLDataset(io.cdap.cdap.etl.api.engine.sql.dataset.SQLDataset) SQLWriteRequest(io.cdap.cdap.etl.api.engine.sql.request.SQLWriteRequest) SQLEngine(io.cdap.cdap.etl.api.engine.sql.SQLEngine) PushCapability(io.cdap.cdap.etl.api.engine.sql.capability.PushCapability) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) IOException(java.io.IOException) File(java.io.File) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 98 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class MockJoiner method merge.

@Override
public StructuredRecord merge(StructuredRecord joinKey, Iterable<JoinElement<StructuredRecord>> joinRow) {
    StructuredRecord.Builder outRecordBuilder;
    outRecordBuilder = StructuredRecord.builder(outputSchema);
    for (JoinElement<StructuredRecord> joinElement : joinRow) {
        StructuredRecord record = joinElement.getInputRecord();
        for (Schema.Field field : record.getSchema().getFields()) {
            outRecordBuilder.set(field.getName(), record.get(field.getName()));
        }
    }
    return outRecordBuilder.build();
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 99 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class AbstractMockSink method readOutput.

/**
 * Used to read the records written by this sink.
 *
 * @param tableManager dataset manager used to get the sink dataset to read from
 */
public static List<StructuredRecord> readOutput(DataSetManager<Table> tableManager) throws Exception {
    tableManager.flush();
    Table table = tableManager.get();
    try (Scanner scanner = table.scan(null, null)) {
        List<StructuredRecord> records = new ArrayList<>();
        Row row;
        while ((row = scanner.next()) != null) {
            Schema schema = Schema.parseJson(row.getString(SCHEMA_COL));
            String recordStr = row.getString(RECORD_COL);
            records.add(StructuredRecordStringConverter.fromJsonString(recordStr, schema));
        }
        return records;
    }
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) Table(io.cdap.cdap.api.dataset.table.Table) Schema(io.cdap.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) Row(io.cdap.cdap.api.dataset.table.Row) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Example 100 with StructuredRecord

use of io.cdap.cdap.api.data.format.StructuredRecord in project cdap by caskdata.

the class FilterTransform method getOutputBuilder.

private StructuredRecord.Builder getOutputBuilder(StructuredRecord input) {
    List<Schema.Field> outFields = new ArrayList<>();
    for (Schema.Field field : input.getSchema().getFields()) {
        outFields.add(field);
    }
    Schema outSchema = Schema.recordOf(input.getSchema().getRecordName(), outFields);
    // copy all the values
    StructuredRecord.Builder outputBuilder = StructuredRecord.builder(outSchema);
    for (Schema.Field inField : input.getSchema().getFields()) {
        outFields.add(inField);
        outputBuilder.set(inField.getName(), input.get(inField.getName()));
    }
    return outputBuilder;
}
Also used : PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Schema(io.cdap.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord)

Aggregations

StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)210 Schema (io.cdap.cdap.api.data.schema.Schema)169 Test (org.junit.Test)119 Table (io.cdap.cdap.api.dataset.table.Table)76 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)73 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)73 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)68 ApplicationManager (io.cdap.cdap.test.ApplicationManager)68 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)59 WorkflowManager (io.cdap.cdap.test.WorkflowManager)54 HashSet (java.util.HashSet)50 ArrayList (java.util.ArrayList)44 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)40 HashMap (java.util.HashMap)25 File (java.io.File)17 ETLPlugin (io.cdap.cdap.etl.proto.v2.ETLPlugin)16 FormatSpecification (io.cdap.cdap.api.data.format.FormatSpecification)15 DataStreamsConfig (io.cdap.cdap.etl.proto.v2.DataStreamsConfig)14 SparkManager (io.cdap.cdap.test.SparkManager)12 Map (java.util.Map)12