0

Let's consider a simple object with the same representation in a SQL database with properties(columns¨): Id, UserId,Ip.

I would like to prepare a query that would generate event in case that one user logs in from 2 IP adresses (or more) within 1 hour period.

My SQL looks like:

SELECT id,user_id,ip FROM w_log log
LEFT JOIN
(SELECT user_id, count(distinct ip) AS ip_count FROM w_log GROUP BY user_id) ips 
ON log.user_id = ips.user_id
WHERE ips.ip_count > 1

Transformation to EPL:

SELECT * FROM LogEntry.win:time(1 hour) logs LEFT INNER join 
(select UserId,count(distinct Ip) as IpCount FROM LogEntry.win:time(1 hour)) ips 
ON logs.UserId = ips.UserId where ips.IpCount>1

Exception:

Additional information: Incorrect syntax near '(' at line 1 column 100, please check the outer join within the from clause near reserved keyword 'select'

UPDATE:

I was successfuly able to create a schema, named window and insert data into it (or update it). I would like to increase the counter when a new LogEvent arrives in the .win:time(10 seconds) and decrease it when the event is leaving the 10 seconds window. Unfortunately the istream() doesn't seem to provide the true/false when the event is in remove stream.

create schema IpCountRec as (ip string, hitCount int)

create window IpCountWindow.win:time(10 seconds) as IpCountRec

on LogEvent.win:time(10 seconds) log 
merge IpCountWindow ipc
where ipc.ip = log.ip
when matched and istream()
  then update set hitCount = hitCount + 1 
when matched and not istream()
  then update set hitCount = hitCount - 1
when not matched
  then insert select ip, 1 as hitCount

Is there something I missed?

Vojtech B
  • 2,837
  • 7
  • 31
  • 59

1 Answers1

0

In EPL I don't think it is possible to put a query into the from-part. You can change using "insert into". An EPL alternative is also a named window or table.

user650839
  • 2,594
  • 1
  • 13
  • 9
  • 1
    The EPL you posted isn't even valid at all. I validated with the online tool. Why post invalid EPL ? The query could count using "select count(*) from Window" without manual counting . – user650839 Mar 26 '15 at 17:07
  • Ok fair point. I didn't realize that LogEvent is autocreated for me by the framework. But the rest of the query should be valid right? – Vojtech B Mar 26 '15 at 18:18