Search in sources :

Example 16 with TalendRuntimeException

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

the class PreparationUtils method prettyPrint.

private static void prettyPrint(PreparationActions blob, OutputStream out) {
    if (blob == null) {
        return;
    }
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
        writer.append("\t\t\tContent: ").append("\n");
        writer.append("======").append("\n");
        writer.append(blob.toString()).append("\n");
        writer.append("======").append("\n");
        writer.flush();
    } 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 17 with TalendRuntimeException

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

the class ActionParser method parse.

/**
 * Return the parsed actions ready to be run.
 *
 * @param actions the actions to be parsed as string.
 * @return the parsed actions.
 * @throws IllegalArgumentException if <code>actions</code> is null.
 */
public List<RunnableAction> parse(String actions) {
    if (actions == null) {
        // Actions cannot be null (but can be empty string for no op actions).
        throw new IllegalArgumentException("Actions parameter can not be null.");
    }
    if (StringUtils.isEmpty(actions)) {
        return Collections.emptyList();
    }
    try {
        // Parse action JSON
        final Actions parsedActions = mapper.reader(Actions.class).readValue(actions);
        final List<Action> allActions = parsedActions.getActions();
        // Create closures from parsed actions
        final List<RunnableAction> builtActions = new ArrayList<>(allActions.size() + 1);
        // 
        allActions.stream().filter(// 
        parsedAction -> parsedAction != null && parsedAction.getName() != null).forEach(parsedAction -> {
            String actionNameLowerCase = parsedAction.getName().toLowerCase();
            final ActionDefinition metadata = actionRegistry.get(actionNameLowerCase);
            builtActions.add(factory.create(metadata, parsedAction.getParameters()));
        });
        // all set: wraps everything and return to caller
        return builtActions;
    } catch (TalendRuntimeException tpe) {
        // leave TDPException as is
        throw tpe;
    } catch (Exception e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNABLE_TO_PARSE_JSON, e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ActionRegistry(org.talend.dataprep.transformation.pipeline.ActionRegistry) ActionFactory(org.talend.dataprep.transformation.actions.common.ActionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Actions(org.talend.dataprep.api.preparation.Actions) ArrayList(java.util.ArrayList) List(java.util.List) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) BaseErrorCodes(org.talend.dataprep.BaseErrorCodes) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Action(org.talend.dataprep.api.preparation.Action) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) Actions(org.talend.dataprep.api.preparation.Actions) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) ArrayList(java.util.ArrayList) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException)

Example 18 with TalendRuntimeException

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

the class TypeDetectionNode method store.

// Store row in temporary file
private void store(DataSetRow row, List<ColumnMetadata> columns) {
    try {
        generator.writeStartObject();
        columns.forEach(column -> {
            try {
                generator.writeStringField(column.getId(), row.get(column.getId()));
            } catch (IOException e) {
                throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
            }
        });
        if (row.isDeleted()) {
            generator.writeBooleanField("_deleted", true);
        }
        final Optional<Long> tdpId = Optional.ofNullable(row.getTdpId());
        if (tdpId.isPresent()) {
            generator.writeNumberField(FlagNames.TDP_ID, tdpId.get());
        }
        for (Map.Entry<String, String> entry : row.getInternalValues().entrySet()) {
            generator.writeStringField(entry.getKey(), entry.getValue());
        }
        generator.writeEndObject();
    } catch (IOException e) {
        throw new TalendRuntimeException(BaseErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) IOException(java.io.IOException) Map(java.util.Map)

Example 19 with TalendRuntimeException

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

the class ActionMetadataValidationTest method checkScopeConsistency_should_throw_exception_on_missing_scope.

@Test
public void checkScopeConsistency_should_throw_exception_on_missing_scope() throws Exception {
    // given
    final Map<String, String> parameters = new HashMap<>();
    parameters.put("column_id", "0001");
    ActionDefinition actionMock = new ActionMetadataExtendingColumn();
    // when
    try {
        validator.checkScopeConsistency(actionMock, parameters);
        fail("should have thrown TDP exception because param scope is missing");
    }// then
     catch (final TalendRuntimeException e) {
        assertThat(e.getCode(), is(MISSING_ACTION_SCOPE));
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) HashMap(java.util.HashMap) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) Test(org.junit.Test) ServiceBaseTest(org.talend.ServiceBaseTest)

Example 20 with TalendRuntimeException

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

the class ReorderColumn method compile.

@Override
public void compile(ActionContext actionContext) {
    super.compile(actionContext);
    if (ActionsUtils.doesCreateNewColumn(actionContext.getParameters(), CREATE_NEW_COLUMN_DEFAULT)) {
        ActionsUtils.createNewColumn(actionContext, singletonList(ActionsUtils.additionalColumn()));
    }
    Map<String, String> parameters = actionContext.getParameters();
    RowMetadata rowMetadata = actionContext.getRowMetadata();
    String targetColumnId = parameters.get(SELECTED_COLUMN_PARAMETER);
    ColumnMetadata targetColumn = rowMetadata.getById(targetColumnId);
    if (targetColumn == null) {
        return;
    }
    String originColumnId = parameters.get(COLUMN_ID.getKey());
    // column id may be different from index in the list
    // we cannot rely on id as index
    // so we have to find first the origin and target index
    int index = 0, originIndex = 0, targetIndex = 0;
    for (ColumnMetadata columnMetadata : rowMetadata.getColumns()) {
        if (StringUtils.equals(columnMetadata.getId(), originColumnId)) {
            originIndex = index;
        }
        if (StringUtils.equals(columnMetadata.getId(), targetColumnId)) {
            targetIndex = index;
        }
        index++;
    }
    // now we have both index so we can iterate again and swap few columns
    // we have different case as target can he lower than origin or the opposite
    boolean forwardMove = targetIndex > originIndex;
    try {
        if (forwardMove) {
            for (index = originIndex; index < targetIndex; index++) {
                swapColumnMetadata(rowMetadata.getColumns().get(index), rowMetadata.getColumns().get(index + 1));
            }
        } else {
            for (index = originIndex; index > targetIndex; index--) {
                swapColumnMetadata(rowMetadata.getColumns().get(index), rowMetadata.getColumns().get(index - 1));
            }
        }
    } catch (Exception e) {
        LOGGER.debug("cannot swap columns: {}", e.getMessage());
        throw new TalendRuntimeException(UNEXPECTED_EXCEPTION, build().put("message", e.getMessage()));
    }
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException)

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