Java Code Examples for org.apache.nifi.components.PropertyDescriptor#isDynamic()

The following examples show how to use org.apache.nifi.components.PropertyDescriptor#isDynamic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EvaluateXQuery.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> results = new ArrayList<>(super.customValidate(context));

    final String destination = context.getProperty(DESTINATION).getValue();
    if (DESTINATION_CONTENT.equals(destination)) {
        int xQueryCount = 0;
        for (final PropertyDescriptor desc : context.getProperties().keySet()) {
            if (desc.isDynamic()) {
                xQueryCount++;
            }
        }
        if (xQueryCount != 1) {
            results.add(new ValidationResult.Builder().subject("XQueries").valid(false)
                    .explanation("Exactly one XQuery must be set if using destination of " + DESTINATION_CONTENT).build());
        }
    }
    return results;
}
 
Example 2
Source File: YandexTranslate.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
    final List<ValidationResult> results = new ArrayList<>();
    if (validationContext.getProperty(TRANSLATE_CONTENT).asBoolean().equals(Boolean.FALSE)) {
        boolean foundDynamic = false;
        for (final PropertyDescriptor descriptor : validationContext.getProperties().keySet()) {
            if (descriptor.isDynamic()) {
                foundDynamic = true;
                break;
            }
        }

        if (!foundDynamic) {
            results.add(new ValidationResult.Builder().subject("Text to translate").input("<none>").valid(false)
                    .explanation("Must either set 'Translate Content' to true or add at least one user-defined property").build());
        }
    }

    return results;
}
 
Example 3
Source File: GetTCP.java    From nifi with Apache License 2.0 6 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) throws ProcessException {
    this.receiveBufferSize = context.getProperty(RECEIVE_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    this.originalServerAddressList = context.getProperty(ENDPOINT_LIST).getValue();
    this.endOfMessageByte = ((byte) context.getProperty(END_OF_MESSAGE_BYTE).asInteger().intValue());
    this.connectionAttemptCount = context.getProperty(CONNECTION_ATTEMPT_COUNT).asInteger();
    this.reconnectInterval = context.getProperty(RECONNECT_INTERVAL).asTimePeriod(TimeUnit.MILLISECONDS);

    this.clientScheduler = new ScheduledThreadPoolExecutor(originalServerAddressList.split(",").length + 1);
    this.clientScheduler.setKeepAliveTime(10, TimeUnit.SECONDS);
    this.clientScheduler.allowCoreThreadTimeOut(true);

    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            this.dynamicAttributes.put(descriptor.getName(), entry.getValue());
        }
    }
}
 
Example 4
Source File: CouchbaseClusterService.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Establish a connection to a Couchbase cluster.
 * @param context the configuration context
 * @throws InitializationException if unable to connect a Couchbase cluster
 */
@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {

    bucketPasswords = new HashMap<>();
    for(PropertyDescriptor p : context.getProperties().keySet()){
        if(p.isDynamic() && p.getName().startsWith(DYNAMIC_PROP_BUCKET_PASSWORD)){
            String bucketName = p.getName().substring(DYNAMIC_PROP_BUCKET_PASSWORD.length());
            String password = context.getProperty(p).evaluateAttributeExpressions().getValue();
            bucketPasswords.put(bucketName, password);
        }
    }

    final String userName = context.getProperty(USER_NAME).evaluateAttributeExpressions().getValue();
    final String userPassword = context.getProperty(USER_PASSWORD).evaluateAttributeExpressions().getValue();

    try {
        cluster = CouchbaseCluster.fromConnectionString(context.getProperty(CONNECTION_STRING).evaluateAttributeExpressions().getValue());
        if (!StringUtils.isEmpty(userName) && !StringUtils.isEmpty(userPassword)) {
            cluster.authenticate(userName, userPassword);
        }
    } catch(CouchbaseException e) {
        throw new InitializationException(e);
    }
}
 
Example 5
Source File: FetchElasticsearchHttp.java    From nifi with Apache License 2.0 6 votes vote down vote up
private URL buildRequestURL(String baseUrl, String docId, String index, String type, String fields, ProcessContext context) throws MalformedURLException {
    if (StringUtils.isEmpty(baseUrl)) {
        throw new MalformedURLException("Base URL cannot be null");
    }
    HttpUrl.Builder builder = HttpUrl.parse(baseUrl).newBuilder();
    builder.addPathSegment(index);
    builder.addPathSegment((StringUtils.isEmpty(type)) ? "_all" : type);
    builder.addPathSegment(docId);
    if (!StringUtils.isEmpty(fields)) {
        String trimmedFields = Stream.of(fields.split(",")).map(String::trim).collect(Collectors.joining(","));
        builder.addQueryParameter(FIELD_INCLUDE_QUERY_PARAM, trimmedFields);
    }

    // Find the user-added properties and set them as query parameters on the URL
    for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
        PropertyDescriptor pd = property.getKey();
        if (pd.isDynamic()) {
            if (property.getValue() != null) {
                builder.addQueryParameter(pd.getName(), context.getProperty(pd).evaluateAttributeExpressions().getValue());
            }
        }
    }

    return builder.build().url();
}
 
Example 6
Source File: GetTCP.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) throws ProcessException {
    this.receiveBufferSize = context.getProperty(RECEIVE_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    this.originalServerAddressList = context.getProperty(ENDPOINT_LIST).getValue();
    this.endOfMessageByte = ((byte) context.getProperty(END_OF_MESSAGE_BYTE).asInteger().intValue());
    this.connectionAttemptCount = context.getProperty(CONNECTION_ATTEMPT_COUNT).asInteger();
    this.reconnectInterval = context.getProperty(RECONNECT_INTERVAL).asTimePeriod(TimeUnit.MILLISECONDS);

    this.clientScheduler = new ScheduledThreadPoolExecutor(originalServerAddressList.split(",").length + 1);
    this.clientScheduler.setKeepAliveTime(10, TimeUnit.SECONDS);
    this.clientScheduler.allowCoreThreadTimeOut(true);

    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            this.dynamicAttributes.put(descriptor.getName(), entry.getValue());
        }
    }
}
 
Example 7
Source File: YandexTranslate.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
    final List<ValidationResult> results = new ArrayList<>();
    if (validationContext.getProperty(TRANSLATE_CONTENT).asBoolean().equals(Boolean.FALSE)) {
        boolean foundDynamic = false;
        for (final PropertyDescriptor descriptor : validationContext.getProperties().keySet()) {
            if (descriptor.isDynamic()) {
                foundDynamic = true;
                break;
            }
        }

        if (!foundDynamic) {
            results.add(new ValidationResult.Builder().subject("Text to translate").input("<none>").valid(false)
                    .explanation("Must either set 'Translate Content' to true or add at least one user-defined property").build());
        }
    }

    return results;
}
 
Example 8
Source File: EvaluateJsonPath.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> results = new ArrayList<>(super.customValidate(context));

    final String destination = context.getProperty(DESTINATION).getValue();
    if (DESTINATION_CONTENT.equals(destination)) {
        int jsonPathCount = 0;

        for (final PropertyDescriptor desc : context.getProperties().keySet()) {
            if (desc.isDynamic()) {
                jsonPathCount++;
            }
        }

        if (jsonPathCount != 1) {
            results.add(new ValidationResult.Builder().subject("JsonPaths").valid(false)
                    .explanation("Exactly one JsonPath must be set if using destination of " + DESTINATION_CONTENT).build());
        }
    }

    return results;
}
 
Example 9
Source File: EvaluateXQuery.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> results = new ArrayList<>(super.customValidate(context));

    final String destination = context.getProperty(DESTINATION).getValue();
    if (DESTINATION_CONTENT.equals(destination)) {
        int xQueryCount = 0;
        for (final PropertyDescriptor desc : context.getProperties().keySet()) {
            if (desc.isDynamic()) {
                xQueryCount++;
            }
        }
        if (xQueryCount != 1) {
            results.add(new ValidationResult.Builder().subject("XQueries").valid(false)
                    .explanation("Exactly one XQuery must be set if using destination of " + DESTINATION_CONTENT).build());
        }
    }
    return results;
}
 
Example 10
Source File: ActionHandlerLookup.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext context) {
    final List<ValidationResult> results = new ArrayList<>();

    int numDefinedServices = 0;
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (descriptor.isDynamic()) {
            numDefinedServices++;
        }

        final String referencedId = context.getProperty(descriptor).getValue();
        if (this.getIdentifier().equals(referencedId)) {
            results.add(new ValidationResult.Builder()
                    .subject(descriptor.getDisplayName())
                    .explanation("the current service cannot be registered as an ActionHandler to lookup")
                    .valid(false)
                    .build());
        }
    }

    if (numDefinedServices == 0) {
        results.add(new ValidationResult.Builder()
                .subject(this.getClass().getSimpleName())
                .explanation("at least one Action Handler must be defined via dynamic properties")
                .valid(false)
                .build());
    }

    return results;
}
 
Example 11
Source File: EvaluateJsonPath.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) {
    if (descriptor.isDynamic()) {
        if (!StringUtils.equals(oldValue, newValue)) {
            if (oldValue != null) {
                cachedJsonPathMap.remove(oldValue);
            }
        }
    }
}
 
Example 12
Source File: PutKafka.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
private Properties buildKafkaConfigProperties(final ProcessContext context) {
    Properties properties = new Properties();
    String timeout = String.valueOf(context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).longValue());
    properties.setProperty("bootstrap.servers", context.getProperty(SEED_BROKERS).evaluateAttributeExpressions().getValue());
    properties.setProperty("acks", context.getProperty(DELIVERY_GUARANTEE).getValue());
    properties.setProperty("buffer.memory", String.valueOf(context.getProperty(MAX_BUFFER_SIZE).asDataSize(DataUnit.B).longValue()));
    properties.setProperty("compression.type", context.getProperty(COMPRESSION_CODEC).getValue());
    properties.setProperty("batch.size", context.getProperty(BATCH_NUM_MESSAGES).getValue());

    properties.setProperty("client.id", context.getProperty(CLIENT_NAME).getValue());
    Long queueBufferingMillis = context.getProperty(QUEUE_BUFFERING_MAX).asTimePeriod(TimeUnit.MILLISECONDS);
    if (queueBufferingMillis != null) {
        properties.setProperty("linger.ms", String.valueOf(queueBufferingMillis));
    }
    properties.setProperty("max.request.size", String.valueOf(context.getProperty(MAX_RECORD_SIZE).asDataSize(DataUnit.B).longValue()));
    properties.setProperty("timeout.ms", timeout);
    properties.setProperty("metadata.fetch.timeout.ms", timeout);

    // Set Dynamic Properties
    for (final Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            if (PARTITION_STRATEGY.equals(descriptor)) {
                continue;
            }
            if (properties.containsKey(descriptor.getName())) {
                this.getLogger().warn("Overriding existing property '" + descriptor.getName() + "' which had value of '"
                                + properties.getProperty(descriptor.getName()) + "' with dynamically set value '"
                                + entry.getValue() + "'.");
            }
            properties.setProperty(descriptor.getName(), entry.getValue());
        }
    }
    return properties;
}
 
Example 13
Source File: RouteOnAttribute.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * When this processor is scheduled, update the dynamic properties into the map
 * for quick access during each onTrigger call
 * @param context ProcessContext used to retrieve dynamic properties
 */
@OnScheduled
public void onScheduled(final ProcessContext context) {
    final Map<Relationship, PropertyValue> newPropertyMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (!descriptor.isDynamic()) {
            continue;
        }
        getLogger().debug("Adding new dynamic property: {}", new Object[]{descriptor});
        newPropertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor));
    }

    this.propertyMap = newPropertyMap;
}
 
Example 14
Source File: PutSlack.java    From nifi with Apache License 2.0 5 votes vote down vote up
@OnScheduled
public void initialize(final ProcessContext context) {
    attachments.clear();
    for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
        PropertyDescriptor descriptor = property.getKey();
        if (descriptor.isDynamic()) {
            attachments.add(descriptor);
        }
    }
}
 
Example 15
Source File: JMSConnectionFactoryProvider.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * This operation follows standard bean convention by matching property name
 * to its corresponding 'setter' method. Once the method was located it is
 * invoked to set the corresponding property to a value provided by during
 * service configuration. For example, 'channel' property will correspond to
 * 'setChannel(..) method and 'queueManager' property will correspond to
 * setQueueManager(..) method with a single argument.
 *
 * There are also few adjustments to accommodate well known brokers. For
 * example ActiveMQ ConnectionFactory accepts address of the Message Broker
 * in a form of URL while IBMs in the form of host/port pair (more common).
 * So this method will use value retrieved from the 'BROKER_URI' static
 * property 'as is' if ConnectionFactory implementation is coming from
 * ActiveMQ and for all others (for now) the 'BROKER_URI' value will be
 * split on ':' and the resulting pair will be used to execute
 * setHostName(..) and setPort(..) methods on the provided
 * ConnectionFactory. This may need to be maintained and adjusted to
 * accommodate other implementation of ConnectionFactory, but only for
 * URL/Host/Port issue. All other properties are set as dynamic properties
 * where user essentially provides both property name and value, The bean
 * convention is also explained in user manual for this component with links
 * pointing to documentation of various ConnectionFactories.
 *
 * @see #setProperty(String, String) method
 */
private void setConnectionFactoryProperties(ConfigurationContext context) {
    for (final Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        PropertyDescriptor descriptor = entry.getKey();
        String propertyName = descriptor.getName();
        if (descriptor.isDynamic()) {
            this.setProperty(propertyName, entry.getValue());
        } else {
            if (propertyName.equals(BROKER)) {
                if (context.getProperty(CONNECTION_FACTORY_IMPL).evaluateAttributeExpressions().getValue().startsWith("org.apache.activemq")) {
                    this.setProperty("brokerURL", entry.getValue());
                } else {
                    String[] hostPort = entry.getValue().split(":");
                    if (hostPort.length == 2) {
                        this.setProperty("hostName", hostPort[0]);
                        this.setProperty("port", hostPort[1]);
                    } else if (hostPort.length != 2) {
                        this.setProperty("serverUrl", entry.getValue()); // for tibco
                    } else {
                        throw new IllegalArgumentException("Failed to parse broker url: " + entry.getValue());
                    }
                }
                SSLContextService sc = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
                if (sc != null) {
                    SSLContext ssl = sc.createSSLContext(ClientAuth.NONE);
                    this.setProperty("sSLSocketFactory", ssl.getSocketFactory());
                }
            } // ignore 'else', since it's the only non-dynamic property that is relevant to CF configuration
        }
    }
}
 
Example 16
Source File: PutKafka.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
private Properties buildKafkaConfigProperties(final ProcessContext context) {
    Properties properties = new Properties();
    String timeout = String.valueOf(context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).longValue());
    properties.setProperty("bootstrap.servers", context.getProperty(SEED_BROKERS).getValue());
    properties.setProperty("acks", context.getProperty(DELIVERY_GUARANTEE).getValue());
    properties.setProperty("buffer.memory", String.valueOf(context.getProperty(MAX_BUFFER_SIZE).asDataSize(DataUnit.B).longValue()));
    properties.setProperty("compression.type", context.getProperty(COMPRESSION_CODEC).getValue());
    properties.setProperty("batch.size", context.getProperty(BATCH_NUM_MESSAGES).getValue());

    properties.setProperty("client.id", context.getProperty(CLIENT_NAME).getValue());
    Long queueBufferingMillis = context.getProperty(QUEUE_BUFFERING_MAX).asTimePeriod(TimeUnit.MILLISECONDS);
    if (queueBufferingMillis != null) {
        properties.setProperty("linger.ms", String.valueOf(queueBufferingMillis));
    }
    properties.setProperty("max.request.size", String.valueOf(context.getProperty(MAX_RECORD_SIZE).asDataSize(DataUnit.B).longValue()));
    properties.setProperty("timeout.ms", timeout);
    properties.setProperty("metadata.fetch.timeout.ms", timeout);

    // Set Dynamic Properties
    for (final Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            if (PARTITION_STRATEGY.equals(descriptor)) {
                continue;
            }
            if (properties.containsKey(descriptor.getName())) {
                this.getLogger().warn("Overriding existing property '" + descriptor.getName() + "' which had value of '"
                                + properties.getProperty(descriptor.getName()) + "' with dynamically set value '"
                                + entry.getValue() + "'.");
            }
            properties.setProperty(descriptor.getName(), entry.getValue());
        }
    }
    return properties;
}
 
Example 17
Source File: HBase_2_ClientService.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected Connection createConnection(final ConfigurationContext context) throws IOException, InterruptedException {
    final String configFiles = context.getProperty(HADOOP_CONF_FILES).evaluateAttributeExpressions().getValue();
    final Configuration hbaseConfig = getConfigurationFromFiles(configFiles);

    // override with any properties that are provided
    if (context.getProperty(ZOOKEEPER_QUORUM).isSet()) {
        hbaseConfig.set(HBASE_CONF_ZK_QUORUM, context.getProperty(ZOOKEEPER_QUORUM).evaluateAttributeExpressions().getValue());
    }
    if (context.getProperty(ZOOKEEPER_CLIENT_PORT).isSet()) {
        hbaseConfig.set(HBASE_CONF_ZK_PORT, context.getProperty(ZOOKEEPER_CLIENT_PORT).evaluateAttributeExpressions().getValue());
    }
    if (context.getProperty(ZOOKEEPER_ZNODE_PARENT).isSet()) {
        hbaseConfig.set(HBASE_CONF_ZNODE_PARENT, context.getProperty(ZOOKEEPER_ZNODE_PARENT).evaluateAttributeExpressions().getValue());
    }
    if (context.getProperty(HBASE_CLIENT_RETRIES).isSet()) {
        hbaseConfig.set(HBASE_CONF_CLIENT_RETRIES, context.getProperty(HBASE_CLIENT_RETRIES).evaluateAttributeExpressions().getValue());
    }

    // add any dynamic properties to the HBase configuration
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            hbaseConfig.set(descriptor.getName(), entry.getValue());
        }
    }

    if (SecurityUtil.isSecurityEnabled(hbaseConfig)) {
        String principal = context.getProperty(kerberosProperties.getKerberosPrincipal()).evaluateAttributeExpressions().getValue();
        String keyTab = context.getProperty(kerberosProperties.getKerberosKeytab()).evaluateAttributeExpressions().getValue();
        String password = context.getProperty(kerberosProperties.getKerberosPassword()).getValue();

        // If the Kerberos Credentials Service is specified, we need to use its configuration, not the explicit properties for principal/keytab.
        // The customValidate method ensures that only one can be set, so we know that the principal & keytab above are null.
        final KerberosCredentialsService credentialsService = context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);
        if (credentialsService != null) {
            principal = credentialsService.getPrincipal();
            keyTab = credentialsService.getKeytab();
        }

        if (keyTab != null) {
            kerberosUserReference.set(new KerberosKeytabUser(principal, keyTab));
            getLogger().info("HBase Security Enabled, logging in as principal {} with keytab {}", new Object[] {principal, keyTab});
        } else if (password != null) {
            kerberosUserReference.set(new KerberosPasswordUser(principal, password));
            getLogger().info("HBase Security Enabled, logging in as principal {} with password", new Object[] {principal});
        } else {
            throw new IOException("Unable to authenticate with Kerberos, no keytab or password was provided");
        }

        ugi = SecurityUtil.getUgiForKerberosUser(hbaseConfig, kerberosUserReference.get());
        getLogger().info("Successfully logged in as principal " + principal);

        return getUgi().doAs(new PrivilegedExceptionAction<Connection>() {
            @Override
            public Connection run() throws Exception {
                return ConnectionFactory.createConnection(hbaseConfig);
            }
        });

    } else {
        getLogger().info("Simple Authentication");
        return ConnectionFactory.createConnection(hbaseConfig);
    }

}
 
Example 18
Source File: RouteText.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));
    boolean dynamicProperty = false;

    final String matchStrategy = validationContext.getProperty(MATCH_STRATEGY).getValue();
    final boolean compileRegex = matchStrategy.equals(matchesRegularExpressionValue) || matchStrategy.equals(containsRegularExpressionValue);
    final boolean requiresExpression = matchStrategy.equalsIgnoreCase(satisfiesExpression);

    Validator validator = null;
    if (compileRegex) {
        validator = StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true);
    }

    Map<PropertyDescriptor, String> allProperties = validationContext.getProperties();
    for (final PropertyDescriptor descriptor : allProperties.keySet()) {
        if (descriptor.isDynamic()) {
            dynamicProperty = true;

            final String propValue = validationContext.getProperty(descriptor).getValue();

            if (compileRegex) {
                ValidationResult validationResult = validator.validate(descriptor.getName(), propValue, validationContext);
                if (validationResult != null) {
                    results.add(validationResult);
                }
            } else if (requiresExpression) {
                try {
                    final ResultType resultType = validationContext.newExpressionLanguageCompiler().compile(propValue).getResultType();
                    if (resultType != ResultType.BOOLEAN) {
                        results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName())
                            .explanation("expression returns type of " + resultType.name() + " but is required to return a Boolean value").build());
                    }
                } catch (final IllegalArgumentException iae) {
                    results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName())
                        .explanation("input is not a valid Expression Language expression").build());
                }
            }
        }
    }

    if (!dynamicProperty) {
        results.add(new ValidationResult.Builder().subject("Dynamic Properties")
            .explanation("In order to route text there must be dynamic properties to match against").valid(false).build());
    }

    return results;
}
 
Example 19
Source File: PutHiveStreaming.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@OnScheduled
public void setup(final ProcessContext context) {
    ComponentLog log = getLogger();

    final String metastoreUri = context.getProperty(METASTORE_URI).getValue();
    final String dbName = context.getProperty(DB_NAME).getValue();
    final String tableName = context.getProperty(TABLE_NAME).getValue();
    final boolean autoCreatePartitions = context.getProperty(AUTOCREATE_PARTITIONS).asBoolean();
    final Integer maxConnections = context.getProperty(MAX_OPEN_CONNECTIONS).asInteger();
    final Integer heartbeatInterval = context.getProperty(HEARTBEAT_INTERVAL).asInteger();
    final Integer txnsPerBatch = context.getProperty(TXNS_PER_BATCH).asInteger();
    final String configFiles = context.getProperty(HIVE_CONFIGURATION_RESOURCES).getValue();
    hiveConfig = hiveConfigurator.getConfigurationFromFiles(configFiles);

    // add any dynamic properties to the Hive configuration
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            hiveConfig.set(descriptor.getName(), entry.getValue());
        }
    }

    options = new HiveOptions(metastoreUri, dbName, tableName)
            .withTxnsPerBatch(txnsPerBatch)
            .withAutoCreatePartitions(autoCreatePartitions)
            .withMaxOpenConnections(maxConnections)
            .withHeartBeatInterval(heartbeatInterval);

    hiveConfigurator.preload(hiveConfig);

    if (SecurityUtil.isSecurityEnabled(hiveConfig)) {
        final String principal = context.getProperty(kerberosProperties.getKerberosPrincipal()).getValue();
        final String keyTab = context.getProperty(kerberosProperties.getKerberosKeytab()).getValue();

        log.info("Hive Security Enabled, logging in as principal {} with keytab {}", new Object[]{principal, keyTab});
        try {
            ugi = hiveConfigurator.authenticate(hiveConfig, principal, keyTab, TICKET_RENEWAL_PERIOD, log);
        } catch (AuthenticationFailedException ae) {
            throw new ProcessException("Kerberos authentication failed for Hive Streaming", ae);
        }
        log.info("Successfully logged in as principal {} with keytab {}", new Object[]{principal, keyTab});
        options = options.withKerberosPrincipal(principal).withKerberosKeytab(keyTab);
    } else {
        ugi = null;
    }

    allWriters = new ConcurrentHashMap<>();
    String timeoutName = "put-hive-streaming-%d";
    this.callTimeoutPool = Executors.newFixedThreadPool(1,
            new ThreadFactoryBuilder().setNameFormat(timeoutName).build());

    sendHeartBeat.set(true);
    heartBeatTimer = new Timer();
    setupHeartBeatTimer();
}
 
Example 20
Source File: YandexTranslate.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final StopWatch stopWatch = new StopWatch(true);
    final String key = context.getProperty(KEY).getValue();
    final String sourceLanguage = context.getProperty(SOURCE_LANGUAGE).evaluateAttributeExpressions(flowFile).getValue();
    final String targetLanguage = context.getProperty(TARGET_LANGUAGE).evaluateAttributeExpressions(flowFile).getValue();
    final String encoding = context.getProperty(CHARACTER_SET).evaluateAttributeExpressions(flowFile).getValue();

    final List<String> attributeNames = new ArrayList<>();
    final List<String> textValues = new ArrayList<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (descriptor.isDynamic()) {
            attributeNames.add(descriptor.getName()); // add to list so that we know the order when the translations come back.
            textValues.add(context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue());
        }
    }

    if (context.getProperty(TRANSLATE_CONTENT).asBoolean()) {
        final byte[] buff = new byte[(int) flowFile.getSize()];
        session.read(flowFile, new InputStreamCallback() {
            @Override
            public void process(final InputStream in) throws IOException {
                StreamUtils.fillBuffer(in, buff);
            }
        });
        final String content = new String(buff, Charset.forName(encoding));
        textValues.add(content);
    }

    final Invocation invocation = prepareResource(key, textValues, sourceLanguage, targetLanguage);

    final Response response;
    try {
        response = invocation.invoke();
    } catch (final Exception e) {
        getLogger().error("Failed to make request to Yandex to transate text for {} due to {}; routing to comms.failure", new Object[]{flowFile, e});
        session.transfer(flowFile, REL_COMMS_FAILURE);
        return;
    }

    if (response.getStatus() != Response.Status.OK.getStatusCode()) {
        getLogger().error("Failed to translate text using Yandex for {}; response was {}: {}; routing to {}", new Object[]{
                flowFile, response.getStatus(), response.getStatusInfo().getReasonPhrase(), REL_TRANSLATION_FAILED.getName()});
        flowFile = session.putAttribute(flowFile, "yandex.translate.failure.reason", response.getStatusInfo().getReasonPhrase());
        session.transfer(flowFile, REL_TRANSLATION_FAILED);
        return;
    }

    final Map<String, String> newAttributes = new HashMap<>();
    final Translation translation = response.readEntity(Translation.class);
    final List<String> texts = translation.getText();
    for (int i = 0; i < texts.size(); i++) {
        final String text = texts.get(i);
        if (i < attributeNames.size()) {
            final String attributeName = attributeNames.get(i);
            newAttributes.put(attributeName, text);
        } else {
            flowFile = session.write(flowFile, new OutputStreamCallback() {
                @Override
                public void process(final OutputStream out) throws IOException {
                    out.write(text.getBytes(encoding));
                }
            });

            newAttributes.put("language", targetLanguage);
        }
    }

    if (!newAttributes.isEmpty()) {
        flowFile = session.putAllAttributes(flowFile, newAttributes);
    }

    stopWatch.stop();
    session.transfer(flowFile, REL_SUCCESS);
    getLogger().info("Successfully translated {} items for {} from {} to {} in {}; routing to success",
            new Object[]{texts.size(), flowFile, sourceLanguage, targetLanguage, stopWatch.getDuration()});
}