Nifi自定义处理器如何获取用于Redis的数据库连接并重用该连接

时间:2019-07-13 22:02:12

标签: redis apache-nifi

使用Nifi我正在查询Redis服务器以获取地理空间数据。但是一旦我停止处理器,我正在查询的密钥就会被删除。

我想在处理器中创建和重用redis连接。

但是该代码在普通的Java类中运行,并且如果我运行该代码,密钥也不会删除。

我不明白为什么要删除密钥。以下是Redis地理空间数据的自定义处理器代码。

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.trinity.redis;


import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.annotation.behavior.ReadsAttribute;
import org.apache.nifi.annotation.behavior.ReadsAttributes;
import org.apache.nifi.annotation.behavior.WritesAttribute;
import org.apache.nifi.annotation.behavior.WritesAttributes;
import org.apache.nifi.annotation.lifecycle.OnScheduled;
import org.apache.nifi.annotation.lifecycle.OnStopped;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.SeeAlso;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.ProcessorInitializationContext;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.util.StandardValidators;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.params.GeoRadiusParam;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Tags({"example"})
@CapabilityDescription("Provide a description")
@SeeAlso({})
@ReadsAttributes({@ReadsAttribute(attribute="", description="")})
@WritesAttributes({@WritesAttribute(attribute="", description="")})
public class IotHubRedis extends AbstractProcessor {

    private volatile Jedis jedisPool;


    public static final PropertyDescriptor ConnectionHost = new PropertyDescriptor
            .Builder().name("ConnectionHost")
            .displayName("ConnectionHost")
            .description("ConnectionHost")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();
    public static final PropertyDescriptor ConnectionPort = new PropertyDescriptor
            .Builder().name("ConnectionPort")
            .displayName("ConnectionPort")
            .description("ConnectionPort")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();

    public static final PropertyDescriptor Radius = new PropertyDescriptor
            .Builder().name("Radius")
            .displayName("Radius")
            .description("Radius")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();


      public static final PropertyDescriptor Lattitude = new PropertyDescriptor
      .Builder().name("Lattitude") .displayName("Lattitude")
      .description("Lattitude") .required(true)
      .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
      .expressionLanguageSupported(true) .build();


      public static final PropertyDescriptor Longitude = new PropertyDescriptor
      .Builder().name("Longitude") .displayName("Longitude")
      .description("Longitude") .required(true)
      .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
      .expressionLanguageSupported(true) .build();


    public static final PropertyDescriptor RedisKey = new PropertyDescriptor
            .Builder().name("RedisKey")
            .displayName("RedisKey")
            .description("RedisKey")
            .required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
            .build();

    public static final Relationship Success = new Relationship.Builder()
            .name("Success")
            .description("Success")
            .build();

    public static final Relationship Failure = new Relationship.Builder()
            .name("Failure")
            .description("Failure")
            .build();

    private List<PropertyDescriptor> descriptors;

    private Set<Relationship> relationships;

    @Override
    protected void init(final ProcessorInitializationContext context) {
        final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
        descriptors.add(ConnectionHost);
        descriptors.add(ConnectionPort);
        descriptors.add(Radius);
        descriptors.add(Lattitude);
        descriptors.add(Longitude);
        descriptors.add(RedisKey);
        this.descriptors = Collections.unmodifiableList(descriptors);

        final Set<Relationship> relationships = new HashSet<Relationship>();
        relationships.add(Success);
        relationships.add(Failure);
        this.relationships = Collections.unmodifiableSet(relationships);


    }

    @Override
    public Set<Relationship> getRelationships() {
        return this.relationships;
    }

    @Override
    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
        return descriptors;
    }

    @OnScheduled
    public void onScheduled(final ProcessContext context) {
        try {
            //jedisPool = new Jedis(context.getProperty("ConnectionHost").toString(), Integer.parseInt(context.getProperty("ConnectionPort").toString()));
            //getLogger().info("----->Redis Connection is Successful"+jedisPool);
            jedisPool = new Jedis("192.168.8.214",6379);
        } catch (Exception e) {
            getLogger().error("Unable to establish Redis connection pool.");
        }

    }

    @OnStopped
    public void closeRedisPool(final ProcessContext context) {
        jedisPool.flushAll();
        jedisPool.close();
    }


    @Override
    public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
        FlowFile flowFile = session.get();
        Double lat=Double.parseDouble(context.getProperty("Lattitude").evaluateAttributeExpressions(flowFile).getValue().toString());
        Double lon=Double.parseDouble(context.getProperty("Longitude").evaluateAttributeExpressions(flowFile).getValue().toString());
        String location="NA";
        Double geoRadiusInKm=Double.parseDouble(context.getProperty("Radius").toString());
        String RedisKey=context.getProperty("RedisKey").toString();
        if ( flowFile == null ) {
            return;
        }

        else {
            try {

                if(!(lat==null || lat==null|| lat.equals("") || lon.equals(""))) {
                GeoRadiusParam param = GeoRadiusParam.geoRadiusParam();
                param.withDist().sortAscending().count(1);
                 List<GeoRadiusResponse> georadius2 = jedisPool.georadius(RedisKey.toString().getBytes(),lat, lon, geoRadiusInKm, GeoUnit.KM, param);
                 //flowFile = session.putAttribute(flowFile, "connection1", "------------>"+RedisKey+"=="+lat+"=="+lon+"=="+geoRadiusInKm+"=="+param);
                 if (!georadius2.isEmpty()) {
                    for (GeoRadiusResponse geoRadiusResponse : georadius2) {
                                    location = geoRadiusResponse.getMemberByString();
                    }
                    flowFile = session.putAttribute(flowFile, "msg", "------------>if block inside try");
                    flowFile = session.putAttribute(flowFile, "location", location);
                    session.transfer(flowFile, Success);
                }
                else {
                    flowFile = session.putAttribute(flowFile, "msg", "------------>else georadius2 is empty");
                    flowFile = session.putAttribute(flowFile, "location", location);
                    session.transfer(flowFile, Success);
                }
            }
                else {
                    flowFile = session.putAttribute(flowFile, "msg", "------------>lat lon null or empty");
                    flowFile = session.putAttribute(flowFile, "location", location);
                    session.transfer(flowFile, Success);
                }


        }
            catch (JedisConnectionException e) { 
                session.transfer(flowFile, Success);

            }
        // TODO implement
    }

}
}

2 个答案:

答案 0 :(得分:1)

Error Traceback (most recent call last): File "C:\Users\Ekami\Documents\workspace\NomadSpeed-Web\users\tests.py", line 64, in test_jwt_auth resp = self.client.post(REST_LOGIN_URL, {'email': email, 'password': password}) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\test.py", line 300, in post path, data=data, format=format, content_type=content_type, **extra) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\test.py", line 213, in post return self.generic('POST', path, data, content_type, **extra) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\test.py", line 238, in generic method, path, data, content_type, secure, **extra) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\test\client.py", line 422, in generic return self.request(**r) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\test.py", line 289, in request return super(APIClient, self).request(**kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\test.py", line 241, in request request = super(APIRequestFactory, self).request(**kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\test\client.py", line 503, in request raise exc_value File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\views\generic\base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper return bound_method(*args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper return view(request, *args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_auth\views.py", line 49, in dispatch return super(LoginView, self).dispatch(*args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\views.py", line 495, in dispatch response = self.handle_exception(exc) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\views.py", line 455, in handle_exception self.raise_uncaught_exception(exc) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\views.py", line 492, in dispatch response = handler(request, *args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_auth\views.py", line 103, in post self.serializer.is_valid(raise_exception=True) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\serializers.py", line 236, in is_valid self._validated_data = self.run_validation(self.initial_data) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_framework\serializers.py", line 437, in run_validation value = self.validate(value) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\rest_auth\serializers.py", line 108, in validate email_address = user.emailaddress_set.get(email=user.email) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\ProgramData\Anaconda3\envs\nomad\lib\site-packages\django\db\models\query.py", line 408, in get self.model._meta.object_name allauth.account.models.EmailAddress.DoesNotExist: EmailAddress matching query does not exist.

您正在呼叫https://redis.io/commands/flushall

这将删除所有现有数据库的所有键...

因此,只需删除此命令

答案 1 :(得分:0)

我知道这有点旧,但是在您的代码中,您有:

FlowFile flowFile = session.get();
// Doing something with flowfile
if ( flowFile == null ) {
    return;
}

您了解,当flowFile为null时,这可能会/将会爆炸?