Set and Get to redis stack server from apache nifi

256 views Asked by At

Actually, I can work fine with Redis in Apache Nifi. But I can't find any reference or documentation of working with Redis Stack and RedisJSON.

I've tried to use the current solution that worked with Redis, to use with Redis Stack, but it doesn't work.

Does anyone have any experience with that, or if you have any suggestions? please tell me in the comment.

I really appreciate any help you can provide.

1

There are 1 answers

2
Saeed Mousazadeh On

You can try this approach with the help of Groovy language in Apache Nifi:

  1. Download JedisJAR file.

    NOTE: This a required dependency that you have to add to your Groovy Script class path

  2. Upload the downloaded JAR file on a directory in your running Nifi Instance.

    NOTE: If your are using Nifi on Docker, upload the JAR file on a mounted volume that you can access to it from your Nifi Instance.

  3. Add ExecuteScript processor to your Nifi Flow. Select the Groovy in Script Engine Property Then fill the Module Directory Property with the address of the JAR file directory on you Nifi Instance.

  4. In Order to connect to RedisStack , Select the Script Body Property in ExecuteScript processor and write down your desired script. For Example:

// Import the necessary Jedis classes
import redis.clients.jedis.Jedis
import redis.clients.jedis.JedisPool
import redis.clients.jedis.JedisPoolConfig

// Set up a connection to RedisStack using Jedis
// Replace [REDIS_IP_ADDRESS] and [REDIS_PORT] with your actual running Redis IP address and port number.
def redisHost = "[REDIS_IP_ADDRESS]"
def redisPort = [REDIS_PORT]
def jedis = new Jedis(redisHost, redisPort)

// Connect to the RedisJSON module using the Jedis MODULE LOAD command
//This loads the RedisJSON module into RedisStack, allowing you to use RedisJSON commands
jedis.sendCommand("MODULE", "LOAD", "redisjson")

//Use RedisJSON commands with Jedis to interact with RedisJSON
// Set a JSON object
jedis.sendCommand("JSON.SET", "myjson", ".", '{"name":"John","age":30}')

// Get a JSON object
def result = jedis.sendCommand("JSON.GET", "myjson")
println(result)

// Query a JSON path
def query = ".name"
def value = jedis.sendCommand("JSON.GET", "myjson", query)
println(value)

//Do not forget to close your Jedis connection when you done
jedis.close()

This is the way you can connect to RedisStack and use RedisJSON commands in Groovy using the Jedis library and all of this is applicable in Apache Nifi.

I hope this will help you out.