Hector (API)

Hector
Original authorRan Tavory
Final release
2.0 / July 16, 2014; 12 years ago (2014-07-16)
Written inJava
TypeColumn-oriented DBMS
LicenseMIT License
Websiteprettyprint.me/2010/02/23/hector-a-java-cassandra-client/ Edit this on Wikidata
Repositorygithub.com/hector-client/hector

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, that is encapsulated by Hector. It also has Maven repository access.

History

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

The high-level features of Hector are

TypeComment
FAIL_FASTIf an error occurs, it fails
ON_FAIL_TRY_ONE_NEXT_AVAILABLETries one more host before giving up
ON_FAIL_TRY_ALL_AVAILABLETries all available hosts before giving up

Availability metrics

Hector exposes availability counters and statistics through JMX.

Load balancing

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. .

Pooling

The ExhaustedPolicy determines how the underlying client connection pools are controlled. Currently, three options are available:

TypeComment
WHEN_EXHAUSTED_FAILFails acquisition when no more clients are available
WHEN_EXHAUSTED_GROWThe pool is automatically increased to react to load increases
WHEN_EXHAUSTED_BLOCKBlock on acquisition until a client becomes available (the default)

Code examples

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;
      }
    });
  }