2015年11月9日月曜日

The first step of Norikra

http://norikra.github.io/
https://github.com/norikra/norikra
Norikra is an open-source Stream Processing Server with SQL.
Schema-less event streams (called as 'target')
SQL processing with window specifier supports, and JOINs, SubQueries
Complex input/output events with nested Hashes and Arrays, and Query supports
Dynamic query registration/removing, without any restarts
Ultra fast bootstrap and small start
UDF plugins
This entry provides that how to run Norikra on Docker, and some example of put JSON and get result by SQL like query over HTTP API. Why using Norikra on Docker? I thought that It is important to cut down on install operations as much as possible.
Let's try.

Environment: CentOS 6.7

Install Norikra on Docker
Install Docker.
# yum install docker-io
# service docker start
Run Norikra on Docker. Use this Docker image. https://registry.hub.docker.com/u/myfinder/docker-norikra/
# docker pull myfinder/docker-norikra
# docker run -d -p 26578:26578 -p 26571:26571 -v /var/tmp/norikra:/var/tmp/norikra:rw -t myfinder/docker-norikra norikra start --stats /var/tmp/norikra/stats.json -l /var/tmp/norikra
Check it. What's up? Norikra.
# docker ps
CONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS                                                NAMES
25842cda847f        myfinder/docker-norikra   "norikra start --sta   5 seconds ago       Up 4 seconds        0.0.0.0:26571->26571/tcp, 0.0.0.0:26578->26578/tcp   naughty_mcclintock
# docker exec -it 25842cda847f ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0 10 03:07 ?        00:00:28 java -Xmx500m -Xss2048k -Djffi.boot.library.path=/jruby-1.7.19/lib/jni -server -XX:-UseGCO
root        64     0  0 03:11 ?        00:00:00 ps -ef
About one minute later..
# tail -f /var/tmp/norikra/norikra.log
2015-11-09 03:08:16 +0000 [INFO] : thread configurations, engine:{:inbound=>{:threads=>0, :capacity=>0}, :outbound=>{:threads=>0, :capacity=>0}, :route_exec=>{:threads=>0, :capacity=>0}, :timer_exec=>{:threads=>0, :capacity=>0}}, rpc:{:threads=>2}, web:{:threads=>2}
2015-11-09 03:08:16 +0000 [INFO] : logging configurations, level:nil, dir:"/var/tmp/norikra", filesize:nil, backups:nil, bufferlines:nil
2015-11-09 03:08:17 +0000 [INFO] : Loading UDF plugins
2015-11-09 03:08:17 +0000 [INFO] : Loading Listener plugins
2015-11-09 03:08:17 +0000 [INFO] : Listener loaded, name:Norikra::Listener::Stdout
2015-11-09 03:08:17 +0000 [INFO] : Listener loaded, name:Norikra::Listener::Loopback
2015-11-09 03:08:17 +0000 [INFO] : RPC server 0.0.0.0:26571, 2 threads
2015-11-09 03:08:17 +0000 [INFO] : WebUI server 0.0.0.0:26578, 2 threads
2015-11-09 03:08:17 +0000 [INFO] : Norikra server started.
# curl -I localhost:26578
HTTP/1.1 200 OK
You can see web console.

Examples
Set alias of norikra-client for docker exec.
# alias norikra-client='docker exec -it 25842cda847f norikra-client'
Make target.
# norikra-client target open www path:string status:integer referer:string agent:string userid:integer
# norikra-client target list
TARGET AUTO_FIELD
www true
1 targets found.

Example 1
Make querie name of "www.toppageviews".
# norikra-client query add www.toppageviews 'SELECT count(*) AS cnt FROM www.win:time_batch(10 sec) WHERE path="/" AND status=200'
Send events.
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/", "status":200, "referer":"", "agent":"MSIE", "userid":3}]}' http://localhost:26578/api/send
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/login", "status":301, "referer":"/", "agent":"MSIE", "userid":3}]}' http://localhost:26578/api/send
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/content", "status":200, "referer":"/login", "agent":"MSIE", "userid":3}]}' http://localhost:26578/api/send
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/page/1", "status":200, "referer":"/content", "agent":"MSIE", "userid":3}]}' http://localhost:26578/api/send
Result of querie.
# curl -X GET -H "Content-Type: application/json" --data '{"query_name":"www.toppageviews"}' http://localhost:26578/api/see
[[1447046244,{"cnt":1}]]

Example 2
Add other querie name of "www.search".
# norikra-client query add www.search 'SELECT count(*) AS cnt FROM www.win:time_batch(10 sec) WHERE path="/content" AND search_param.length() > 0'
Send events.
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/", "status":200, "referer":"", "agent":"MSIE", "userid":3}]}' http://localhost:26578/api/send
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/", "status":200, "referer":"", "agent":"Firefox", "userid":4}]}' http://localhost:26578/api/send
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/content", "status":200, "referer":"/login", "agent":"MSIE", "userid":3}]}' http://localhost:26578/api/send
# curl -X POST -H "Content-Type: application/json" --data '{"target":"www", "events":[{"path":"/content", "status":200, "referer":"/login", "agent":"Firefox", "userid":4, "search_param":"news worldwide"}]}' http://localhost:26578/api/send
Result of querie with removing events.
# curl -X GET -H "Content-Type: application/json" --data '{"query_name":"www.search"}' http://localhost:26578/api/see
[[1447046270,{"cnt":1}],[1447046280,{"cnt":0}]]

It seems good!

Top image from https://github.com/norikra/norikra.github.io