lua脚本调试工具和lua-redis

想法是在flink异步IO中通过lua脚本访问Redis,对MQ里无序的数据进行排序合并。
一条数据的不同状态顺序或增删改顺序。
工欲善其事必先利其器。

lua IDE

https://studio.zerobrane.com/
https://download.zerobrane.com/ZeroBraneStudioEduPack-1.80-macos.dmg

如果只使用redis.lua,可以把redis.lua放到lua path下面:
https://github.com/nrk/redis-lua/edit/version-2.0/src/redis.lua

http://luarocks.github.io/luarocks/releases/
http://luarocks.github.io/luarocks/releases/luarocks-3.1.3.tar.gz
https://github.com/luarocks/luarocks/wiki/Installation-instructions-for-macOS

安装lua5.3.5、lua-cjson和redis-lua

需要注意的是redis服务器端是支持cjson的,最好确认,还要注意具体语法是否支持。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
$ tar xvf lua-5.3.5.tar.gz
$ cd lua-5.3.5
$ make macosx
$ sudo make install
$ lua -v
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio

$ git clone https://github.com/mpx/lua-cjson.git
$ cd lua-cjson
$ vi Makefile
CJSON_LDFLAGS = -bundle -undefined dynamic_lookup
#FPCONV_OBJS = fpconv.o
CJSON_CFLAGS +=     -DUSE_INTERNAL_FPCONV
CJSON_CFLAGS +=     -DIEEE_BIG_ENDIAN
CJSON_CFLAGS +=     -pthread -DMULTIPLE_THREADS

$ make
$ make install

$ brew update
$ brew install luarocks
$ luarocks install redis-lua

$ vi ~/.bash_profile
export ZBS=/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio
export LUA_PATH="./?.lua;$ZBS/lualibs/?/?.lua;$ZBS/lualibs/?.lua"

$ source ~/.bash_profile

ZeroBraneStudio使用cjson报错

1
2
3
4
error loading module 'cjson' from file '/usr/local/lib/lua/5.3/cjson.so':
dlopen(/usr/local/lib/lua/5.3/cjson.so, 6): no suitable image found. Did find:
/usr/local/lib/lua/5.3/cjson.so: mach-o, but wrong architecture
/usr/local/lib/lua/5.3/cjson.so: mach-o, but wrong architecture

解决思路:修改配置cfg/user.lua然后重启

https://stackoverflow.com/questions/47320554/wrong-architecture-error-when-loading-lgi

1
lua53 binary that comes with ZeroBrane Studio is i386 executable (32bit), but the library you are using is probably 64bit, hence the message about the wrong architecture.
1
2
$ vi /Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/cfg/user.lua
path.lua53 = '/usr/local/bin/lua'

重启ZeroBraneStudio即可

示例程序

注意Since Lua 5.2, loadstring has been replaced by load.

1
2
3
4
5
6
local cjson = require("cjson")

local sampleJson = [[{"age":"18","testArray":{"array":[8,9,11,14,25]},"blog":"https://icocoro.me"}]]
local data = cjson.decode(sampleJson)
print(data["age"])
print(data["testArray"]["array"][1])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
local redis = require 'redis'
local host = "127.0.0.1"
local port = 6379

-- 定义为global 否则redis.call无法调用
client = redis.connect(host, port)

local response = client:ping()
print(("response: %s"):format(response))

redis.call = function(cmd, ...)
return assert(load('return client:'.. string.lower(cmd) ..'(...)'))(...)
end

local hk = 'etlpre_test_p0'
local isExists = client:exists(hk)
local h = redis.call("exists", hk)
print(h)

-- 先判断key是否已经存在
if not isExists then
print(package.path)
end

需要注意的是json字符串在decode之后成为table,就不能再encode为json字符串了!!!
Please note that nested tables can require a lot of memory to encode. T

所以,针对简单JSON,比如只有一层{},解析JSON,然后拼接成新的JOSN字符串输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- 旧的消息 来的晚
local i = cjson.decode(info)
-- newest是最新的消息 来的早
local j = cjson.decode(newest)
-- 最新的消息在等待旧的消息到来后重新合并
-- 遍历当前info,如果info里的key不在newest,则添加到newest里面

local tbl = {(string.gsub(newest, '}', ''))}

for key, value in pairs(i) do
if j[key] == nil and value ~= nil then
if type(value) == 'number' then
table.insert( tbl, (',"%s":%d'):format(key,value))
elseif type(value) == 'boolean' then
table.insert( tbl, (',"%s":%s'):format(key,value))
else
table.insert( tbl, (',"%s":"%s"'):format(key,value))
end
end
end
table.insert( tbl, '}')

print(table.concat(tbl))

演示图片

image

image

image

image

image

image

邵志鹏 wechat
扫一扫上面的二维码关注我的公众号
0%