Search in sources :

Example 1 with TalendRuntimeException

use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.

the class ModifyDate method applyOnColumn.

/**
 * @see ColumnAction#applyOnColumn(DataSetRow, ActionContext)
 */
@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
    final String columnId = context.getColumnId();
    final String originalValue = row.get(columnId);
    if (StringUtils.isBlank(originalValue)) {
        row.set(ActionsUtils.getTargetColumnId(context), originalValue);
        return;
    }
    Map<String, String> parameters = context.getParameters();
    String mode = parameters.get(MODE_PARAMETER);
    long amount;
    switch(mode) {
        case CONSTANT_MODE:
            amount = context.get(AMOUNT_CONTEXT_KEY);
            break;
        case OTHER_COLUMN_MODE:
            String otherColId = parameters.get(SELECTED_COLUMN_PARAMETER);
            if (!NumericHelper.isBigDecimal(row.get(otherColId))) {
                // In this case, do not change the original value
                return;
            }
            amount = computeAmount(row.get(otherColId));
            break;
        default:
            throw new // 
            TalendRuntimeException(// 
            ActionErrorCodes.BAD_ACTION_PARAMETER, ExceptionContext.build().put("paramName", OtherColumnParameters.CONSTANT_MODE));
    }
    try {
        final DatePattern outputPattern = new DatePattern(context.get(PATTERN_CONTEXT_KEY));
        LocalDateTime date = Providers.get().parse(originalValue, context.getRowMetadata().getById(columnId));
        date = date.plus(amount, context.get(UNIT_CONTEXT_KEY));
        row.set(ActionsUtils.getTargetColumnId(context), outputPattern.getFormatter().format(date));
    } catch (DateTimeException e) {
        row.set(ActionsUtils.getTargetColumnId(context), originalValue);
        // cannot parse the date, let's leave it as is
        LOGGER.debug("Unable to parse date {}.", originalValue, e);
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) LocalDateTime(java.time.LocalDateTime) DateTimeException(java.time.DateTimeException) RowMetadataUtils.getMostUsedDatePattern(org.talend.dataprep.api.dataset.row.RowMetadataUtils.getMostUsedDatePattern)

Example 2 with TalendRuntimeException

use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.

the class PreparationUtils method prettyPrint.

private static void prettyPrint(PreparationRepository repository, String stepId, OutputStream out) {
    if (stepId == null) {
        return;
    }
    try {
        Step step = repository.get(stepId, Step.class);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
        writer.append("\t\tStep (").append(step.id()).append(")").append("\n");
        writer.flush();
        PreparationActions blob = repository.get(step.getContent(), PreparationActions.class);
        prettyPrint(blob, out);
        if (step.getParent() != null) {
            prettyPrint(repository, step.getParent(), out);
        }
    } catch (IOException e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 3 with TalendRuntimeException

use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.

the class PreparationUtils method prettyPrint.

public static void prettyPrint(PreparationRepository repository, Preparation preparation, OutputStream out) {
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
        writer.append("Preparation (").append(preparation.id()).append(")\n");
        writer.append("\tData set: ").append(preparation.getDataSetId()).append("\n");
        writer.append("\tAuthor: ").append(preparation.getAuthor()).append("\n");
        writer.append("\tCreation date: ").append(String.valueOf(preparation.getCreationDate())).append("\n");
        writer.append("\tSteps:").append("\n");
        writer.flush();
        prettyPrint(repository, preparation.getHeadId(), out);
    } catch (IOException e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 4 with TalendRuntimeException

use of org.talend.daikon.exception.TalendRuntimeException in project data-prep by Talend.

the class NodeBuilder method to.

/**
 * Append a new node at the end, with a custom link
 *
 * @param linkFunction A function that create the link from the previous nodes
 * @param node The new node to append
 */
public NodeBuilder to(final Function<Node[], Link> linkFunction, final Node node) {
    try {
        state = state.next(linkFunction);
        state = state.next(node);
    } catch (IllegalStateException e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, new Exception("Each to() must be followed by node().", e));
    }
    return this;
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException)

Example 5 with TalendRuntimeException

use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.

the class SimpleFileIOOutputErrorTest method testTryToOverwrite.

/**
 * Basic unit test using all default values (except for the path) on an in-memory DFS cluster.
 */
@Test
public void testTryToOverwrite() throws IOException, URISyntaxException {
    Path parent = new Path(mini.newFolder().toString());
    Path dst = new Path(parent, "output");
    String fileSpec = mini.getLocalFs().getUri().resolve(dst.toUri()).toString();
    // Write something to the file before trying to run.
    try (OutputStream out = mini.getLocalFs().create(new Path(dst, "part-00000"))) {
        out.write(0);
    }
    // Trying to write to an existing destination throws an exception.
    thrown.expect(TalendRuntimeException.class);
    thrown.expect(hasProperty("code", is(SimpleFileIOErrorCode.OUTPUT_ALREADY_EXISTS)));
    thrown.expectMessage("The path " + fileSpec + " already exists. Please remove it manually.");
    // Now try using the component.
    try {
        // Configure the component.
        SimpleFileIOOutputProperties props = SimpleFileIOOutputRuntimeTest.createOutputComponentProperties();
        props.getDatasetProperties().path.setValue(fileSpec);
        // Create the runtime.
        SimpleFileIOOutputRuntime runtime = new SimpleFileIOOutputRuntime();
        runtime.initialize(null, props);
        // Use the runtime in a direct pipeline to test.
        final Pipeline p = beam.createPipeline();
        PCollection<IndexedRecord> input = // 
        p.apply(// 
        Create.of(// 
        ConvertToIndexedRecord.convertToAvro(new String[] { "1", "one" }), // 
        ConvertToIndexedRecord.convertToAvro(new String[] { "2", "two" })));
        input.apply(runtime);
        // And run the test.
        p.run().waitUntilFinish();
    } catch (Pipeline.PipelineExecutionException e) {
        if (e.getCause() instanceof TalendRuntimeException)
            throw (TalendRuntimeException) e.getCause();
        throw e;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) SimpleFileIOOutputProperties(org.talend.components.simplefileio.output.SimpleFileIOOutputProperties) ConvertToIndexedRecord(org.talend.components.adapter.beam.transform.ConvertToIndexedRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) OutputStream(java.io.OutputStream) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Aggregations

TalendRuntimeException (org.talend.daikon.exception.TalendRuntimeException)28 IndexedRecord (org.apache.avro.generic.IndexedRecord)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Pipeline (org.apache.beam.sdk.Pipeline)6 Schema (org.apache.avro.Schema)5 Path (org.apache.hadoop.fs.Path)5 ConvertToIndexedRecord (org.talend.components.adapter.beam.transform.ConvertToIndexedRecord)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SimpleFileIOOutputProperties (org.talend.components.simplefileio.output.SimpleFileIOOutputProperties)4 BufferedWriter (java.io.BufferedWriter)3 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DateTimeException (java.time.DateTimeException)2 LocalDateTime (java.time.LocalDateTime)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 StringUtils (org.apache.commons.lang.StringUtils)2 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)2