Cassandra rapid prototyping with achilles
-
Upload
duyhai-doan -
Category
Technology
-
view
467 -
download
4
description
Transcript of Cassandra rapid prototyping with achilles
@doanduyhaiwww.achilles.io
DuyHai DOANJava/Cassandra freelance developer !
!
Daytime: + !
!
At night: +
2
@doanduyhaiwww.achilles.io
Main API
•manager.insert(entity) !
•manager.update(managedEntity) !
•manager.remove(entity) !
•manager.find(Entity.class, primaryKey)
6
@doanduyhaiwww.achilles.io
Thread safety for persistence manager
No JOINS means
7
@doanduyhaiwww.achilles.io
Thread safety for persistence manager
No JOINS means !
!
No complex object graph
8
@doanduyhaiwww.achilles.io
Thread safety for persistence manager
No JOINS means !
!
No complex object graph !
!
No state to keep
9
@doanduyhaiwww.achilles.io
What’s about proxy ?
proxifiedEntity = manager.find(Entity.class, primaryKey)
10
ProxifiedEntity
Real Entity
Dirty check
State
Setters interception
@doanduyhaiwww.achilles.io
Why proxies ?
User einstein = manager.find(User.class,”emc²”); !
einstein.setCountry(“USA”); !
manager.insert(einstein);
11
emc² Albert EINSTEIN GERMANY
emc² Albert EINSTEIN GERMANY emc² Albert EINSTEIN USA
Waste of space !!
@doanduyhaiwww.achilles.io
Why proxies ?
User einstein = manager.find(User.class,”emc²”); !
einstein.setCountry(“USA”); !
manager.update(einstein);
12
emc² Albert EINSTEIN GERMANY
emc² Albert EINSTEIN GERMANY USA
@doanduyhaiwww.achilles.io
List<Message> entities = manager.sliceQuery(Message.class) .forSelect() .withPartitionComponents(10L) .fromClusterings(“forums”).toClusterings(“forums”, uuid1) .limit(10).fromInclusiveToExclusiveBounds() .get();
Slice query
13
SELECT * FROM Message WHERE user_id = 10 AND (message_folder) ≥ (‘forums’) AND (message_folder, date) < (‘forums’, uuid1) ORDER BY message_folder ASC LIMIT 10;
@doanduyhaiwww.achilles.io
RegularStatement select = select().from(“user_messages”) .where(eq(“user_id”,bindMarker())) .and(gte(asList(“message_folder”), bindMarker())) .and(lt(asList(“message_folder”, “date”), bindMarker())) .limit(10);
!
List<Message> messages = manager.typedQuery(Message.class, select, userId, asList(“forums”), asList(“forums”, uuid1)) .get();
Typed query
14
@doanduyhaiwww.achilles.io
Native query
15
RegularStatement nativeQuery = new SimpleStatement(“SELECT * FROM Message WHERE … LIMIT 20”); !
List<TypedMap> messages = manager.nativeQuery(nativeQuery).get(); !
TypedMap firstMessage = messages.get(0); !
// with normal Map<String, Object>// String interlocutor = (String) map.get(“interlocutor”); !
String interlocutor = firstMessage.getTyped(“interlocutor”); String interlocutor = firstMessage.<String>getTyped(“interlocutor”);
@doanduyhaiwww.achilles.io
Counter API
Special proxy type
16
public Long get(); !
public void incr(); !
public void incr(Long increment); !
public void decr(); !
public void decr(Long decrement);
@doanduyhaiwww.achilles.io
Options
•Setting C* special options
•Apply to main API insert(), update(), remove() …
17
manager.insert(user, OptionsBuilder .withConsistency(QUORUM) .ttl(10) .timestamp(1357949499999L) .ifNotExists() );
@doanduyhaiwww.achilles.io
Lifecycle interceptors
Hooks into persistence lifecycle
18
public interface Interceptor<T> { public void onEvent(T entity); public List<Event> events(); }
!
public enum Event { PRE_PERSIST, POST_PERSIST, PRE_UPDATE, POST_UPDATE, PRE_REMOVE, POST_REMOVE, POST_LOAD; }
@doanduyhaiwww.achilles.io
Bean Validation (JSR-303)
@Entity(table = “entity”) public class Entity { @Id private Long id; !
@Column @NotEmpty private String name; }
Just a built-in interceptor, PRE_PERSIST, PRE_UPDATE
19
@doanduyhaiwww.achilles.io
Batch mode
Batch batch = manager.createBatch(); !
batch.startBatch(); !
batch.insert(new Entity(…..)); batch.update(…..); batch.remove(…..); batch.removeById(MyEntity.class, primaryKey); !
batch.endBatch();
C* 2.0 atomic batches
20
@doanduyhaiwww.achilles.io
Documentation
•Comprehensive Github WIKI !
•Twitter-clone demo app (demo.achilles.io) !
•Versioned documentation (HTML & PDF) !
•JavaDoc
21
@doanduyhaiwww.achilles.io
Asynchronous
•Available for ➡ main API ( insert(), update(), …) ➡ slice queries ➡ typed & native queries
23
@doanduyhaiwww.achilles.io
Roadmap for future
•C* 2.1 user defined types (UDT) & tuples !
•Reactive programming (RxJava) !
•Transparent search integration (ElasticSearch, Solr…)
24
@doanduyhaiwww.achilles.io
Where to download ?
•www.achilles.io !
•Google “Achilles Cassandra” ☞ first result
25
@doanduyhaiwww.achilles.io
Take away
•More than a simple object mapper !
•Productivity-oriented !
•KISS !
•Documented
26