开发者学堂课程【大数据实战项目:反爬虫系统(Lua+Spark+Redis+Hadoop 框架搭建)第一阶段:Openresty 自带 redis.lua 介绍】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/669/detail/11609
Openresty 自带 redis.lua 介绍
内容介绍:
一、总体思路
二、操作 redis
一、总体思路
前面已经把获取 header 和 body,getpost 信息已经处理完了,在向后开始操作redis。
在整个数据处理流程中包括 redis,数据预处理分析,分析以后将纯爬虫数据写入到redis 当中,写入 redis 以后爬虫就准备好了,在下一步是从 redis 当中读取数据,再到前端展示,先实现一个建议版的,在 redis 中先将数据读取出来,再把流程打通如何剔除后面会讲解,实际上剔除的动作不是做数据分析所关心的,把爬虫计算出来识别出来,写到 redis 中去就好。
实际上第七步是由前端工程师需要进行的操作在 redis 中读出来去做剔除。
简易版先读出数据通过 lua 脚本读出来,后续的剔除操作就比较简单了。
二、操作 redis
1.目标:
掌握 Openresty 连接 redis 用法
2.如何通过 Openresty 读取 redis
Openresty 里面是由 nginx 和 lua 进行集成的,而 lua 里面又集成了大量的精良的以及第三方的一些插件包,lua 里面集成了 redis 了吗?
Openresty 里面有一个 lualip 这样一个包,在 Openresty 下面,lualip 里面有 redis 进入到 redis 中有一个 so 这个不是 lua 的脚本除了 redis 以外有 resty,ll 一下有 redis.lua, 这个项目定义一个本地的变量做调用 locaI _M 是变量的名称,说个就是读取数据的 lua 或者 Openresty 集成的 redis 脚本。
Local sub = string . sub
Local byte = string . byte
Local tcP .= ng ×. Socket . tcp
Local nu11= ng ×.nu1
IocaI type = type
local pairs = pairs
Local unpack = unpack
Local setmetatab1e= setmetatable
Local tonumber = tonumber
local tostring = tostring
Local rawget = rawget
Local error = error
local ok , new _ tab =pcal1( require ," table . new '')
if not ok or type ( new _ tab )~=" function " then
new _ tab = function ( narr , nrec ) return end end
locaI _M = new _ tab (0,54)
_ M ._ VERSION =· o .26'
Local common _ Cmds =
“ redis .1ua"439L,9227c
就以这个脚本为例来看怎么和 redis 进行集成。先将 redis 下载到本地,打开 redis查看里面内容,Openresty 集成的 redis 脚本有 local,oknew_tab,locaI _M,common_cmds 是操作 redis 的一些操作:获取数据,设置数据, _ M. new ( self ) 是自己_ M. 是一个 tab 一个新的 tabnew 是实例化一个 redis 在 java 中实例化需要先 new 一个对象。
所以_ M. new 就是实例化 redis 对象,self 就是自己所以可以认为没有参数,set _ timeout ( self , timeout )
设置了一个超时的时间,_ M.connect 是设置一个连接,redis 类似于数据库对里面数据进行增删改查操作需要先连接上,既然都是以_ M. 开头的,对它进行 found找一个 get,set, 获取字符串类型的没有提供方法,不是没有提供,实际上在 Local common _ Cmds 中,对字符串进行操作的方法都在这里面,get,set,Mget 实现字符串操作,对list进行操作的方法也在其中,也包括对 hashes 的操作,所以后面写脚本就按照 Local common _ Cmds 进行书写。
以上就是 Openresty 读取 redis 的介绍,redis 在 pwd 路径下 use/local/Openresty
/lualip/resty 里面有一个 redis.lua 的文件,文件里面是 Openresty 读取 redis 已经集成 redis 的一些 API 或者是一些方法都在其中,后续读取 redis 就从其中调用方法。
local ok , new _ tab peal1( reqruire ," table . new ")
if not ok or type ( new _ cab )~= " function " then
new_tab=function(narr,nrec)return()end
end
local_ M = neW _ tab (0,54)
_M_VERSION=0.26!
local common_cmds={
local sub comumands
" subscribe "," psubscribe ”
local unsub _ eommands ={
" unsubscribe "," punsubscribe "
local mt{_index=_M_{}
function _ M. new ( sel f)
local sock , err tcpO )
if not sock thenl
return nil ,err
end
return setmetatable (_sock=sock,_ subscribed = false )
end
function _ M.set _ timeout ( self , timeout )
local sock rawget (se1f,"_ sock ”)
if not sock then
return nil ," not initialized
end
return sock : settimeout ( timeout )
end
functionM . connect (self ·)
LocalSockTaWget(self," sock ")
if not sock then
return nil ," not inicialized" end
self_ SubsCribed = false return sock: connect (..) end
function _ M . set _ keepalive ( self ,
local sock rawget ( self ,"_ sock ")
if not sock then
retarn nil ," not initialized ”
end
if rawget (se1f,
retarn nil " subscribed ") then
" subscribed state "