54
Complex Event Processing with Esper @antonioalegria

Complex Event Processing with Esper

Embed Size (px)

DESCRIPTION

Talk I gave at Codebits 2011 on 11/11/11 about Complex Event Processing using Esper.

Citation preview

Page 1: Complex Event Processing with Esper

Complex Event Processing with Esper

@antonioalegria

Page 2: Complex Event Processing with Esper

CEP

Complex Event Processing?

Page 3: Complex Event Processing with Esper

“Complex Event is an event that could only happen if lots of other events happened”

“CEP is a set of tools and techniques for analyzing and controlling the complex series

of interrelated events that drive modern distributed information

systems”

David Luckham, 2002

Page 4: Complex Event Processing with Esper

Example

• Church bell ringing

• Appearance of a man in a tuxedo

• Appearance of a woman in a white gown

• Rice flying through the air

Page 5: Complex Event Processing with Esper

Example

• Church bell ringing

• Appearance of a man in a tuxedo

• Appearance of a woman in a white gown

• Rice flying through the air

Wedding has happened!

Page 6: Complex Event Processing with Esper

CEP Use Cases

• Are our business processes running on time and correctly?

• Can we detect an opportunity for arbitrage in our trading department?

• Are we servicing our call center customer’s requests in a timely fashion?

• Was there a breach in our network?

Page 7: Complex Event Processing with Esper

It’s not a technology

Page 8: Complex Event Processing with Esper

like SOA!

It’s a Buzzword

Page 9: Complex Event Processing with Esper

It’s an Architectural Pattern

Page 10: Complex Event Processing with Esper
Page 11: Complex Event Processing with Esper

What do you need for CEP?

Page 12: Complex Event Processing with Esper

Event driven

Page 13: Complex Event Processing with Esper

(soft) Real-time

Page 14: Complex Event Processing with Esper

(soft) Real-timeRight

Page 15: Complex Event Processing with Esper

Across all layers of organization

Page 16: Complex Event Processing with Esper

Event Aggregation

Page 17: Complex Event Processing with Esper

Event Relationships

• Causality

• Membership

• Timing

Page 18: Complex Event Processing with Esper

Event Patterns

Page 19: Complex Event Processing with Esper

for Event Processing

Domain Specific Language

Page 20: Complex Event Processing with Esper

What you need for CEP

• Event Driven

• Right-time

• Across all layers

• Aggregation, Correlation & Traceability

• Patterns

• DSL

Page 21: Complex Event Processing with Esper

Common CEP Operations

• Windowing

• Transformation

• Aggregation/Grouping

• Merging/Union

• Filtering

• Sorting

• Correlation

• Pattern Detection

Page 22: Complex Event Processing with Esper

http://esper.codehaus.org

Esper

Page 23: Complex Event Processing with Esper
Page 24: Complex Event Processing with Esper

Esper makes it easier to build a CEP app

Page 25: Complex Event Processing with Esper

Not meant to replace Databases

Page 26: Complex Event Processing with Esper

But some parallels can be made

Page 27: Complex Event Processing with Esper

• Stores data

• On-demand queries

• Time is a data type

DBEsper

• Stores queries

• Continuous queries

• Time is a dimension

Page 28: Complex Event Processing with Esper

• SQL

• Tables

• Rows

DBEsper

• EPL

• Event Streams

• Events

Page 29: Complex Event Processing with Esper

Esper Processing Model

Page 30: Complex Event Processing with Esper

EPLEvent Processing Language

Page 31: Complex Event Processing with Esper

Event Definition (1/2)

create schema Event ( id string, // Event unique identifier ts long // Timestamp (milliseconds));

create schema Tweet ( user string,// username (e.g. ‘codebits’) text string,// actual tweet retweet_of string // references a Tweet.id) inherits Event;

Page 32: Complex Event Processing with Esper

Event Definition (2/2)

create schema Hashtag ( tweet_id string, // references a Tweet.id user string, value string) inherits Event;

// Create Url and Mention event types as a copy of Hashtag

create schema Url() copyfrom Hashtag;

create schema Mention() copyfrom Hashtag;

Page 33: Complex Event Processing with Esper

Looks like SQL...

// All eventsselect * from Event;

// Only tweetsselect user, text as statusfrom Tweet;

Page 34: Complex Event Processing with Esper

Filtering

// Tweets from @codebitsselect * from Tweet(user = 'codebits');

// Another way to do itselect * from Tweet where user = 'codebits';

// All occurrences of #codebits not posted by @codebitsselect user, value as hashtag, current_timestamp() as tsfrom Hashtag(value = 'codebits' and user != 'codebits');

Page 35: Complex Event Processing with Esper

Stream Creation and Redirection

insert into CodebitsTweetsselect * from Tweet(user = ‘codebits’);

select * from CodebitsTweets;

Page 36: Complex Event Processing with Esper

Aggregation

insert into UrlsPerSecondselect count(*) as count from Url.win:time_batch(1 sec);

// Every second (driven by above rule) calculate for last minute// - average Urls tweeted// - total Urls tweetedselect avg(count), sum(count)from UrlsPerSecond.win:length(60);

Page 37: Complex Event Processing with Esper

Grouping

select value as hashtag, count(*)from Hashtag(value != null).win:time(30 seconds)group by value;

Page 38: Complex Event Processing with Esper

Simple Event Views

select * from Tweet.win:time(5 min);

select * from Tweet.win:time_batch(1 hour);

select * from Tweet.win:length(10);

select * from Tweet.win:length_batch(10);

Page 39: Complex Event Processing with Esper

Other Standard Event Views

// Don’t use system clock, use event stream propertyselect * from Tweet.win:ext_timed(ts, 5 min);

// Last 10 tweets per userselect * from Tweet.std:groupwin(user).win:length(10);

// Top 5 Hashtagsselect * from HashtagsPerMinute.std:sort(5, count desc);

Page 40: Complex Event Processing with Esper

You can create your own custom Views

Page 41: Complex Event Processing with Esper

Correlation

// Associate hashtags used to describe a URLinsert into UrlTagsselect u.value as url, h.value as hashtagfrom Url.std:lastevent() as u, Hashtag.std:lastevent() as hwhere u.tweet_id = h.tweet_id;

insert into UrlTagsCountselect url, hashtag, count(*) as countfrom UrlTags.win:time(1 hour)group by url, hashtag;

Page 42: Complex Event Processing with Esper

Correlation (1/2)

// Every minute, output Top 3 hashtags per URLselect * from UrlTagsCount.ext:sort(3, count desc)output snapshot at(*/1,*,*,*,*);

Page 43: Complex Event Processing with Esper

Event Patterns

// Measure how long it takes users to respond to Tweetinsert into ResponseDelayselect t.id as tweet_id, t.user as author, m.value as responder, t.ts as start_ts, m.ts as stop_ts, m.ts - t.ts as durationfrom pattern [ every (t=Tweet -> m=Mention(value = t.user))];

Page 44: Complex Event Processing with Esper

Detecting Missing Events

// No Tweet from @codebits in 1 hourselect *from pattern [ every Tweet(user = ‘codebits’) -> (timer:interval(1 hour) and not Tweet(user = ‘codebits’))];

Page 45: Complex Event Processing with Esper

Other features

• Subqueries

• Inner, outer joins

• Named windows

• 1st class integration with databases (JDBC)

• Regex-like Event Pattern matching (match-recognize)

Page 46: Complex Event Processing with Esper

Esper is awesome!

Page 47: Complex Event Processing with Esper

well, duh!

It’s not a silver bullet

Page 48: Complex Event Processing with Esper

Memory Usage

Page 49: Complex Event Processing with Esper

Resilience & Persistence

Page 50: Complex Event Processing with Esper

Weak Pattern matching

Page 51: Complex Event Processing with Esper

Drill-down not trivial

Page 52: Complex Event Processing with Esper

It’s NOT distributed!

Page 53: Complex Event Processing with Esper

Not full-stack

Page 54: Complex Event Processing with Esper

For more: @antonioalegria

QA