Original author(s) | Ran Tavory |
---|---|
Stable release | 0.7.0-23 |
Preview release | none / n/a |
Development status | Active |
Written in | Java |
Operating system | Cross-platform |
Available in | English |
Type | Column-oriented DBMS |
License | MIT License |
Website | https://github.com/rantav/hector |
Hector is a high-level client API for Apache Cassandra. Named after Hector, the builder of Troy in Greek mythology, it is a substitute for the Cassandra Java Client, or Thrift,[1] that is encapsulated by Hector.[2] It also has Maven repository access.[3]
Contents |
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.
Over the last couple of days I got the conclusion that the java client I’ve been using so far to speak to cassanrda wasn’t satisfactory. I used the one simply called cassandra-java-client, which is a good start but had some shortcomings I could just not live with (no support for Cassandra v0.5, no JMX and no failover). So I’ve written my own.
The high-level features of Hector are[1]
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 |
Hector exposes availability counters and statistics through JMX.[5]
Hector follows two load balancing policies with the LoadBalancingPolicy
interface. The default is called the LeastActiveBalancingPolicy
and routes requests to the pools having the lowest number of active connections, ensuring a good spread of utilisation across the cluster. The RoundRobinBalancingPolicy
is a simple round-robin distribution algorithm.[6]
The ExhaustedPolicy
determines how the underlying client connection pools are controlled. Currently, three options are available:[7]
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) |
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; } }); }
LoadBalancingPolicy
interface. Out of the box, two basic implementations are provided: LeastActiveBalancingPolicy
(the default) and RoundRobinBalancingPolicy
. LeastActiveBalancingPolicy
routes requests to the pools with the lowest number of active connections. This ensures a good spread of utilization across the cluster by sending requests to the machine that has the least number of connections. RoundRobinBalancingPolicy
implements a simple round-robin distribution algorithm."