How to output to console from Redis Lua script?

Why doesn't it print hello?

$ redis-cli 127.0.0.1:6379> eval "print( 'hello' )" 0 (nil) 127.0.0.1:6379> 

Launch 2.8.14 on Mac OS X, 2.8.12 on Windows 7.

I am calling in the Lua script from Jedis. To develop them is how to build a ship with a bottle, wear mittens, and someone hit me in the face. My ultimate goal is to somehow recreate a semi-functional development stack using the print trace, debug, whatever commands.

My workaround for my Lua script is a Redis list called "log" returning it to Jedis and then dumping the contents. Kind:

 redis.call( 'del', 'log' ) redis.call( 'rpush', 'log', 'trace statement 1' ) redis.call( 'rpush', 'log', 'trace statement 2' ) ... redis.call( 'lrange', 'log', 0, -1 ) 

Thanks in advance for any advice, help, etc.

Update: I just noticed that "hello" displays through the terminal window for the redis-server executable. Smart. So now I am each terminal for redis server, redis-cli interactive and redis-cli.

Update 2: it turned out that I can print the trace instructions on the redis-cli monitor as follows:

 eval "redis.call( 'echo', 'ugh')" 0 

Which would look like this:

 123.456 [0 127.0.0.1:57709] "eval" "redis.call( 'echo', 'ugh')" "0" 123.456 [0 lua] "echo" "ugh" 
+7
lua redis jedis
source share
3 answers

Slap in the palm. Finally it turned out that redis.log (loglevel, message). Which also writes to the output of the redis-server console.

Special thanks to the helpful anonymous voter. You are an excellent person.

+10
source share

There are better ways to develop LUA scripts against redis.

Using lua logs is one way. But you can also publish on-demand logs in the debugging topic by subscribing to it.

You can also set up an IDE with lua intercept points, which I consider to be the best development solution: http://www.trikoder.net/blog/make-lua-debugging-easier-in-redis-87/

Also, remember that automatic testing, unit tests, and / or integrations are useful (running against an instance of debug redis).

+1
source share

using jedis, here is how you can do it .. this is an example of using the set and get commands. you need to include the jar file jedis-2.6.0 in the classpath.

 //jar file - jedis-2.6.0.jar import redis.clients.jedis.Jedis; public class MainClass { public static void main(String[] args){ Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); jedis.set("name", "a"); System.out.println("Stored string (b4 lua) : "+ jedis.get("name")); String script="redis.call('set','name','b')"; jedis.eval(script); System.out.println("Stored string : "+ jedis.get("name")); } } 

output: Connection to the server successfully Saved string (b4 lua): a Saved string: b

+1
source share

All Articles