Search in sources :

Example 1 with Producer

use of org.talend.sdk.component.api.input.Producer in project component-runtime by Talend.

the class ModelVisitor method validateProcessor.

private void validateProcessor(final Class<?> input) {
    final List<Method> producers = Stream.of(input.getMethods()).filter(m -> m.isAnnotationPresent(ElementListener.class)).collect(toList());
    if (producers.size() != 1) {
        throw new IllegalArgumentException(input + " must have a single @ElementListener method");
    }
    if (Stream.of(producers.get(0).getParameters()).filter(p -> {
        if (p.isAnnotationPresent(Output.class)) {
            if (!ParameterizedType.class.isInstance(p.getParameterizedType())) {
                throw new IllegalArgumentException("@Output parameter must be of type OutputEmitter");
            }
            final ParameterizedType pt = ParameterizedType.class.cast(p.getParameterizedType());
            if (OutputEmitter.class != pt.getRawType()) {
                throw new IllegalArgumentException("@Output parameter must be of type OutputEmitter");
            }
            return false;
        }
        return true;
    }).count() < 1) {
        throw new IllegalArgumentException(input + " doesn't have the input parameter on its producer method");
    }
    Stream.of(input.getMethods()).filter(m -> m.isAnnotationPresent(BeforeGroup.class) || m.isAnnotationPresent(AfterGroup.class)).forEach(m -> {
        if (m.getParameterCount() > 0) {
            throw new IllegalArgumentException(m + " must not have any parameter");
        }
    });
}
Also used : Emitter(org.talend.sdk.component.api.input.Emitter) PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) Producer(org.talend.sdk.component.api.input.Producer) AfterGroup(org.talend.sdk.component.api.processor.AfterGroup) BeforeGroup(org.talend.sdk.component.api.processor.BeforeGroup) Collection(java.util.Collection) Assessor(org.talend.sdk.component.api.input.Assessor) Split(org.talend.sdk.component.api.input.Split) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Processor(org.talend.sdk.component.api.processor.Processor) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ElementListener(org.talend.sdk.component.api.processor.ElementListener) Annotation(java.lang.annotation.Annotation) PartitionSize(org.talend.sdk.component.api.input.PartitionSize) Method(java.lang.reflect.Method) Output(org.talend.sdk.component.api.processor.Output) ParameterizedType(java.lang.reflect.ParameterizedType) BeforeGroup(org.talend.sdk.component.api.processor.BeforeGroup) Method(java.lang.reflect.Method) AfterGroup(org.talend.sdk.component.api.processor.AfterGroup)

Example 2 with Producer

use of org.talend.sdk.component.api.input.Producer in project component-runtime by Talend.

the class JdbcInput method nextRow.

@Producer
public Map<String, Object> nextRow() throws SQLException {
    if (resultSet == null) {
        // execute the query and prepare the mapping to pass to next processor
        statement = connection.prepareStatement(dataset.getQuery());
        resultSet = statement.executeQuery();
        final ResultSetMetaData metaData = resultSet.getMetaData();
        mapper.clear();
        for (int i = 0; i < metaData.getColumnCount(); i++) {
            final int idx = i;
            final String name = metaData.getColumnLabel(i);
            switch(// todo: real impl would use a mapper and not do it here
            metaData.getColumnType(i)) {
                case Types.VARCHAR:
                    mapper.put(name, () -> {
                        try {
                            return resultSet.getString(idx);
                        } catch (final SQLException e) {
                            log.warn(e.getMessage(), e);
                            return null;
                        }
                    });
                    break;
                case Types.DOUBLE:
                    mapper.put(name, () -> {
                        try {
                            return resultSet.getDouble(idx);
                        } catch (final SQLException e) {
                            log.warn(e.getMessage(), e);
                            return null;
                        }
                    });
                    break;
                default:
                    mapper.put(name, () -> {
                        try {
                            return resultSet.getObject(idx);
                        } catch (final SQLException e) {
                            log.warn(e.getMessage(), e);
                            return null;
                        }
                    });
            }
        }
    }
    return resultSet.next() ? map() : null;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Producer(org.talend.sdk.component.api.input.Producer)

Aggregations

Producer (org.talend.sdk.component.api.input.Producer)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Assessor (org.talend.sdk.component.api.input.Assessor)1 Emitter (org.talend.sdk.component.api.input.Emitter)1 PartitionMapper (org.talend.sdk.component.api.input.PartitionMapper)1 PartitionSize (org.talend.sdk.component.api.input.PartitionSize)1 Split (org.talend.sdk.component.api.input.Split)1 AfterGroup (org.talend.sdk.component.api.processor.AfterGroup)1 BeforeGroup (org.talend.sdk.component.api.processor.BeforeGroup)1 ElementListener (org.talend.sdk.component.api.processor.ElementListener)1 Output (org.talend.sdk.component.api.processor.Output)1