Hector (API)
| Hector | |
|---|---|
| Original author | Ran Tavory |
| Final release | 2.0
/ July 16, 2014[1] |
| Repository | github |
| Written in | Java |
| Engine | |
| Type | Column-oriented DBMS |
| License | MIT License |
Lua error in mw.title.lua at line 392: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal'). Hector is a high-level client API for Apache Cassandra. Named after Hector, a warrior of Troy in Greek mythology, it is a substitute for the Cassandra Java Client, or Thrift,[2] that is encapsulated by Hector.[3] It also has Maven repository access.[4]
History
[edit | edit source]As Cassandra is shipped with the low-level Thrift (protocol), there was a potential to develop a better protocol for application developers. Hector was developed by Ran Tavory as a high-level interface that overlays the shortcomings of Thrift. It is licensed with the MIT License that allows to use, modify, split and change the design.[dubious – discuss]
Features
[edit | edit source]The high-level features of Hector are[2]
- A high-level object oriented interface to Cassandra: It is mainly inspired by the Cassandra-java-client. The API is defined in the Keyspace interface.
- Connection pooling. As in high-scale applications, the usual pattern for DAOs is a large number of reads/writes. It is too expensive for clients to open new connections with each request. So, a client may easily run out of available sockets, if it operates fast enough. Hector provides connection pooling and a nice framework that manages the details.
- Failover support: As Cassandra is a distributed data store where hosts (nodes) may go down. Hector has its own failover policy.
| Type | Comment |
|---|---|
FAIL_FAST |
If an error occurs, it fails |
ON_FAIL_TRY_ONE_NEXT_AVAILABLE |
Tries one more host before giving up |
ON_FAIL_TRY_ALL_AVAILABLE |
Tries all available hosts before giving up |
- JMX support: Hector exposes JMX for many important runtime metrics, such as number of available connections, idle connections, error statistics.
- Load balancing: A simple load balancing exists in the newer version.[5]
- Supports the command design pattern to allow clients to concentrate on their business logic and let Hector take care of the required plumbing.
Availability metrics
[edit | edit source]Hector exposes availability counters and statistics through JMX.[6]
Load balancing
[edit | edit source]Hector follows two load balancing policies with the LoadBalancingPolicy interface. The default is called RoundRobinBalancingPolicy and is a simple round-robin distribution algorithm. The LeastActiveBalancingPolicy routes requests to the pools having the lowest number of active connections, ensuring a good spread of utilisation across the cluster. .
[7]
Pooling
[edit | edit source]The ExhaustedPolicy determines how the underlying client connection pools are controlled. Currently, three options are available:[8]
| Type | Comment |
|---|---|
WHEN_EXHAUSTED_FAIL |
Fails acquisition when no more clients are available |
WHEN_EXHAUSTED_GROW |
The pool is automatically increased to react to load increases |
WHEN_EXHAUSTED_BLOCK |
Block on acquisition until a client becomes available (the default) |
Code examples
[edit | edit source]As an example, an implementation of a simple distributed hashtable over Cassandra is listed.
/**
* Insert a new value keyed by key
* @param key Key for the value
* @param value the String value to insert
*/
public void insert(final String key, final String value) throws Exception {
execute(new Command(){
public Void execute(final Keyspace ks) throws Exception {
ks.insert(key, createColumnPath(COLUMN_NAME), bytes(value));
return null;
}
});
}
/**
* Get a string value.
* @return The string value; null if no value exists for the given key.
*/
public String get(final String key) throws Exception {
return execute(new Command(){
public String execute(final Keyspace ks) throws Exception {
try {
return string(ks.getColumn(key, createColumnPath(COLUMN_NAME)).getValue());
} catch (NotFoundException e) {
return null;
}
}
});
}
/**
* Delete a key from cassandra
*/
public void delete(final String key) throws Exception {
execute(new Command(){
public Void execute(final Keyspace ks) throws Exception {
ks.remove(key, createColumnPath(COLUMN_NAME));
return null;
}
});
}
References
[edit | edit source]- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ a b Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
- ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).