从apache nifi设置并获取redis堆栈服务器

2cmtqfgy  于 2023-08-02  发布在  Redis
关注(0)|答案(1)|浏览(174)

事实上,我可以在Apache Nifi中使用Redis。但我找不到任何使用Redis StackRedisJSON的参考和文档。
我尝试使用当前的解决方案,与Redis一起使用,与Redis Stack一起使用,但它不起作用。
有没有人有过这样的经历,或者你有什么建议?请在评论中告诉我。
我真的很感激你能提供的任何帮助。

to94eoyn

to94eoyn1#

你可以在Apache Nifi中的Groovy语言的帮助下尝试这种方法:
1.下载Jedis JAR文件。

**注意:**这是一个必需的依赖项,您必须将其添加到Groovy Script类路径

1.将下载的JAR文件上传到正在运行的Nifi示例的目录中。

**注意:**如果您在Docker上使用Nifi,请将JAR文件上传到您可以从Nifi示例访问的已挂载卷上。

1.将ExecuteScript处理器添加到Nifi Flow。选择Groovy in Script Engine属性,然后在Module Directory属性中填充Nifi示例上JAR文件目录的地址。
1.要连接到RedisStack,请选择ExecuteScript处理器中的Script Body属性,并写下您需要的脚本。例如:

// 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()

字符串
这就是你可以连接到RedisStack并使用Jedis库在Groovy中使用RedisJSON命令的方式,所有这些都适用于Apache Nifi。
希望这能帮到你。

相关问题